ElasticSearch聚合将额外字段作为数组添加到存储桶

时间:2017-05-15 09:25:18

标签: arrays elasticsearch aggregate

我在弹性搜索的聚合方面很新。

我正在寻找一种方法来为聚合的每个桶添加自定义字段,这个字段应该是一个数组或字符串,每个特定项目之间都有一些分隔符。

我有弹性搜索映射映射

{
    "mappings": {
        "place": {
            "properties": {
                "name": {
                    "type": "string",
                }
            }
        }
    }
}

我有一个汇总查询

  {
  size: 0,
  query: {...},
  aggs: {
    "merchants": {
      "terms": {
        "field": "name",
        "min_doc_count": 1,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "script": "_score"
          }
        }
      }
    }
  }
}

我有这样的结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 82,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "merchants": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Post Office",
                    "doc_count": 82,
                    "max_score": {
                        "value": 1.7627471685409546
                    }
                }
            ]
        }
    }
}

在该示例中,此结果包含82个文档。 我的目标是每个存储桶都有一个额外的字段,其中包含每个文档_id,更好的是数组,例如“refs”:[1,2,3,4,...]

1 个答案:

答案 0 :(得分:0)

实现此目标的最佳方法是在terms字段上添加另一个_id子聚合:

{
  size: 0,
  query: {...},
  aggs: {
    "merchants": {
      "terms": {
        "field": "name",
        "min_doc_count": 1,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "ids": {               <--- add this
          "terms": {
            "field": "_id",
            "size": 100
          }
        },
        "max_score": {
          "max": {
            "script": "_score"
          }
        }
      }
    }
  }
}