Search By Id in Java API Client

Hi,

I am trying to migrate my existing Elastic Search Rest High Level Client from 6.3.2 to the latest Java Client API.

My current code has

               SearchRequest sr = new SearchRequest(indexName);
                searchRequest.types(DOCUMENT_TYPE_NAME);
                SearchSourceBuilder srBuilder = new SearchSourceBuilder();
                searchSourceBuilder.query(QueryBuilders.idsQuery().addIds(myids));
                searchSourceBuilder.from(0).size(ids.size());
                searchRequest.source(searchSourceBuilder);
                SearchResponse searchResponse = client.search(searchRequest);

In Java API Client though, I can't seem to find a way to work with the Id Query. Would appreciate any help here.

Thank you

Hi @DavJane

This code can help you:

List<String> ids = List.of("1", "2");
var idsQuery = IdsQuery.of(idq -> idq.values(ids));
var request = SearchRequest.of(s -> s
    .from(0)
    .size(ids.size())
    .query(Query.of(q -> q.ids(idsQuery))
    ));

Thank you very much!

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