【问题标题】:sub field aggregation group by order by in elasticsearch在elasticsearch中按顺序进行子字段聚合组
【发布时间】:2019-07-09 03:31:36
【问题描述】:

我无法找到正确的语法来获取按计数字段排序的子对象的聚合。

Twitter 文档就是一个很好的例子:

{
  "properties" : {
    "id" : {
      "type" : "long"
    },
    "message" : {
      "type" : "string"
    },
    "user" : {
      "type" : "object",
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "screenName" : {
          "type" : "string"
        },
        "followers" : {
          "type" : "long"
        }
      }
    }
  }
}

我将如何获得一组给定推文的顶级影响者?这将是按“user.followers”字段排序的前 10 个“用户”对象的唯一列表。

我尝试过使用 top_hits 但遇到异常:

org.elasticsearch.common.breaker.CircuitBreakingException:[FIELDDATA] 数据太大,[user.id] 的数据

"aggs": {
    "top-influencers": {
      "terms": {
        "field": "user.id",

        "order": {
          "top_hit": "desc"
        }
      },
      "aggs": {
        "top_tags_hits": {
          "top_hits": {}
        },
        "top_hit": {
          "max": {
            "field": "user.followers"
          }
        }
      }
    }
  }

我几乎可以使用查询中的“排序”字段(无聚合)得到我想要的,但是如果用户有多个推文,那么它们将在结果中出现两次。我需要能够按子对象“用户”进行分组,并且每个用户只返回一次。

---更新---

我已经设法获得了一份及时返回的顶级用户列表。不幸的是,它仍然不是独一无二的。文档还说 top_hits 被设计为子 agg...,我将其用作顶级 agg...

"aggs": {
    "top_influencers": {
      "top_hits": {
        "sort": [
          {
            "user.followers": {
              "order": "desc"
            }
          }
        ],
        "_source": {
          "include": [
            "user.id",
            "user.screenName",
            "user.followers"
          ]
        },
        "size": 10
      }
    }
  }

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    试试这个:

    {
        "aggs": {
            "GroupByType": {
                "terms": {
                    "field": "user.id",
                    "size": 10000
                },
                "aggs": {
                    "Group": {
                        "top_hits":{
                            "size":1, 
                            "_source": {
                                    "includes": ["user.id", "user.screenName", "user.followers"]
                            },
                            "sort":[{
                                "user.followers": {
                                    "order": "desc"
                                }
                            }]
    
                         }
                    }
                }
            }
        }
    }
    

    然后您可以获取此查询的前 10 个结果。请注意,弹性搜索中的普通搜索最多只能搜索 10000 条记录。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2021-01-30
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2020-11-14
      • 2016-03-20
      相关资源
      最近更新 更多