首页 > 解决方案 > Elasticsearch 必须在列表对象上

问题描述

我正在使用 Elastic 6.0。我的 kibana 查询如下

  "bool": {
       "must": [
          {
           "match": {
             "relationships.relation": {
               "query": "spouse"
            }
      }
    },
    {
      "match": {
        "relationships.first_name": {
          "query": "james"
        }
      }
    },
    {
      "match": {
        "relationships.last_name": {
          "query": "smith"
        }
      }
    }
  ]
}

这给了我结果

    1.    "relationships": [
        {
          "first_name": "John",
          "last_name": "Heafner",
          "relation": "Father"
        },
        {
          "first_name": "Mary",
          "last_name": "Dellinger",
          "relation": "Mother"
        },
        {
          "first_name": "James S.",
          "last_name": "Smith",
          "relation": "Spouse"
        }
      ]

    2. "relationships": [
        {
          "first_name": "James",
          "last_name": "Hickey",
          "relation": "Father"
        },
        {
          "first_name": "Mary",
          "last_name": "Raftery",
          "relation": "Mother"
        },
        {
          "first_name": "Richard",
          "last_name": "Smith",
          "relation": "Spouse"
        }
      ]

我只想要第一个结果,因为它包含基于我的查询的完全匹配
{ "first_name": "James S.", "last_name": "Smith", "relation": "Spouse" } 我已经尝试过术语查询,但它给出了结果与上述相同。任何建议都会有很大帮助。

标签: elasticsearchnestelasticsearch-5elasticsearch-net

解决方案


这听起来像您已relationships映射为objecttype。为此,relationships需要将其映射为一种nested类型,并且您应该使用nested查询来查询嵌套对象的字段。


推荐阅读