Kibana 如何在elasticsearch中按分组字段计数排序?

kokeuurv  于 12个月前  发布在  Kibana
关注(0)|答案(1)|浏览(319)

我的数据对象包含3个字符串字段:节,类别和用户名。我想做的类别和用户名分组,找出前20名的“节”,并在我的分组计数没有
基本上,我想要的结果是像SQL查询:选择部分,计数(*)从表组按类别,用户名顺序由2 desc限制20
我试着做部分聚合,类别和用户名.结果是我正在寻找的分组,但它没有排序“节”字段通过这个分组(例如,我需要第一个结果包含出现在类别和用户名及其计数的最组合的部分)
相反,它只按doc_count对节进行排序,在每个节中,它按doc_count对“category”进行排序,在每个节中,它按doc_count对“username”进行排序
这就是我所尝试的:

GET some_index/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "some_filter_i_needed": "filter_value"
          }
        }
      ]
    }
  },
  "aggs": {
    "top_section": {
      "terms": {
        "field": "section.keyword",  
        "size": 20,  
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "top_category": {
          "terms": {
            "field": "category.keyword",
            "size": 1,
            "order": {
              "_count": "desc"
            }
          },
          "aggs": {
            "top_username": {
              "terms": {
                "field": "username.keyword",
                "size": 1 ,
                "order": {
                  "_count": "desc"
                }
              }
            }
          }
        }
      }
    }
  }
}

字符串
有什么方法可以得到我想要的输出吗?
谢谢

l3zydbqr

l3zydbqr1#

根据我对你的问题的理解,你需要先按“category”和“username”分组,然后在每个组中找到最上面的“section”。你提供的查询是相反的,它是先按“section”分组,然后是“category”,然后是“username”。
请查看以下示例:

POST some_index/_bulk
{ "index" : { "_index" : "some_index", "_id" : "1" } }
{ "section" : "section1", "category" : "category1", "username" : "username1" }
{ "index" : { "_index" : "some_index", "_id" : "2" } }
{ "section" : "section2", "category" : "category1", "username" : "username1" }
{ "index" : { "_index" : "some_index", "_id" : "3" } }
{ "section" : "section1", "category" : "category2", "username" : "username2" }
{ "index" : { "_index" : "some_index", "_id" : "4" } }
{ "section" : "section2", "category" : "category2", "username" : "username2" }

个字符
x1c 0d1x的数据

update-1

GET some_index/_search
{
  "size": 0,
  "aggs": {
    "sections": {
      "terms": {
        "field": "section.keyword",
        "size": 10
      },
      "aggs": {
        "category_user_combinations": {
          "terms": {
            "script": {
              "source": "doc['category.keyword'].value + ' ' + doc['username.keyword'].value",
              "lang": "painless"
            },
            "size": 20,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}


相关问题