Xc's Blog

ES的聚合

1996/10/01 Share

scripted-metric-aggregation

abstract

可以执行脚本,并且返回结果,可用于复杂查询

只有map_script

允许的返回值

- primitive types
- String
- Map (containing only keys and values of the types listed here)
- Array (containing elements of only the types listed here)

DEMO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POST ledger/_search?size=0
{
"query" : {
"match_all" : {}
},
"aggs": {
"profit": {
"scripted_metric": {
"init_script" : "params._agg.transactions = []",
"map_script" : "params._agg.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)",
"combine_script" : "double profit = 0; for (t in params._agg.transactions) { profit += t } return profit",
"reduce_script" : "double profit = 0; for (a in params._aggs) { profit += a } return profit"
}
}
}
}

docs

  • init_script

    在找到文档之前就执行,一般会用来进行值的初始化

    Executed prior to any collection of documents. Allows the aggregation to set up any initial state.

    In the above example, the init_script creates an array transactions in the _agg object.

  • map_script

    每次找到拿到一个文档就会执行。这是一个必须的参数。如果没有定义combine_script,则需要吧结果放在_agg这个对象中

    Executed once per document collected. This is the only required script. If no combine_script is specified, the resulting state needs to be stored in an object named _agg.

    In the above example, the map_script checks the value of the type field. If the value is sale the value of the amount field is added to the transactions array. If the value of the type field is not sale the negated value of the amount field is added to transactions.

  • combine_script

    在每个分片上执行一次,在分片上完成文档收集后执行。允许聚合巩固每个分片上返回的状态。如果combine_script没有提供,则默认返回聚合对象

    Executed once on each shard after document collection is complete. Allows the aggregation to consolidate the state returned from each shard. If a combine_script is not provided the combine phase will return the aggregation variable.

    In the above example, the combine_script iterates through all the stored transactions, summing the values in the profit variable and finally returns profit.

  • reduce_script

    在所有的分片都返回结果后会执行,这个脚本可以访问所有的aggs对象列表。若没有这个脚本,则直接返回_aggs对象

    Executed once on the coordinating node after all shards have returned their results. The script is provided with access to a variable _aggs which is an array of the result of the combine_script on each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.

    In the above example, the reduce_script iterates through the profit returned by each shard summing the values before returning the final combined profit which will be returned in the response of the aggregation.

Filter Aggregationedit

abstract

Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.

将搜索结果根据fitler分类

Example:

POST /sales/_search?size=0
{
“aggs” : {
“t_shirts” : {
“filter” : { “term”: { “type”: “t-shirt” } },
“aggs” : {
“avg_price” : { “avg” : { “field” : “price” } }
}
}
}
}
COPY AS CURLVIEW IN CONSOLE
In the above example, we calculate the average price of all the products that are of type t-shirt.

Response:

{

“aggregations” : {
“t_shirts” : {
“doc_count” : 3,
“avg_price” : { “value” : 128.33333333333334 }
}
}
}

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-scripted-metric-aggregation.html

CATALOG
  1. 1. scripted-metric-aggregation
    1. 1.1. abstract
    2. 1.2. 允许的返回值
    3. 1.3. DEMO
    4. 1.4. docs
  2. 2. Filter Aggregationedit
    1. 2.1. abstract
    2. 2.2. link