JAVA滚动查询es

通过设置的查询条数,滚动查询es,直到查出所有结果

  1. 首先定义
private static TimeValue timeValue = new TimeValue(1000000);
  1. 查询条件
QueryBuilders.disMaxQuery().add(QueryBuilders.termQuery("", ""));
  1. 查询代码
SearchRequestBuilder setQuery = esTransportClient.prepareSearch("索引名")
				.setTypes("索引类型")
				.setQuery(must)
				.setFrom(0)
				.setSize(10000)
				.setScroll(timeValue)
				;
		SearchResponse actionGet = setQuery.execute().actionGet();
		List> list = new ArrayList>();
		while (true){
			SearchHits hits = actionGet.getHits();
			SearchHit[] hits2 = hits.getHits();
			if (hits2.length < 1){
                break;
            }
			for (SearchHit searchHit : hits2) {
				Map map = searchHit.getSourceAsMap();
				List fullTextSeven = getFullTextSeven(map);
				list.add(fullTextSeven);
			}
			String scrollID = actionGet.getScrollId();
			actionGet = esTransportClient.prepareSearchScroll(scrollID)
	                .setScroll(timeValue).get();
		}
		System.out.println(list.size());

你可能感兴趣的:(java,java,es,滚动查询)