Search after example in java8

I cant follow example mentioned in rest api documentation.Elastic java client is really tough to understand.
Can some one share how to use searchafter api with java tutorial.
There is some sort field we need to share what that will be

Hi @suresh_chaudhari

I think I already answered a similar question, here's an example using RHLC. I sort by name in this example

If you are using the new java client, use the example to adapt.


    //FIRST REQUEST
    SearchSourceBuilder builder = new SearchSourceBuilder();
    builder.sort(SortBuilders.fieldSort("name").order(SortOrder.DESC));
    builder.size(2);
    builder.query(QueryBuilders.matchAllQuery());

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("idx_movies_suggest");
    searchRequest.source(builder);
    var response = client.search(searchRequest, RequestOptions.DEFAULT);

    System.out.println(response);

    //SECOND REQUEST WITH SEARCH AFTER
    builder = new SearchSourceBuilder();
    builder.sort(SortBuilders.fieldSort("name").order(SortOrder.DESC));
    builder.size(2);
    builder.query(QueryBuilders.matchAllQuery());

    searchRequest = new SearchRequest();
    searchRequest.indices("idx_movies_suggest");
    searchRequest.source(builder);

    //USING SEARCH AFTER
    Object[] arrSearchAfter = new Object[]{"batman"};
    builder.searchAfter(arrSearchAfter);

    searchRequest = new SearchRequest();
    searchRequest.indices("idx_movies_suggest");
    searchRequest.source(builder);
    response = client.search(searchRequest, RequestOptions.DEFAULT);

    System.out.println(response);
1 Like

Thanks you @RabBit_BR

1 Like

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