# Java client SearchRequest query building for neural plugin

**URL:** <https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895>\
**Category:** Machine Learning\
**Tags:** troubleshoot\
**Created:** [September 15, 2023, 2:11pm UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895 "2023-09-15T14:11:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinmoydas](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@chinmoydas](https://forum.opensearch.org/u/chinmoydas)\
**Post date:** [September 15, 2023, 2:11pm UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/1 "2023-09-15T14:11:02Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser): OpenSearch 2.7 in AWS managed domain

**Describe the issue** : I am developing a java client code to be deployed in AWS Lambda for neural search. In the API based search, the payload is as below:  
GET /nlp\_pqa\_demo/\_search  
{  
“\_source”: [“field1”, “field2”],  
“size”: 3,  
“query”: {  
“neural”: {  
“field1\_vector”: {  
“query\_text”: “my query text”,  
“model\_id”: “XtvQbYoBJWF-B3wGCHdb”,  
“k”: 30  
}  
}  
}  
}  
How to generate such a query using the SearchRequest query builder?

**Configuration** :

**Relevant Logs or Screenshots** :

---

<div class="post-metadata">

**Author:** ![Navneet](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/navneet/32/3645_2.png) [@Navneet](https://forum.opensearch.org/u/Navneet)\
**Post date:** [September 15, 2023, 11:05pm UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/2 "2023-09-15T23:05:55Z")

</div>

Hi @chinmoydas ,  
Thanks for reaching out.

So, currently there is no standard NeuralSearch query builder class to build the query. You need to take a different route here. You have to create the json payload and then send that as a HTTP request using the OpenSearch client.

Please feel free to cut a github issue for creating QueryBuilder for neural Query Clause.

> **[Issues · opensearch-project/opensearch-java](https://github.com/opensearch-project/opensearch-java/issues)**
>
> Java Client for OpenSearch. Contribute to opensearch-project/opensearch-java development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![chinmoydas](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@chinmoydas](https://forum.opensearch.org/u/chinmoydas)\
**Post date:** [September 16, 2023, 10:58am UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/3 "2023-09-16T10:58:03Z")

</div>

Thanks @Navneet . Can you please share a sample code for sending a JSON as HTTP request using the OpenSearch client?

---

<div class="post-metadata">

**Author:** ![Navneet](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/navneet/32/3645_2.png) [@Navneet](https://forum.opensearch.org/u/Navneet)\
**Post date:** [September 16, 2023, 11:16pm UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/4 "2023-09-16T23:16:21Z")

</div>

Hi @chinmoydas  
Please find the code snippet below. The query goes here: `searchRequest.setJsonEntity`

build.gradle

```auto
dependencies {
     implementation 'org.opensearch.client:opensearch-java:2.6.0'
     implementation 'org.opensearch.client:opensearch-rest-client:2.9.0'
}

```

TestNeuralQuery.java

```auto
package navneev.neural;

import org.apache.http.HttpHost;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestNeuralQuery {
    public static void main(String[] args) throws Exception {
        final HttpHost host = new HttpHost("<ENDPOINT>", <PORT>, "<http/https>");
        // Set the authentication and other things in this rest client Ref: https://opensearch.org/docs/latest/clients/java/#initializing-the-client-with-ssl-and-tls-enabled-using-restclient-transport
        final RestClient restClient = RestClient.builder(host).build();

        Request searchRequest = new Request("POST", "<INDEX_NAME>/_search");
        // Set the query in the Json Entity, you can set your neural query here.
        searchRequest.setJsonEntity("{\"query\": { \"match_all\": {} }}");

        Response response = restClient.performRequest(searchRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }

        System.out.println(sb);
        restClient.close();
    }
}

```

I hope this helps.

For more info you can refer this documentation: [Java client - OpenSearch documentation](https://opensearch.org/docs/latest/clients/java)

---

<div class="post-metadata">

**Author:** ![chinmoydas](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@chinmoydas](https://forum.opensearch.org/u/chinmoydas)\
**Post date:** [September 17, 2023, 1:38pm UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/5 "2023-09-17T13:38:15Z")

</div>

Thanks @Navneet a lot!

---

<div class="post-metadata">

**Author:** ![grant1337](https://avatars.discourse-cdn.com/v4/letter/g/43a26b/32.png) [@grant1337](https://forum.opensearch.org/u/grant1337)\
**Post date:** [October 18, 2023, 9:20am UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/6 "2023-10-18T09:20:31Z")

</div>

Hi @chinmoydas.  
I created a [PR](https://github.com/opensearch-project/opensearch-java/pull/674) to solve this problem

---

<div class="post-metadata">

**Author:** ![Priyank](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Priyank](https://forum.opensearch.org/u/Priyank)\
**Post date:** [November 28, 2023, 10:11am UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/7 "2023-11-28T10:11:29Z")

</div>

@Navneet There is still no support for NeuralQueryBuilder for Java high-level rest client. Should we migrate to java client for the same or there is still a possibility to support neural query in java high-level rest client?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [January 27, 2024, 10:11am UTC](https://forum.opensearch.org/t/java-client-searchrequest-query-building-for-neural-plugin/15895/8 "2024-01-27T10:11:35Z")

</div>

Closed on January 27, 2024, 10:11am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
