我的索引映射如下所示:
curl -X PUT localhost:9200/testing/listings/_mapping -d '{
"listings" : {
"properties" : {
"address" : {
"properties": {
"location": { "type" : "string",
"index" : "not_analyzed"
}
}
},
"suggest" : {
"type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}'
我需要在elasticsearch中执行以下查询的操作
SELECT * FROM LISTINGS WHERE (published = true AND inActive = false) AND (residentialKind = "Villa" OR residentialKind = "Apartment");
为了执行上述查询操作,我在elasticsearch中使用了以下嵌套 bool 查询。
{"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
}
}
它给出以下错误
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[B5SbtdlkQTGL0WU-dvg2yg][propgod][0]: SearchParseException[[propgod][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][1]: SearchParseException[[propgod][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][2]: SearchParseException[[propgod][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][3]: SearchParseException[[propgod][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][4]: SearchParseException[[propgod][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }]",
"status": 400
}
因此,如果我的查询有误,请帮助我修复此查询。提前致谢。
最佳答案
"match"
是查询,而不是过滤器,因此您会收到错误,因为您尝试将其用作过滤器。这应该可以满足您的要求(假设未分析 "residentialKind"
):
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
如果使用标准分析器之类的工具来分析“residentialKind”
,则这应该有效:
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"match":{"residentialKind":"Villa"}},
{"match":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
甚至
POST /test_index/_search
{
"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"term":{"published":true}},
{"term":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"villa"}},
{"term":{"residentialKind":"apartment"}}
]
}
}
]
}
}
}
}
}
这是我用于测试的代码:
http://sense.qbox.io/gist/bbed61ef297b058c92d9c7f1523479dfeb3c35b2
关于mysql - 如何进行嵌套 bool 过滤器以在elasticsearch中的同一字段上查找精确匹配的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28876163/