Result window is too large, from + size must be less than or equal to: [10000] but was

ES其默认只能查询前一万条记录,当from + size > 1w时,就会抛出如下异常

Caused by: org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [51875]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.]
	at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:496)
	at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:407)
	at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:437)
	at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:603)
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:169)
	... 40 common frames omitted

解决方案:

1、调用Elasticsearch的接口,增大max_result_window的值
PUT index/_settings
{
  "index":{
    "max_result_window":10000000
  }
}

2、创建索引时,新增max_result_window的配置
"settings": {
	"index.max_result_window": 100000000
}


遗留问题:

修改了max_result_window之后,其结果中的totalHits还是1w,并不是实际的总记录数。
具体情况如下所示:
GET  index/_search
{
  "query": {
    "match_all": {
      
    }
  }
}

// 返回体
{
  "took" : 13,
  "hits" : {
    "total" : {
      "value" : 10000,		//索引中其实并不止10000条数据
      "relation" : "gte"
    }
  }
  ...
}

解决方案:

1、RestHighLevelClient在构建查询体时添加如下代码
searchSourceBuilder.trackTotalHits(true);

2、Restul Api 调用时添加track_total_hits的设置
GET  index/_search
{
  "query": {
    "match_all": {
      
    }
  },
  "track_total_hits": true
}

你可能感兴趣的:(ES,java,elasticsearch,es,spring)