Using aggregation functions in Elasticsearch queries
Asked Answered
B

1

6

I'm using elasticsearch 0.90.10 and I want to perform a search on it using a query with aggregation functions like sum(), avg(), min().

Suppose my data is something like that

[
    {
        "name" : "Alice",
        "grades" : [40, 50, 60, 70]
    },

    {
        "name" : "Bob",
        "grades" : [10, 20, 30, 40]
    }, 

    {
        "name" : "Charlie",
        "grades" : [70, 80, 90, 100]
    }
]

Let's say I need to fetch students with average grade greater than 75 (i.e. avg(grades) >= 75). How can I wrote such a query in ES using DSL, filters or scripting?

Thanks in advance.

Baku answered 21/1, 2014 at 10:1 Comment(0)
R
4

The new ES 1.0.0.RC1 that is out might have better ways to do this with aggregations BUT here is a simple (and very verbose) script that works:

POST /test_one/grades/_search
{
    "query" : {
        "match_all": {}
    },
    "filter" : {
        "script" : {
            "script" : " sum=0; foreach( grade : doc['grades'].values) { sum = sum + grade }; avg = sum/doc['grades'].values.length; avg > 25;  "
        }
    }
}

Data I tested with:

POST /test_one/grades
{
    "name": "chicken",
    "grades": [35,55,65]
}

POST /test_one/grades
{
    "name": "pork",
    "grades": [15,35,45]
}

POST /test_one/grades
{
    "name": "kale",
    "grades": [5,10,20]
}
Romina answered 21/1, 2014 at 15:6 Comment(1)
it should, as my original comment states this is the existing script way of doing things.Romina

© 2022 - 2024 — McMap. All rights reserved.