Elasticsearch-PHP 遇到的坑

1,872 阅读1分钟
原文链接: www.mervyntang.com

Elasticsearch-PHP 遇到的坑

大数据量分页查询报错

问题详情

在用elasticsearch-php分页查询时,分页几次后报错,错误内容如下:

{
    "error":{
        "root_cause":[
            {
                "type":"query_phase_execution_exception",
                "reason":"Result window is too large, from + size must be less than or equal to: [10000] but was [15000]. 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."
            }
        ],
        "type":"search_phase_execution_exception",
        "reason":"all shards failed",
        "phase":"query",
        "grouped":true,
        "failed_shards":[
            {
                "shard":0,
                "index":"es_type",
                "node":"OVgyFsCDsfqwnvXz0nqA",
                "reason":{
                    "type":"query_phase_execution_exception",
                    "reason":"Result window is too large, from + size must be less than or equal to: [10000] but was [15000]. 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."
                }
            }
        ]
    },
    "status":500
}

解决方法

Elasticsearch 默认是有查询上限的, from + size 的值必须要小于等于 10000, 需要修改对应参数 max_result_window 的值。

PUT http://127.0.0.1:9200/es_type/_settings -d '{ "index" : { "max_result_window" : 100000000}}'

curl 进行 PUT 或 POST 时报错

问题详情

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

解决方法

elasticsearch6.x 后需要在执行 curl 命令时加入 header 头

curl -H "Content-Type: application/json"  -XPUT http://127.0.0.1:9200/es_type/_settings -d '
{
    "index" : { "max_result_window" : 100000000}
}'