【问题标题】:Elasticsearch: Query for document containing two key-value pairs in an arrayElasticsearch:查询包含数组中两个键值对的文档
【发布时间】:2015-08-03 17:18:43
【问题描述】:

如何查询包含键值对“key1”-“value1”和“key2”-“value2”的文档?我似乎找不到任何关于此的文档。

我尝试了下面的查询,但即使应该有匹配的文档,它也没有返回任何结果。虽然用 should 替换 must 是可行的,但是当我输入 minimum_should_match = 100% 时,它也不会返回任何结果。

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "must": [
                  [
                    {
                      "match_phrase": {
                        "attributes.key": "key1"
                      }
                    },
                    {
                      "match_phrase": {
                        "attributes.value": "value1"
                      }
                    }
                  ],
                  [
                    {
                      "match_phrase": {
                        "attributes.key": "key2"
                      }
                    },
                    {
                      "match_phrase": {
                        "attributes.value": "value2"
                      }
                    }
                  ]
                ]
              }
            }
          }
        },
        [
          {
            "match_all": {

            }
          }
        ]
      ]
    }
  }
}

这是映射的样子:

{
    "index_name": {
        "mappings": {
            "type_name": {
                "properties": {
                    "attributes": {
                        "type": "nested",
                        "properties": {
                            "key": {
                                "type": "string",
                                "analyzer": "flat"
                            },
                            "value": {
                                "type": "string",
                                "analyzer": "flat"
                            }
                        }
                    }
                }
            },
        }
    }
}

非常感谢您的帮助。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    试试这个:

    POST /test_index/_search
    {
       "query": {
          "filtered": {
             "query": {
                "match_all": {}
             },
             "filter": {
                "bool": {
                   "must": [
                      {
                         "nested": {
                            "path": "attributes",
                            "query": {
                               "bool": {
                                  "must": [
                                     {
                                        "term": {
                                           "attributes.key": "key1"
                                        }
                                     },
                                     {
                                        "term": {
                                           "attributes.value": "value1"
                                        }
                                     }
                                  ]
                               }
                            }
                         }
                      },
                      {
                         "nested": {
                            "path": "attributes",
                            "query": {
                               "bool": {
                                  "must": [
                                     {
                                        "term": {
                                           "attributes.key": "key2"
                                        }
                                     },
                                     {
                                        "term": {
                                           "attributes.value": "value2"
                                        }
                                     }
                                  ]
                               }
                            }
                         }
                      }
                   ]
                }
             }
          }
       }
    }
    

    这是我用来测试它的代码(我只使用了standard 分析器,因为我不知道你的flat 分析器是什么样的;所以你可能需要对此进行调整):

    http://sense.qbox.io/gist/13b7e2aa4d90bfb2f82787c6a00494ee3343e013

    【讨论】:

      【解决方案2】:

      我在使用 Elastic 5 时遇到了同样的问题,我不得不稍微调整推荐的答案以使其正常工作,因为过滤的查询已被弃用。 (没有收到为 [filtered] 注册的 [query])。

      使用 Elastic 5,以下结构为我解决了这个问题:

      {
          "query" : {
              "bool" : {
                  "must" : [
                      {
                          "nested" : {
                              "path": "attributes",
                              "query" : {
                                  "bool" : {
                                      "must" : [
                                          { "term" : { "attributes.key" : "key1" } },
                                          { "term" : { "attributes.value" : "value1" } }
                                      ]
                                  }
                              }
                          }
                      },
                      {
                          "nested" : {
                              "path" : "attributes",
                              "query" : {
                                  "bool" : {
                                      "must" : [
                                          { "term" : { "attributes.key" : "key2" } },
                                          { "term" : { "attributes.value" : "value2" } }
                                      ]
                                  }
                              }
                          }
                      }
                  ]
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        相关资源
        最近更新 更多