【问题标题】:Elasticsearch include other fields in top level aggregationElasticsearch 在顶级聚合中包含其他字段
【发布时间】:2019-03-01 19:37:43
【问题描述】:

我的索引文档如下:

{
  "user": { 
     "email": "test@test.com",
     "firstName": "test",
     "lastName": "test" 
  },
  ...
  "category": "test_category"
}

目前我有一个聚合,它通过用户的电子邮件来计算文档,然后是一个子聚合来计算每个用户的类别:

"aggs": {
  "users": {
    "terms": {
      "field": "user.email",
      "order": {
        "_count": "desc"
      }
    },
    "aggs": {
      "categories": {
        "terms": {
          "field": "category",
          "order": {
            "_count": "desc"
          }
        }
      }
    }
  }
}

我正在尝试将用户的名字和姓氏包含到由顶级聚合生成的存储桶中,同时仍然从类别子聚合中获得相同的结果。我已经尝试包含 top_hits 聚合,但我没有任何运气得到我想要的结果。

有什么建议吗?谢谢!

编辑:

让我换个说法。实际上,我确实通过 top_hits 聚合获得了用户数据方面的预期结果,我只是不知道如何正确地将其包含在我的原始聚合中,以便 categories 子聚合仍然给我相同的结果。我尝试了以下top_hits 聚合:

"aggs": {
  "user": {
    "top_hits": {
      "size": 1,
      "_source": {
        "include": ["user"]
      }
    }
  }
}

我希望将用户数据放在顶级 agg 存储桶中,然后仍然在其下方按类别进行聚合。

【问题讨论】:

  • 你能贴出你试过的top_hist吗?你用过:_source: {include: ['users']} ?
  • 编辑了我的帖子。
  • 试试这个: aggs: { users: { terms: {field: 'user.email'}, aggs: { user: { top_hits: { _source: {include: ['user' ],大小 = 1 } } } } }

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

如果我是对的,用户和名字姓氏有一个双射。

因此您可以在这些字段上使用自定义脚本检索它们(并在客户端使用“_”或 wathever 分隔符提取这些存储桶值)

aggs: {
  users: {
    terms: {
        script: 'doc["users.email"].value + "_" + doc["users.firstName"].value + "_" + doc["users.lastName"].value'
    }
  }
}

【讨论】:

  • 感谢您的建议,但在我的示例中似乎不起作用。我使用了这个聚合而不是我自己的用户聚合。
  • 没关系,现在可以使用了。似乎给了我想要的结果。谢谢!
猜你喜欢
  • 2017-11-04
  • 2016-01-22
  • 1970-01-01
  • 2016-10-22
  • 2020-02-18
  • 2017-04-13
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多