简体   繁体   中英

How to use search_after in Elastic High Level Rest Client for pagination

I am using elastic RestHighLevelClient to talk to ES. I am able to query basic queries. Although i am trying to use teh search_after api to design a paginated api from my front end queries. Although query_after is simple to use in the RestLowLevelClient api, i am not able to figure how to use it in the HighLevel API.

Looks like the lucene api has SearchAfterSortedDocQuery, but i am not able to figure how to use it with the elastic search api. For example: in the code below i initialize SearchAfterSortedDocQuery query but not sure how to use it.

        RestHighLevelClient client = ESRestClient.getClient();

        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery("field1",value1));
        searchSourceBuilder.query(QueryBuilders.termQuery("field2",value2));

        searchSourceBuilder.from(0);
        searchSourceBuilder.size(limit);
        SortField[] sortFields = new SortField[2];

        sortFields[0]= new SortField("field1", SortField.Type.LONG);
        sortFields[1]= new SortField("field2", SortField.Type.LONG);


        FieldDoc fieldDoc = new FieldDoc(0,0); //Is this correct? how to initialize field doc?
        fieldDoc.fields = new Object[2];
        fieldDoc.fields[0] = new Long("-156034");
        fieldDoc.fields[1] = new Long("2297416849");

        SearchAfterSortedDocQuery query = new SearchAfterSortedDocQuery(new Sort(sortFields), fieldDoc);
        searchRequest.source(searchSourceBuilder);
        searchRequest.indices("index1");

        try {
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(response);
        }
        catch(IOException e){
            System.out.println(e);
        }

    }

I think instead of you using SearchAfterSortedDocQuery, just set search_after in searchSourceBuilder look like below

searchSourceBuilder.searchAfter(new Object[]{sortAfterValue});

after that use SearchRequest and rest client to get the response

SearchRequest searchRequest = new SearchRequest("index");
    searchRequest.types("type");
    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT)
    SearchHits hits = searchResponse.getHits();

final you should keep the last sort value by getSortValues() from hits to go to the next page.

hits.getAt(lastIndex).getSortValues()

Elasticsearch search_after api

hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM