0

我的索引映射如下所示:

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
}

因此,如果我的查询有误,请帮助我修复此查询。提前致谢。

4

1 回答 1

0

"match"是一个查询,而不是一个过滤器,所以你得到一个错误,因为你试图将它用作过滤器。这应该做你想做的(假设"residentialKind"没有分析):

POST /test_index/_search
{
    "query": {
        "bool":{
            "must":[
               {"match":{"published":true}},
               {"match":{"inActive":false}},
               {"bool":
                    {"should": [
                            {"term":{"residentialKind":"Villa"}},
                            {"term":{"residentialKind":"Apartment"}} 
                        ]
                    }
                }
             ]
         }
    }
}

如果"residentialKind"IS 使用标准分析器之类的工具进行分析,则应该可以:

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

于 2015-03-05T17:35:51.253 回答