如何使用JestClient处理切片滚动搜索?

时间:2018-12-06 04:54:35

标签: java elasticsearch

我想使用JestClient进行并行搜索,但我不知道如何使用API​​。我从github上搜索了问题:https://github.com/searchbox-io/Jest/issues?page=1&q=is%3Aissue+is%3Aopen,但其中未包含任何主题。 这是我正在使用的版本:

<dependency>
  <groupId>io.searchbox</groupId>
  <artifactId>jest</artifactId>
  <version>5.3.3</version>
</dependency>

我可以像这样使用滚动搜索:

@Test
public void testSerialScroll() throws IOException {
    BoolQueryBuilder builder = QueryBuilders.boolQuery();

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(SCROLL_SEARCH_SIZE);
    searchSourceBuilder.query(builder);

    Search search = new Search
        .Builder(searchSourceBuilder.toString())
        .addIndex(indexName)
        .addType(typeName)
        .addSourceIncludePattern("open_id")
        .setParameter(Parameters.SCROLL, "10m")
        .build();

    int sum = 0, loop = 0;
    JestResult result = jestClient.execute(search);
    while(result.isSucceeded()
            && result.getSourceAsObjectList(Map.class).size() > 0) {
        List<Map> record = result.getSourceAsObjectList(Map.class);
        sum += record.size();
        System.out.println("currentLoopCount:" + (++loop) + ", queryResult:" + record.size() + ", total:" + sum);
        String scrollId = result.getJsonObject().get("_scroll_id").getAsString();
        SearchScroll scroll = new SearchScroll.Builder(scrollId, "10m").build();
        result = jestClient.execute(scroll);
    }
}

0 个答案:

没有答案