ElasticSearch usage summary (seven)

Elasticsearch provides a variety of aggregation methods, which can help users quickly conduct information statistics and classification. This article mainly explains how to use Range aggregation.

The simplest example, if you want to count a class exam below 60 points, 60 to 80 points, 80 to 100 points, you can easily count with just one command in ES....
Aggregate example

{
    "aggs":{
        "grade_ranges":{
            "range":{
                "field":"grade",
                "ranges":[
                    {"to":60},
                    {"from":60,"to":80},
                    {"from":80}]
                }
            }
        }
}

The following results are obtained:

{
    ...
    "aggregations":{
        "price_ranges":{
            "buckets":[
                {
                    "to":60,
                    "doc_count":2
                },
                {
                    "from":60,
                    "to":80,
                    "doc_count":32
                },
                {
                    "from":80,
                    "doc_count":14
                }
            ]
        }
    }
}

An example of a complex point, specifying the name of each interval
can be set to keyed: true, so that each interval returns a specific name

{
    ...
    "aggregations":{
        "price_ranges":{
            "buckets":{
                "*-50.0":{
                    "to":50,
                    "doc_count":2
                },
                "50.0-100.0":{
                    "from":50,
                    "to":100,
                    "doc_count":4
                },
                "100.0-*":{
                    "from":100,
                    "doc_count":4
                }
            }
        }
    }
}

Of course, you can also specify the name of the interval:

{
    "aggs":{
        "price_ranges":{
            "range":{
                "field":"price",
                "keyed":true,
                "ranges":[
                    {"key":"cheap","to":50},
                    {"key":"average","from":50,"to":100},
                    {"key":"expensive","from":100}
                ]
            }
        }
    }
}

Using scripts
Similar to other aggregations, Range aggregation supports the use of scripts:

{
    "aggs":{
        "price_ranges":{
            "range":{
                "script":"doc['price'].value",
                "ranges":[
                    {"to":50},
                    {"from":50,"to":100},
                    {"from":100}
                ]
            }
        }
    }
}

The operations of file scripts or script values ​​are similar to others, so I won't repeat them.
Aggregation nesting
Usually in interval aggregation, sub-aggregations are nested. For example, we do statistical stats aggregation in each interval:

{
    "aggs":{
        "price_ranges":{
            "range":{
                "field":"price",
                "ranges":[
                    {"to":50},
                    {"from":50,"to":100},
                    {"from":100}
                ]},
                "aggs":{
                    "price_stats":{
                        "stats":{ "field":"price" } }
                }
            }
        }
    }

Then the result will look like this:

{
    "aggregations":{
        "price_ranges":{
            "buckets":[
                {
                    "to":50,
                    "doc_count":2,
                    "price_stats":{
                        "count":2,
                        "min":20,
                        "max":47,
                        "avg":33.5,
                        "sum":67 }
                },
                {
                    "from":50,
                    "to":100,
                    "doc_count":4,
                    "price_stats":{
                        "count":4,
                        "min":60,
                        "max":98,
                        "avg":82.5,
                        "sum":330 }
                },
                {
                    "from":100,
                    "doc_count":4,
                    "price_stats":{
                        "count":4,
                        "min":134,
                        "max":367,
                        "avg":216,
                        "sum":864 }
                }
            ]
        }
    }
}

If you do not specify the aggregated fields, the statistics will be calculated according to the Range aggregated fields by default:

{
    "aggs":{
        "price_ranges":{
            "range":{
                "field":"price",
                "ranges":[
                    {"to":50},
                    {"from":50,"to":100},
                    {"from":100}
                ]
            },
            "aggs":{
                "price_stats":{
                    "stats":{} }
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773786&siteId=291194637