我正在使用弹性RestHighLevelClient与ES交谈。我能够查询基本查询。虽然我正在尝试使用teh search_after api从前端查询设计分页的api。尽管query_after在RestLowLevelClient api中很容易使用,但我无法弄清楚如何在HighLevel API中使用它。
看起来像lucene api有SearchAfterSortedDocQuery,但是我不知道如何在弹性搜索api中使用它。例如:在下面的代码中,我初始化SearchAfterSortedDocQuery查询,但不确定如何使用它。
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);
}
}
答案 0 :(得分:1)
我认为不是您使用SearchAfterSortedDocQuery,而是在searchSourceBuilder中设置search_after,如下所示
searchSourceBuilder.searchAfter(new Object[]{sortAfterValue});
然后使用SearchRequest和rest客户获取响应
SearchRequest searchRequest = new SearchRequest("index");
searchRequest.types("type");
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT)
SearchHits hits = searchResponse.getHits();
最后,您应该保留点击中getSortValues()的最后一个排序值,以转到下一页。
hits.getAt(lastIndex).getSortValues()
Elasticsearch search_after api
希望获得帮助。