4

I have documents that look like this:

{  
    "clientId": "FPIev_86RwB",
    "viewId": "FPIewF1aRyU",
    "sessionId": "FPIewE16Rxu",
    "trackingId": "FPIewHfaRx9",
    "type": "view",
    "intVal": [  
        21,
        72,
        37
    ]
}

I want to different aggregations (such as sum or avg) on just one index of the array across a set of documents. However, it seems that I can't specify the index like I could specify a field on an object. Here is what I tried:

{
    "size": 0,
    "aggs": {
        "avg_1": {
            "avg": {
                "field": "intVal.1"
            }
        }
    }
}

As you can see, I tried selecting the index of the field with intVal.1 but that isn't working. I have a variable number of values that may be added to an array, but the values in a specific index position are all for the same thing so I want to do an aggregation on the array index. Is there any way I can make this work?

1 Answer 1

5

I found a solution using a script. The query looks like this:

{
    "size": 0,
    "aggs": {
        "avg_1": {
            "avg": {
                "script": "doc['intVal'][1].value"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.