术语聚合仅考虑前缀进行聚合 [英] Term aggregation consider only the prefix to aggregate

查看:231
本文介绍了术语聚合仅考虑前缀进行聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的弹性搜索文档中,我有一些用户和他在组织中的地位,例如:
CEO是职位1
直接在CEO下面的人将是1/1 ,1/2,1/3等等
1/1以下的1/1/1,1/1/2,1/2/3等等



我有一个聚合,我想汇总由副总裁,所以我想要所有人在1/1,1/2,1/3。



为此,我创建了一个这样的查询:

 aggs:{
information :{
terms:{
field:position,
script:_value.replaceAll('(1/1 / [0 /] * [1- 9])。+','$ 1')
}

前缀和正则表达式中的组替换,所以每个人都将拥有相同的位置,然后我可以进行聚合。这个表现不佳。



我正在考虑使用这样的东西

 aggs:{
information:{
terms:{
field:position,
prefix:1/1 /.*'
}

所以我会把所有人以1/1开头(1/1/1/1,1/1/1/2,1/1/1/3为1组,1/1/2/1,1 / 1 / 2,1 / 1/2 / 3将是第二组,等等)。



可以吗?

解决方案

如果事先知道您希望运行此聚合的级别,可以将这些级别存储在不同的字段中:

  {
name:Jack,
own_level:4,
level_1:1,
level_2 3,
level_3:2,
level_4:null
}

但是这需要许多嵌套术语聚合ns来重现层次结构。这个版本会使一个这样的集合足够:

  {
name:Jack,
own_level:4,
level_1:1,
level_2:1/3,
level_3:1/3/2,
level_4:null
}

如果您想要更简单的查询过滤器通过在字段 level_2 上的过滤器和字段上的术语聚合来 1/1 c $ c> level_3 。



如果您不知道层次结构的最大级别,可以使用这样的嵌套文档,但是查询和汇总有点复杂:

  {
name:Jack,
own_level:4,
bosses:[
{
level:1,
id:1
},
{
level:2,
id:1/3
},
{
level:3,
id:1 / 3/2
}
]
}


In my elastic search documents I have users and some sort of representation of his place in the organization, for instance: The CEO is position 1 The ones directly under the CEO will be 1/1, 1/2, 1/3, and so on The ones under 1/1 will be 1/1/1, 1/1/2, 1/2/3, etc

I have an aggregration in which I want to aggregate by VP, so I want everybody under 1/1, 1/2, 1/3.

To do that I created a query like this one:

"aggs": {
            "information": {
                "terms":{
                    "field": "position",
                    "script": "_value.replaceAll('(1/1/[0/]*[1-9]).+', '$1')"
                } 

This would get the prefix and replace by the group in the regex, so everyone would have the same position, then I could make the aggregation. This has a poor performance.

I was thinking about using something like this

"aggs": {
            "information": {
                "terms":{
                    "field": "position",
                    "prefix": "1/1/.*'
                } 

So I would group by everyone that starts with 1/1 (1/1/1/1, 1/1/1/2, 1/1/1/3 would be one group, 1/1/2/1, 1/1/2/2, 1/1/2/3 would be a second group and so on).

Is it possible?

解决方案

If you know beforehand that on how deep level you want to run this aggregation, you could simply store these levels at different fields:

{
    "name": "Jack",
    "own_level": 4,
    "level_1": "1",
    "level_2": "3",
    "level_3": "2",
    "level_4": null
}

But this would require many nested terms aggregations to reproduce the hierarchy. This version would make one such aggregation sufficient:

{
    "name": "Jack",
    "own_level": 4,
    "level_1": "1",
    "level_2": "1/3",
    "level_3": "1/3/2",
    "level_4": null
}

It also has simpler query filter if you want to focus on people under for example 1/1 by having a filter on field level_2 and terms aggregation on field level_3.

If you don't know the maximum level of the hierarchy you can use nested documents like this, but then queries and aggregations get a bit more complex:

{
    "name": "Jack",
    "own_level": 4,
    "bosses": [
        {
            "level": 1,
            "id": "1"
        },
        {
            "level": 2,
            "id": "1/3"
        },
        {
            "level": 3,
            "id": "1/3/2"
        }
    ]
}

这篇关于术语聚合仅考虑前缀进行聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆