当前位置: 首页 > elasticsearch, 搜索 > 正文

ElasticSearch里DSL使用事故一则:_cache参数

1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

大家在写QueryDSL的时候,要特别注意参数,比如今天我就碰到了一个关于_cache的问题 由于使用自己写的QueryBuilder来生成QueryDSL查询语句,所以有些参数虽然没有指定,但是默认带上了,但有些情况下不注意就会出现问题。 如下例:

Url:127.0.0.1:9200/index_search_operate14894cbd-ec15-414f-a405-f329cc370b29/type/_search,
RequestBody:
{
    "from": 0,
    "size": 5,
    "query": {
        "constant_score": {
            "filter": {
                "and": {
                    "filters": [
                        {
                            "numeric_range": {
                                "age": {
                                    "from": "22",
                                    "to": "25",
                                    "include_lower": true,
                                    "include_upper": true
                                }
                            },
                            "_cache": false
                        }
                    ]
                },
                "_cache": false
            },
            "boost": 1
        }
    },
    "explain": false
}
 
 
Response:
 
 
{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][0]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][0]: from[0],size[5]: Parse Failure [Failed to parse source [{\"from\": 0,\"size\": 5,\"query\":{\"constant_score\":{\"filter\":{\"and\":{\"filters\":[{\"numeric_range\":{\"age\":{\"from\":\"22\",\"to\":\"25\",\"include_lower\":true,\"include_upper\":true}},\"_cache\": false}]},\"_cache\": false},\"boost\": 1}},\"explain\": false}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] No filter registered for [filters]]; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][3]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][3]: from[0],size[5]: Parse Failure [Failed to parse source [{\"from\": 0,\"size\": 5,\"query\":{\"constant_score\":{\"filter\":{\"and\":{\"filters\":[{\"numeric_range\":{\"age\":{\"from\":\"22\",\"to\":\"25\",\"include_lower\":true,\"include_upper\":true}},\"_cache\": false}]},\"_cache\": false},\"boost\": 1}},\"explain\": false}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] No filter registered for [filters]]; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][1]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][1]: from[0],size[5]: Parse Failure [Failed to parse source [{\"from\": 0,\"size\": 5,\"query\":{\"constant_score\":{\"filter\":{\"and\":{\"filters\":[{\"numeric_range\":{\"age\":{\"from\":\"22\",\"to\":\"25\",\"include_lower\":true,\"include_upper\":true}},\"_cache\": false}]},\"_cache\": false},\"boost\": 1}},\"explain\": false}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] No filter registered for [filters]]; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][4]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][4]: from[0],size[5]: Parse Failure [Failed to parse source [{\"from\": 0,\"size\": 5,\"query\":{\"constant_score\":{\"filter\":{\"and\":{\"filters\":[{\"numeric_range\":{\"age\":{\"from\":\"22\",\"to\":\"25\",\"include_lower\":true,\"include_upper\":true}},\"_cache\": false}]},\"_cache\": false},\"boost\": 1}},\"explain\": false}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] No filter registered for [filters]]; }]",
    "status": 500
}

报错了,说filters没有注册,显然有些问题了,先按照提示,改了,因为文档上说,是支持不带filters的方式的,很诡异,先试试:

ReqBody:
 
{
    "from": 0,
    "size": 5,
    "query": {
        "constant_score": {
            "filter": {
                "and": [
                    {
                        "numeric_range": {
                            "age": {
                                "from": "22",
                                "to": "25",
                                "include_lower": true,
                                "include_upper": true
                            }
                        },
                        "_cache": false
                    }
                ]
            },
            "boost": 1
        }
    },
    "explain": false
}
 
 
 
 
Response:
 
{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][2]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][2]: from[0],size[5]: Parse Failure [Failed to parse source [{\"from\": 0,\"size\": 5,\"query\":{\"constant_score\":{\"filter\":{\"and\":{\"filters\":[{\"numeric_range\":{\"age\":{\"from\":\"22\",\"to\":\"25\",\"include_lower\":true,\"include_upper\":true}},\"_cache\": false}]},\"_cache\": false},\"boost\": 1}},\"explain\": false}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] No filter registered for [filters]]; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][0]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][0]: from[0],size[5]: Parse Failure [Failed to parse source [{\r\n    \"from\": 0,\r\n    \"size\": 5,\r\n    \"query\": {\r\n        \"constant_score\": {\r\n            \"filter\": {\r\n                \"and\": [\r\n                    {\r\n                        \"numeric_range\": {\r\n                            \"age\": {\r\n                                \"from\": \"22\",\r\n                                \"to\": \"25\",\r\n                                \"include_lower\": true,\r\n                                \"include_upper\": true\r\n                            }\r\n                        },\r\n                        \"_cache\": false\r\n                    }\r\n                ]\r\n            },\r\n            \"boost\": 1\r\n        }\r\n    },\r\n    \"explain\": false\r\n}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] [or] filter requires 'filters' to be set on it']; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][1]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][1]: from[0],size[5]: Parse Failure [Failed to parse source [{\r\n    \"from\": 0,\r\n    \"size\": 5,\r\n    \"query\": {\r\n        \"constant_score\": {\r\n            \"filter\": {\r\n                \"and\": [\r\n                    {\r\n                        \"numeric_range\": {\r\n                            \"age\": {\r\n                                \"from\": \"22\",\r\n                                \"to\": \"25\",\r\n                                \"include_lower\": true,\r\n                                \"include_upper\": true\r\n                            }\r\n                        },\r\n                        \"_cache\": false\r\n                    }\r\n                ]\r\n            },\r\n            \"boost\": 1\r\n        }\r\n    },\r\n    \"explain\": false\r\n}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] [or] filter requires 'filters' to be set on it']; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][3]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][3]: from[0],size[5]: Parse Failure [Failed to parse source [{\r\n    \"from\": 0,\r\n    \"size\": 5,\r\n    \"query\": {\r\n        \"constant_score\": {\r\n            \"filter\": {\r\n                \"and\": [\r\n                    {\r\n                        \"numeric_range\": {\r\n                            \"age\": {\r\n                                \"from\": \"22\",\r\n                                \"to\": \"25\",\r\n                                \"include_lower\": true,\r\n                                \"include_upper\": true\r\n                            }\r\n                        },\r\n                        \"_cache\": false\r\n                    }\r\n                ]\r\n            },\r\n            \"boost\": 1\r\n        }\r\n    },\r\n    \"explain\": false\r\n}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] [or] filter requires 'filters' to be set on it']; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][2]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][2]: from[0],size[5]: Parse Failure [Failed to parse source [{\r\n    \"from\": 0,\r\n    \"size\": 5,\r\n    \"query\": {\r\n        \"constant_score\": {\r\n            \"filter\": {\r\n                \"and\": [\r\n                    {\r\n                        \"numeric_range\": {\r\n                            \"age\": {\r\n                                \"from\": \"22\",\r\n                                \"to\": \"25\",\r\n                                \"include_lower\": true,\r\n                                \"include_upper\": true\r\n                            }\r\n                        },\r\n                        \"_cache\": false\r\n                    }\r\n                ]\r\n            },\r\n            \"boost\": 1\r\n        }\r\n    },\r\n    \"explain\": false\r\n}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] [or] filter requires 'filters' to be set on it']; }{[Nqi7PeirRBiGPKgsDxFT3w][index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][4]: SearchParseException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29][4]: from[0],size[5]: Parse Failure [Failed to parse source [{\r\n    \"from\": 0,\r\n    \"size\": 5,\r\n    \"query\": {\r\n        \"constant_score\": {\r\n            \"filter\": {\r\n                \"and\": [\r\n                    {\r\n                        \"numeric_range\": {\r\n                            \"age\": {\r\n                                \"from\": \"22\",\r\n                                \"to\": \"25\",\r\n                                \"include_lower\": true,\r\n                                \"include_upper\": true\r\n                            }\r\n                        },\r\n                        \"_cache\": false\r\n                    }\r\n                ]\r\n            },\r\n            \"boost\": 1\r\n        }\r\n    },\r\n    \"explain\": false\r\n}]]]; nested: QueryParsingException[[index_search_operate14894cbd-ec15-414f-a405-f329cc370b29] [or] filter requires 'filters' to be set on it']; }]",
    "status": 500
}

还是不行,后来问了下shay,原来numeric_range的_cache参数位置不对,应该放在numeric_range里面,或者去掉就可以了,有时候生成QueryDSL太复杂,检查起来一定要细心,尤其当错误提示还莫名奇怪的时候,一定要先仔细检查QueryDSL本身。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    "from": 0,
    "size": 5,
    "query": {
        "constant_score": {
            "filter": {
                "and": {
                    "filters": [
                        {
                            "numeric_range": {
                                "age": {
                                    "from": "22",
                                    "to": "25",
                                    "include_lower": true,
                                    "include_upper": true
                                },
                            "_cache": false
                            }
                        }
                    ]
                },
                "_cache": false
            },
            "boost": 1
        }
    },
    "explain": false
}

另外,默认参数,如果没有显式的设置,就不生成到queryDSL里面去,不同的查询,默认参数都不一样。

本文固定链接: http://www.chepoo.com/elasticsearch-query-dsl-cache.html | IT技术精华网

ElasticSearch里DSL使用事故一则:_cache参数:等您坐沙发呢!

发表评论