Elasticsearch - 聚合

此框架收集搜索查询选择的所有数据.该框架由许多构建块组成,这有助于构建复杂的数据摘要.聚合的基本结构如下所示:

"aggregations" : {
   "<aggregation_name>" : {
      "<aggregation_type>" : {
         <aggregation_body>
      }
		
      [,"meta" : { [<meta_data_body>] } ]?
      [,"aggregations" : { [<sub_aggregation>]+ } ]?
   }
}

有不同类型的聚合,每种聚合都有自己的目的和减号;

度量标准聚合

这些聚合有助于根据聚合文档的字段值计算矩阵,有时可以从脚本生成一些值.

数字矩阵单值,如平均聚合多值,如 stats .

平均聚合

此聚合用于获取聚合文档中存在的任何数字字段的平均值.例如,

POST http://localhost:9200/schools/_search

请求正文

{
   "aggs":{
      "avg_fees":{"avg":{"field":"fees"}}
   }
}

回复

{
   "took":44, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},
   "hits":{
      "total":3, "max_score":1.0, "hits":[
         {
            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Saint Paul School", "description":"ICSE Affiliation",
               "street":"Dawarka", "city":"Delhi", "state":"Delhi", 
               "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000, 
               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
            }
         },
			
         {
            "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
            "_source":{
               "name":"Central School", "description":"CBSE Affiliation",
               "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
               "location":[31.8955385, 76.8380405], "fees":2200, 
               "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
            }
         },
			
         {
            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
            "_source":{
               "name":"Crescent School", "description":"State Board Affiliation",
               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", 
               "zip":"176114", "location":[26.8535922, 75.7923988], "fees":2500, 
               "tags":["Well equipped labs"], "rating":"4.5"
            }
         }
      ]
   }, "aggregations":{"avg_fees":{"value":3233.3333333333335}}
}

如果一个或多个聚合文档中不存在该值,则默认情况下将忽略它。 您可以在聚合中添加一个缺失字段,以将缺失值视为默认值。

{
   "aggs":{
      "avg_fees":{
         "avg":{
            "field":"fees"
            "missing":0
         }
      }
   }
}

Cardinality Aggregation

此聚合提供了特定字段的不同值的计数。 例如,

POST http://localhost:9200/schools*/_search

Request Body

{
   "aggs":{
      "distinct_name_count":{"cardinality":{"field":"name"}}
   }
}

Response

………………………………………………
{
   "name":"Government School", "description":"State Board Afiliation",
   "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
   "location":[18.599752, 73.6821995], "fees":500, "tags":["Great Sports"], 
   "rating":"4"
},

{
   "_index":"schools_gov", "_type": "school", "_id":"1", "_score":1.0,
   "_source":{
      "name":"Model School", "description":"CBSE Affiliation", "street":"silk city",
      "city":"Hyderabad", "state":"AP", "zip":"500030", 
      "location":[17.3903703, 78.4752129], "fees":700, 
      "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
   }
}, "aggregations":{"disticnt_name_count":{"value":3}}
………………………………………………

注意:基数的值为3,因为名称中存在三个不同的值-政府,学校和示范。

Extended Stats Aggregation

此聚合生成有关聚合文档中特定数字字段的所有统计信息。 例如,

POST http://localhost:9200/schools/school/_search

Request Body

{
   "aggs" : {
      "fees_stats" : { "extended_stats" : { "field" : "fees" } }
   }
}

Response

………………………………………………
{
   "aggregations":{
      "fees_stats":{
         "count":3, "min":2200.0, "max":5000.0, 
         "avg":3233.3333333333335, "sum":9700.0,
         "sum_of_squares":3.609E7, "variance":1575555.555555556, 
         "std_deviation":1255.2113589175156,
				
         "std_deviation_bounds":{
            "upper":5743.756051168364, "lower":722.9106154983024
         }
      }
   }
}
………………………………………………

Max Aggregation

此聚合在聚合的文档中查找特定数字字段的最大值。 例如,

POST http://localhost:9200/schools*/_search

Request Body

{
   "aggs" : {
      "max_fees" : { "max" : { "field" : "fees" } }
   }
}

Response

………………………………………………
{
   aggregations":{"max_fees":{"value":5000.0}}
}
………………………………………………

Min Aggregation

此聚合在聚合的文档中查找特定数字字段的最大值。 例如,

POST http://localhost:9200/schools*/_search

Request Body

{
   "aggs" : {
      "min_fees" : { "min" : { "field" : "fees" } }
   }
}

Response

………………………………………………
"aggregations":{"min_fees":{"value":500.0}}
………………………………………………

Sum Aggregation

此聚合计算聚合文档中特定数字字段的总和。 例如,

POST http://localhost:9200/schools*/_search

Request Body

{
   "aggs" : {
      "total_fees" : { "sum" : { "field" : "fees" } }
   }
}

Response

………………………………………………
"aggregations":{"total_fees":{"value":10900.0}}
………………………………………………

在特殊情况下还有其他一些度量标准聚合,例如地理边界聚合和地理质心聚合,以实现地理位置。

Bucket Aggregations

这些聚合包含用于具有标准的不同类型聚合的许多存储桶,该标准确定文档是否属于该存储桶。 桶聚合描述如下:

Children Aggregation

此存储桶聚合构成了一系列文档,这些文档被映射到父存储桶。 类型参数用于定义父索引。 例如,我们有一个品牌及其不同的模型,那么模型类型将具有以下_parent字段:

{
   "model" : {
      "_parent" : {
         "type" : "brand"
      }
   }
}

还有许多其他特殊的存储桶聚合,它们在许多其他情况下很有用,它们是:

  • Date Histogram Aggregation

  • Date Range Aggregation

  • Filter Aggregation

  • Filters Aggregation

  • Geo Distance Aggregation

  • GeoHash grid Aggregation

  • Global Aggregation

  • Histogram Aggregation

  • IPv4 Range Aggregation

  • Missing Aggregation

  • Nested Aggregation

  • Range Aggregation

  • Reverse nested Aggregation

  • Sampler Aggregation

  • Significant Terms Aggregation

  • Terms Aggregation

Aggregation Metadata

您可以在请求时使用meta标记添加一些有关聚合的数据,并作为响应获取。 例如,

POST http://localhost:9200/school*/report/_search

Request Body

{
   "aggs" : {
      "min_fees" : { "avg" : { "field" : "fees" } ,
         "meta" :{
            "dsc" :"Lowest Fees"
         }
      }
   }
}

Response

………………………………………………
{
   "aggregations":{"min_fees":{"meta":{"dsc":"Lowest Fees"}, "value":2180.0}}
}
………………………………………………