ES 聚合排序 ES 聚合查詢 ES 聚合查詢

本文基於ES 聚合查詢基礎編寫,測試數據的構建在ES 聚合查詢中查找.

 

1、_key排序

按每個桶的鍵值數值排序

GET food/_search
{
  "size": 0, 
  "aggs": {
    "tags_agg": {
      "terms": {
        "field": "Tags.keyword",
        "size": 30,//查詢多少條結果
        "order": {
          "_key": "desc"//鍵值數值排序
        }
      }
    }
  }
}

搜索結果如下:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "tags_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "非常好喫",
          "doc_count" : 1
        },
        {
          "key" : "進口",
          "doc_count" : 1
        },
        {
          "key" : "超級貴",
          "doc_count" : 1
        },
        {
          "key" : "",
          "doc_count" : 1
        },
        {
          "key" : "營養價值高",
          "doc_count" : 1
        },
        {
          "key" : "營養",
          "doc_count" : 3
        },
        {
          "key" : "綠色蔬菜",
          "doc_count" : 2
        },
        {
          "key" : "白色蔬菜",
          "doc_count" : 1
        },
        {
          "key" : "水果",
          "doc_count" : 3
        },
        {
          "key" : "有點貴",
          "doc_count" : 1
        },
        {
          "key" : "易種植",
          "doc_count" : 1
        },
        {
          "key" : "性價比",
          "doc_count" : 2
        },
        {
          "key" : "好喫",
          "doc_count" : 1
        },
        {
          "key" : "國外",
          "doc_count" : 1
        },
        {
          "key" : "便宜",
          "doc_count" : 1
        }
      ]
    }
  }
}

 

2、_count排序

按照每個桶的數量進行排序

GET food/_search
{
  "size": 0, 
  "aggs": {
    "tags_agg": {
      "terms": {
        "field": "Tags.keyword",
        "size": 30,//查詢多少條結果
        "order": {
          "_count": "desc" //按照每個桶的數量降序排序
        }
      }
    }
  }
}

搜索結果如下:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "tags_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "水果",
          "doc_count" : 3
        },
        {
          "key" : "營養",
          "doc_count" : 3
        },
        {
          "key" : "性價比",
          "doc_count" : 2
        },
        {
          "key" : "綠色蔬菜",
          "doc_count" : 2
        },
        {
          "key" : "便宜",
          "doc_count" : 1
        },
        {
          "key" : "國外",
          "doc_count" : 1
        },
        {
          "key" : "好喫",
          "doc_count" : 1
        },
        {
          "key" : "易種植",
          "doc_count" : 1
        },
        {
          "key" : "有點貴",
          "doc_count" : 1
        },
        {
          "key" : "白色蔬菜",
          "doc_count" : 1
        },
        {
          "key" : "營養價值高",
          "doc_count" : 1
        },
        {
          "key" : "",
          "doc_count" : 1
        },
        {
          "key" : "超級貴",
          "doc_count" : 1
        },
        {
          "key" : "進口",
          "doc_count" : 1
        },
        {
          "key" : "非常好喫",
          "doc_count" : 1
        }
      ]
    }
  }
}

 

3、多級聚合排序

現在需要對食物類型進行降序排序,每種食物類型按照食物品級進行降序排序,代碼如下:

GET food/_search
{
  "size": 0, 
  "aggs": {
    "type_bucket": {
      "terms": {
        "field": "Type.keyword",
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "level_bucket": {
          "terms": {
            "field": "Level.keyword",
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}

搜索結果如下:

{
  "took" : 396,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "type_bucket" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "水果",
          "doc_count" : 4,
          "level_bucket" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "高級水果",
                "doc_count" : 3
              },
              {
                "key" : "普通水果",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "蔬菜",
          "doc_count" : 3,
          "level_bucket" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "普通蔬菜",
                "doc_count" : 2
              },
              {
                "key" : "中等蔬菜",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
}

首先按照食物的類型進行降序排序,第一級的分桶確實是按照數量進行排序的,所以是對的,然後在食物類型的基礎上分別統計食物的品級.可以各個類型的第二級分桶也是按照數量進行降序排序的,也是對的.

 

4、深層嵌套排序

現在需要統計各個食物類型的分桶數量,排序的依據是各個食物類型的食物的最大值進行降序排序

GET food/_search
{
  "size": 0, 
  "aggs": {
    "type_aggs": {
      "terms": {
        "field": "Type.keyword",
        "order": {
          "aggs_stats.max": "desc"
        }
      },
      "aggs": {
        "aggs_stats":{
           "stats": {
              "field": "Price"
            }
        }
      }
    }
  }
}

搜索結果如下:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "type_aggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "水果",
          "doc_count" : 4,
          "aggs_stats" : {
            "count" : 4,
            "min" : 11.109999656677246,
            "max" : 300.1099853515625,
            "avg" : 127.85999655723572,
            "sum" : 511.43998622894287
          }
        },
        {
          "key" : "蔬菜",
          "doc_count" : 3,
          "aggs_stats" : {
            "count" : 3,
            "min" : 11.109999656677246,
            "max" : 66.11000061035156,
            "avg" : 29.77666664123535,
            "sum" : 89.32999992370605
          }
        }
      ]
    }
  }
}

接下去在對價格進行過濾,然後在統計,代碼如下:

GET food/_search
{
  "size": 0, 
  "aggs": {
    "type_aggs": {
      "terms": {
        "field": "Type.keyword",
        "order": {
          "price_aggs>stats_aggs.min": "asc"
        }
      },
      "aggs": {
        "price_aggs":{
          //過濾部分數據,不進行指標聚合,這裏相當於沒有操作
          "filter": {
            "terms": {
              "Type.keyword": ["蔬菜","水果"]
            }
          },
          "aggs": {
            "stats_aggs": {
              "stats": {
                "field": "Price"
              }
            }
          }
        }
        
      }
    }
  }
}

搜索結果如下:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "type_aggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "水果",
          "doc_count" : 4,
          "price_aggs" : {
            "doc_count" : 4,
            "stats_aggs" : {
              "count" : 4,
              "min" : 11.109999656677246,
              "max" : 300.1099853515625,
              "avg" : 127.85999655723572,
              "sum" : 511.43998622894287
            }
          }
        },
        {
          "key" : "蔬菜",
          "doc_count" : 3,
          "price_aggs" : {
            "doc_count" : 3,
            "stats_aggs" : {
              "count" : 3,
              "min" : 11.109999656677246,
              "max" : 66.11000061035156,
              "avg" : 29.77666664123535,
              "sum" : 89.32999992370605
            }
          }
        }
      ]
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章