k-NN Query from ES High Level Client

Hello! I was wondering if there were any examples of using the k-NN query interface with the elasticsearch high level rest client for java? This query type isn’t part of elasticsearch core therefore there isn’t a predefined builder packaged with the high level client.

Thanks,
Dylan

Hi @dnock,

We use the high level client in our Rest Test Cases. But, in those we use the KNNQueryBuilder. This is probably not accessible. So, something like this should work:

        Request request = new Request(
                "POST",
                "/<index_name>/_search"
        );

        request.addParameter("size", size);

        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("query");
        builder.startObject("knn");
        builder.startObject("<field_name>");
        builder.field("vector", vectors);
        builder.field("k", k);
        builder.endObject().endObject().endObject().endObject();

        request.setJsonEntity(Strings.toString(builder));

I’m similarly passing a serialized request to the low level client. A QueryBuilder integration with the high level client would be amazing! Thanks for the response!