Java client SearchRequest query building for neural plugin

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:

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.

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

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

build.gradle

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

TestNeuralQuery.java

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

Thanks @Navneet a lot!

Hi @chinmoydas.
I created a PR to solve this problem

@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?

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