如何在elasticsearch中对数组字段进行分组聚合后对数组字段执行并集操作

bnlyeluc  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(744)

我在索引中有下列文件 products ```
{ "product_name": "prod-1", "meta": [ { "tag": "tag1", "score": "12" }, { "tag": "tag2", "score": "24" } ] }
{ "product_name": "prod-2", "meta": [ { "tag": "tag1", "score": "36" } ] }
{ "product_name": "prod-2", "meta": [ { "tag": "tag2", "score": "44" } ] }
{ "product_name": "prod-3", "meta": [ { "tag": "tag3", "score": "54" } ] }

我知道如何分组 `product_name` 在es中

POST /products/_search
{
"size": 0,
"aggs": {
"by_product": {
"terms": {
"field": "product_name"
}
}
}
}

按分组后 `product_name` ,我希望每个bucket中都有一个名为meta的字段,该字段包含来自该bucket中所有文档的meta的并集,如下所示

[
{
"key": "prod-1",
"meta": [{ "tag": "tag1", "score": "12" }, { "tag": "tag2", "score": "24" }]
},
{
"key": "prod-2",
"meta": [{ "tag": "tag1", "score": "36" }, { "tag": "tag2", "score": "44" }]
},
{
"key": "prod-3",
"meta": [ { "tag": "tag3", "score": "54" } ]
}
]

如何在elaticsearch中实现这一点?
sg2wtvxw

sg2wtvxw1#

显示预期搜索结果的最佳方法是使用top hits聚合,您可以使用它向术语聚合添加其他字段
搜索查询:

{
  "size": 0,
  "aggs": {
    "by_product": {
      "terms": {
        "field": "product_name.keyword"
      },
      "aggs": {
        "top_sales_hits": {
          "top_hits": {
            "_source": {
              "includes": [
                "meta.tag",
                "meta.score"
              ]
            }
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "by_product": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "prod-2",
          "doc_count": 2,
          "top_sales_hits": {
            "hits": {
              "total": {
                "value": 2,
                "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                {
                  "_index": "64801386",
                  "_type": "_doc",
                  "_id": "2",
                  "_score": 1.0,
                  "_source": {
                    "meta": [
                      {
                        "score": "36",
                        "tag": "tag1"
                      }
                    ]
                  }
                },
                {
                  "_index": "64801386",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 1.0,
                  "_source": {
                    "meta": [
                      {
                        "score": "44",
                        "tag": "tag2"
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        {
          "key": "prod-1",
          "doc_count": 1,
          "top_sales_hits": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                {
                  "_index": "64801386",
                  "_type": "_doc",
                  "_id": "1",
                  "_score": 1.0,
                  "_source": {
                    "meta": [
                      {
                        "score": "12",
                        "tag": "tag1"
                      },
                      {
                        "score": "24",
                        "tag": "tag2"
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        {
          "key": "prod-3",
          "doc_count": 1,
          "top_sales_hits": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                {
                  "_index": "64801386",
                  "_type": "_doc",
                  "_id": "4",
                  "_score": 1.0,
                  "_source": {
                    "meta": [
                      {
                        "score": "54",
                        "tag": "tag3"
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      ]
    }

相关问题