mysql - 如何进行嵌套 bool 过滤器以在elasticsearch中的同一字段上查找精确匹配的值?

标签 mysql elasticsearch exact-match subquery sql-match-all

我的索引映射如下所示:

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/

相关文章:

mysql 创建 View 列为 utf8_bin 而不是 utf8_general_ci

arrays - ElasticSearch:嵌套数组与单独类型

elasticsearch - 带有子文档计数的Elasticsearch分数

elasticsearch - elasticsearch,在字段中查找确切短语

bash - grep 固定字符串不是 grep 精确的字符串,带有额外字符 '-' 的字符串结尾也即将到来

C# 在字符串中查找精确匹配

mysql - 在数据库中搜索/替换变量文本

mysql - 为什么枚举字段在 PhpStorm 中显示为 char

php - sphinx 中的排序结果?

java - spring-data-elasticsearch 基于注释的审计?