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.


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(

        request.addParameter("size", size);

        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("query");
        builder.field("vector", vectors);
        builder.field("k", k);


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!