Home > Mobile >  Migrate ElasticsearchClient#Msearch JavaApi (8.5)
Migrate ElasticsearchClient#Msearch JavaApi (8.5)

Time:11-09

I cannot figure out the new ElasticsearchClient#Msearch Java API for Multi-Search Requests.

In version 7.17 I used the Multisearch request like this:

    MultiSearchRequest request = new MultiSearchRequest();
    List<SearchRequest> searchRequests = getSearchRequests();
    MultiSearchResponse multiSearchResponse = restClient.msearch(searchRequests, RequestOptions.DEFAULT);

With the new API, you have to use ElasticsearchClient#mSearch(MsearchRequest request, Class<TDocument> tDocumentClass). In MsearchRequest you can add RequestItem with MsearchRequest.Builder#searches(List<RequestItem> list), however the RequestItem contains MultisearchHeader and MultisearchBody but does not implement anything from the SearchRequest object.

Any suggestion on what I have to change, use ElasticsearchClient#mSearch?

CodePudding user response:

You can use this simple code to test:

var response = client.msearch(MsearchRequest.of(ms -> ms.searches(
    List.of(RequestItem.of(ri -> ri
            .header(MultisearchHeader.of(mh -> mh.index("idx_movies")))
            .body(MultisearchBody.of(msb -> msb.query(MatchAllQuery.of(ma -> ma)._toQuery()))
            )),
        RequestItem.of(ri -> ri
            .header(MultisearchHeader.of(mh -> mh.index("idx_cars")))
            .body(MultisearchBody.of(msb -> msb.query(MatchAllQuery.of(ma -> ma)._toQuery()))
            )))
    )),
    Object.class);
  • Related