Elasticsearch在Java中使用Scroll API [英] Elasticsearch use Scroll api in Java
问题描述
我尝试在此处使用示例:
> https://www.elastic.co/guide/zh-CN/elasticsearch/client/java-api/current/java-search-scrolling.html
I tried to use the example in here: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
如何在Elasticsearch中使用Java滚动。
这是代码:
on how to use scroll with java in elasticsearch. this is the code:
QueryBuilder qb = termQuery("multi", "test");
SearchResponse scrollResp = client.prepareSearch("test")
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).get(); //max of 100 hits will be returned for each scroll
//Scroll until no hits are returned
do {
for (SearchHit hit : scrollResp.getHits().getHits()) {
//Handle the hit...
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
} while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
尽管由于某些原因,我有一个错误,提示方法prepareSearch(String )对于类型RestHighLevelClient
未定义。
我的 client
变量确实是 RestHighLevelClient
,但在本教程中应该是它。
though for some reasons I have an error which says The method prepareSearch(String) is undefined for the type RestHighLevelClient
.
my client
variable is indeed RestHighLevelClient
but in the tutorial it is what it should be.
主意是什么问题?
推荐答案
RestHighLevelClient的工作原理不同于TransportClient 。
RestHighLevelClient works differently than a TransportClient.
如果要与RestHighLevelClient一起使用滚动,必须遵循以下步骤:
1)创建一个 SearchRequest :
SearchRequest request = new SearchRequest("test").scroll(new TimeValue(60000));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(qb);
searchSourceBuilder.sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
request.source(searchSourceBuilder);
2)执行第一次搜索:
2) Perform the first search:
SearchResponse scrollResp = client.search(sreq);
此处的客户端是RestHighLevelClient。
here client is the RestHighLevelClient.
3)对于后续的滚动搜索,请创建SearchScrollRequest,然后将其用于滚动:
3) For subsequent scroll searches create a SearchScrollRequest and then use it for scroll:
scrollResp = client.searchScroll(new SearchScrollRequest(scrollResponse.getScrollId()).scroll(new TimeValue(60000)));
有关更多信息,请参见:搜索滚动API
For more information refer :Search Scroll API
这篇关于Elasticsearch在Java中使用Scroll API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!