简体   繁体   English

如何使用 Elasticsearch 查询嵌套字段的所有值

[英]How to query all values of a nested field with Elasticsearch

I would like to query a value in all data packages I have in Elasticsearch.我想在 Elasticsearch 中的所有数据包中查询一个值。

For example, I have the code :例如,我有代码:

  "website" : "google",
  "color" : [
    {
      "color1" : "red",
      "color2" :  "blue"
    }
  ]
} 

I have this code for an undefined number of website.我有一个未定义数量的网站的代码。 I want to extract all the "color1" for all the websites I have.我想为我拥有的所有网站提取所有“color1”。 How can I do ?我能怎么做 ? I tried with match_all and "size" : 0 but it didn't work.我尝试使用 match_all 和 "size" : 0 但它没有用。

Thanks a lot !非常感谢 !

To be able to query nested object you would need to map them as a nested field first then you can query nested field like this:为了能够查询嵌套对象,您需要先将它们映射为嵌套字段,然后您可以像这样查询嵌套字段:

GET //my-index-000001/_search
{
  "aggs": {
    "test": {
      "nested": {
        "path": "color"
      },
      "aggs": {
        "test2": {
          "terms": {
            "field": "color.color1"
          }
        }
      }
    }
  }
}

Your result should look like this for the query:您的查询结果应如下所示:

"aggregations": {
    "test": {
        "doc_count": 5,
        "test2": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "red",
                    "doc_count": 4
                },
                {
                    "key": "gray",
                    "doc_count": 1
                }
            ]
        }
    }
}

if you check the aggregation result back you will have list of your color1 with number of time it appeared in your documents.如果您检查聚合结果,您将获得color1列表以及它在文档中出现的次数。

For more information you can check Elasticsearch official documentation about Nested Field here and Nested aggregation here .有关更多信息,您可以在此处查看有关嵌套字段的 Elasticsearch 官方文档和此处的嵌套聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM