使用Java API从Elasticsearch获取所有记录

我试图使用Java API从Elasticsearch获取所有记录。 但我收到以下错误

n [[Wild Thing] [localhost:9300] [indices:data / read / search [phase / dfs]]]; 嵌套:QueryPhaseExecutionException [结果窗口太大,+大小必须小于或等于:[10000]但是[10101]。

我的代码如下

Client client; try { client = TransportClient.builder().build(). addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); int from = 1; int to = 100; while (from  0) { for (SearchHit searchData : response.getHits().getHits()) { JSONObject value = new JSONObject(searchData.getSource()); System.out.println(value.toString()); } } } } 

当前存在的记录总数为131881,因此我from = 1开始to = 100 ,然后from <= 131881获得100条记录。 有没有办法在Elasticsearch中没有其他记录,我可以检查以100的集合获取记录。

是的,您可以使用Java客户端也支持的滚动API来执行此操作。

你可以这样做:

 Client client; try { client = TransportClient.builder().build(). addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); QueryBuilder qb = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("user_agent", "")); SearchResponse scrollResp = client.prepareSearch("demo_risk_data") .addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC) .setScroll(new TimeValue(60000)) .setQuery(qb) .setSize(100).execute().actionGet(); //Scroll until no hits are returned while (true) { //Break condition: No hits are returned if (scrollResp.getHits().getHits().length == 0) { break; } // otherwise read results for (SearchHit hit : scrollResp.getHits().getHits()) { JSONObject value = new JSONObject(searchData.getSource()); System.out.println(value.toString()); } // prepare next query scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet(); } }