【问题标题】:ElasticSearch aggregation add extra field as array to bucketsElasticSearch 聚合将额外字段作为数组添加到存储桶
【发布时间】:2017-05-15 17:30:28
【问题描述】:

我是弹性搜索聚合的新手。

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

我有弹性搜索映射映射

{
    "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, ...]

【问题讨论】:

    标签: arrays elasticsearch aggregate


    【解决方案1】:

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

    {
      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"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我能以某种方式省略“size”:100吗?因为我不知道确切的值,应该在那里
    • 你可以设置一个任意高的值,但默认只返回10个词。
    猜你喜欢
    • 2017-09-22
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多