合并两个聚合的结果

须藤会

我有一个 Elasticsearch 索引,其中包含具有以下字段的文档:

  • 作者
  • 贡献者

这些字段中的每一个都可能包含多个用户 ID。

我想执行一个聚合来计算与每个用户(作为作者或贡献者)相关的文档总数。

我可以分别查询每个聚合,但如何组合它们?这是我的查询:

GET documents/_search
{
  "aggs": {
    "contributor": {
      "terms": {
        "field": "contributor"
      }
    },
    "author": {
      "terms": {
        "field": "author"
      }
    }
  }
}

现在,我得到了这个结果:

"aggregations": {
    "author": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
                "key": 2,
                "doc_count": 10
            },
            {
                "key": 1,
                "doc_count": 7
            },
            {
                "key": 5,
                "doc_count": 3
            }
        ]
    },
    "contributor": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
            "key": 5,
            "doc_count": 1
        }]
    }
}

但我想要一个单一的聚合,它返回用户 5 的 4 个文档的计数。

乐大猫

好吧,如果您可以更新您的映射并添加一个字段,这应该可以工作。请不要它可能真的很慢(字符串上的 agg 很慢,不应过度使用)。请注意,如果作者 = 同一文档中的贡献者,则 agg 将不会计算 2 次出现(好消息)。

    {
      "mappings": {
        "test": {
          "properties": {
            "contributor": {
              "type": "keyword",
              "copy_to": "author_and_contributor"
            },
            "author": {
              "type": "keyword",
              "copy_to": "author_and_contributor"
            },
            "author_and_contributor": {
              "type": "string",
              "fielddata": true
            }
          }
        }
      }
}

{
  "size": 0,
  "aggs": {
    "author_contrib_agg": {
      "terms": {
        "field": "author_and_contributor"
      }
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章