search - 在ElasticSearch数组中查找多值元素

标签 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中找到实现此目的的方法

最佳答案

由于我不知道您使用的是哪个Elasticsearch版本,所以我指的是当前版本。

您正在查看以下内容:

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

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

https://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
(点击运行)

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

关于search - 在ElasticSearch数组中查找多值元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979125/

相关文章:

java - org.elasticsearch.client.transport.NoNodeAvailableException : None of the configured nodes are available: []

elasticsearch - Elasticsearch批处理大小限制

elasticsearch - 弹性架构错误 - 根映射定义具有不受支持的参数

json - JSON elasticsearch解析

elasticsearch bool 查询结合必须与或

javascript - 如何使用 javascript 检查输入是否包含 isbn

search - Elasticsearch定制词干算法

javascript - JS从多维数组中获取包含值的数组索引

ios - 键入时搜索大型 Core-Data 数据集

mysql - MyIsam 针对多个 %term% 进行全文搜索