在ElasticSearch Array中查找多值元素

时间:2016-08-16 15:40:17

标签: search elasticsearch

我们有一个用户索引,例如

{
   "name": "Eli",
   "cars": [
        { "model": "Honda", "color": "Red" },
        { "model": "Honda", "color": "Blue" },
        { "model": "Toyota", "color": "Red" }
   ]
}

{
   "name": "Don",
   "cars": [
        { "model": "Honda", "color": "Blue" },
        { "model": "Honda", "color": "Black" },
        { "model": "Toyota", "color": "Red" }
   ]
}

我们正在尝试使用Red Honda检索所有用户,但我们在ElasticSearch中无法找到方法

1 个答案:

答案 0 :(得分:1)

由于我不知道你使用的是哪个弹性搜索版本,我正在引用当前版本。

您正在查看以下内容:

https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.htmlhttps://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.htmlhttps://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html

使用嵌套映射,您可以创建如下所示的查询:

{
 "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "cars",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "cars.model": "honda"
                    }
                  },
                  {
                    "term": {
                      "cars.color": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

链接到示例:https://www.found.no/play/gist/91c5a6c8c9fe81928b1cc497f8740a3f (点击运行)

请注意,这仅在您使用嵌套对象时才有效!映射必须知道这一点。