簡體   English   中英

具有范圍的彈性搜索復合分組

[英]Elastic Search composite grouping with range

考慮以下文檔在我的彈性搜索中。 我想根據排名對文檔進行分組,但是任何低於 1000 的排名都必須單獨顯示,並且必須對高於 1000 的任何排名進行分組我如何使用復合聚合來實現這一點,我是新手,我正在使用復合,因為我想使用 after允許分頁的關鍵功能。

Documents 

    {
        rank : 200,
        name:abcd,
        score1 :100,
        score2:200
    },
    {
        rank 300,
        name:abcd,
        score1:100,
        score2:200
    }
Expected Result:
{
   key:{
    rank:101
   },
   doc_count:1,
   _score1: {value:3123}
   _score2 : {value :3323}
}
{
   key:{
    rank:1000-*
   },
   doc_count:1,
   _score1: {value:3123}
   _score2 : {value :3323}
},
   {
   key:{
    rank:300
   },
   doc_count:1,
   _score1: {value:3123}
   _score2 : {value :3323}
}

######## QUery that I tried

{
    "query":{"match_all":{}},
    "aggs":{
        "_scores":{
            "composite"{
                "sources":[
                    {"_rank":{"terms":{"field":"rank"}}}
                ]
            }
        },
        "aggs":{
            "_ranks":{
                "field":"rank:[
                    {"to":1000},
                    {"from":1000}
                ]
            }
            "_score1": {"sum": {"field": "score1"}}
            "_score2": {"sum": {"field": "score2"}}
        }
    }
}

據我了解,你想

  • 將值低於 1000 等級的聚合分組到它們自己的桶中
  • 將值為 1000 及以上的聚合分組到一個鍵為1000-*桶中
  • 並且對於每個桶,計算所有桶的_score1的總和
  • 同理計算所有bucket的_score2之和

對於這種情況,您可以簡單地使用我在下面的答案中提到的術語聚合

我已經提到了示例映射、示例文檔、查詢和響應,這樣您就可以清楚地了解正在發生的事情。

映射:

PUT my_sample_index
{
  "mappings": {
    "properties": {
      "rank":{
        "type": "integer"
      },
      "name":{
        "type": "keyword"
      },
      "_score1": {
        "type":"integer"
      },
      "_score2":{
        "type": "integer"
      }
    }
  }
}

示例文件:

POST my_sample_index/_doc/1
{
  "rank": 100,
  "name": "john",
  "_score1": 100,
  "_score2": 100
}

POST my_sample_index/_doc/2
{
  "rank": 1001,                        <--- Rank  > 1000
  "name": "constantine",
  "_score1": 200,
  "_score2": 200
}

POST my_sample_index/_doc/3
{
  "rank": 200,
  "name": "bruce",
  "_score1": 100,
  "_score2": 100
}

POST my_sample_index/_doc/4
{
  "rank": 2001,                        <--- Rank > 1000
  "name": "arthur",
  "_score1": 200,
  "_score2": 200
}

聚合查詢:

POST my_sample_index/_search
{
  "size":0,
  "aggs": {
    "_score": {
      "terms": {
        "script": {
          "source": """
            if(doc['rank'].value < 1000){
              return doc['rank'];
            }else
              return '1000-*';
          """
        }
      },
      "aggs":{
        "_score1_sum":{
          "sum": {
            "field": "_score1"
          }
        },
        "_score2_sum":{
          "sum":{
            "field": "_score2"
          }
        }
      }
    }
  }
}

請注意,我在腳本中通過邏輯提到的地方使用了腳本化術語聚合 一旦你經歷了它,我相信邏輯是不言自明的。

回復:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "_score" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1000-*",             <---- Note this
          "doc_count" : 2,              <---- Note this
          "_score2_sum" : {
            "value" : 400.0
          },
          "_score1_sum" : {
            "value" : 400.0
          }
        },
        {
          "key" : "100",
          "doc_count" : 1,
          "_score2_sum" : {
            "value" : 100.0
          },
          "_score1_sum" : {
            "value" : 100.0
          }
        },
        {
          "key" : "200",
          "doc_count" : 1,
          "_score2_sum" : {
            "value" : 100.0
          },
          "_score1_sum" : {
            "value" : 100.0
          }
        }
      ]
    }
  }
}

請注意,有兩個鍵的rank > 1000 ,它們的_score1_score2得分總和為400 ,這是預期的結果。

讓我知道這是否有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM