如何使用eLasticsearchRequest的searchafter查询参数确定下一页是否存在
我们在UI上有更多负载按钮,要求下一页。我们已经使用了搜索程序查询参数来实现ES分页
我正在使用Elasticsearch响应中存在的sortValue来生成NextPagetoken。
我找到了一个方案,即使下一页不存在,我也会在弹性搜索响应中获得排序值,当我用这种排序值查询es时,它会返回空响应。
我该怎么做才能避免这种情况?我可以提前确定下一页是否存在?
我想到的另一件事是,如果ES响应大小小于请求的大小,我们可以假设我们没有任何新页面吗?
// searchRequest代码代码段
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(query)
.fetchSource(getSessionsQuery.getColumns().toArray(String[]::new), null)
.size(getSessionsQuery.getResultSize())
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(TIMESTAMP_FIELD_KEY)).order(SortOrder.ASC))
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(VERSION_SORT_KEY)).order(SortOrder.DESC));
if (!StringUtils.isNullOrEmpty(getSessionsQuery.getNextToken())) {
final String[] tokens = getSessionsQuery.getNextToken().split(ES_SORT_FIELD_DELIMITER);
if (tokens.length > 0) {
sourceBuilder.searchAfter(tokens);
}
}
return new SearchRequest().indices(route.getTechnologyConfiguration().get(INDEX_KEY)).source(sourceBuilder);
//代码段以生成下一页标记
if (hits[hits.length - 1].getSortValues().length > 0) {
final String esSortFieldToken = Joiner.on(ES_SORT_FIELD_DELIMITER).join(hits[hits.length - 1].getSortValues());
nextPageToken = Joiner.on(NEXT_PAGE_TOKEN_DELIMITER).join(route.getRouteName(), esSortFieldToken);
}
We have a load more button on UI which requests for next pages. We have used SearchAfter query param to implement ES Pagination
I am using sortValues present in ElasticSearch Response to generate nextPageToken.
I found a scenario where even though next page didn't exists, I am getting sort values in Elastic search response and when I queries ES with that sort values it returns empty response.
What should I do to avoid this scenario? Can I determine in advance whether next page exists or not?
Another thing coming into my mind is can we assume if ES response size is less than requested size means we don't have any new pages?
//SearchRequest code snippet
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(query)
.fetchSource(getSessionsQuery.getColumns().toArray(String[]::new), null)
.size(getSessionsQuery.getResultSize())
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(TIMESTAMP_FIELD_KEY)).order(SortOrder.ASC))
.sort(new FieldSortBuilder(route.getTechnologyConfiguration().get(VERSION_SORT_KEY)).order(SortOrder.DESC));
if (!StringUtils.isNullOrEmpty(getSessionsQuery.getNextToken())) {
final String[] tokens = getSessionsQuery.getNextToken().split(ES_SORT_FIELD_DELIMITER);
if (tokens.length > 0) {
sourceBuilder.searchAfter(tokens);
}
}
return new SearchRequest().indices(route.getTechnologyConfiguration().get(INDEX_KEY)).source(sourceBuilder);
//code snippet to generate next page token
if (hits[hits.length - 1].getSortValues().length > 0) {
final String esSortFieldToken = Joiner.on(ES_SORT_FIELD_DELIMITER).join(hits[hits.length - 1].getSortValues());
nextPageToken = Joiner.on(NEXT_PAGE_TOKEN_DELIMITER).join(route.getRouteName(), esSortFieldToken);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确定是否还有另一页结果的一个技巧是从 Elasticsearch 请求比您在页面上显示的结果多的结果(例如,如果您的 UI 的页面大小显示 10 个结果,则从 Elasticsearch 请求 11 个结果)。然后,如果您获得了所请求的结果数量,您将确定还有另一页结果,而无需进行额外的查询。如果您得到的结果数量少于请求的结果数量,您就会知道没有更多结果了。
One trick to figure out whether or not there's another page of results is to request one more result from Elasticsearch than you're displaying on the page (e.g. if your UI has a page size showing 10 results, request 11 results from Elasticsearch). Then if you get the number of results you requested, you'll know for sure that there's another page of results without needing to make an additional query. If you get less than the number of results requested, you'll know there are no more.
是的,当您收到小于
size
(或零)的回击时,您可以假设没有更多页面。Yes, you can assume that there are no more pages when you get less than
size
(or zero) hits back.