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?
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.
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();
}
}
@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?