2

i have json structure like this:

[{
    'id': 1,
    'result': [{
        "score": 0.0,
        "result_rules": [{
                "rule_id": "sr-1",
            },
            {
                "rule_id": "sr-2",
            }
        ]
    }]
},
{
    'id': 2,
    'result': [{
        "score": 0.0,
        "result_rules": [{
                "rule_id": "sr-1",
            },
            {
                "rule_id": "sr-4",
            }
        ]
    }]
}]

i want to count rule_id, so the result would be:

[
    {
        'rule_id': 'sr-1',
        'doc_count': 2
    },
    {
        'rule_id': 'sr-2',
        'doc_count': 1
    },
    {
        'rule_id': 'sr-4',
        'doc_count': 1
    }
]

i've tried something like this, but it's showing empty aggregation

{
    "aggs":{
        "group_by_rule_id":{
            "terms":{
                "field": "result.result_rules.rule_id.keyword"
            }
        }
    }
}

1 Answer 1

1

For aggregation on nested structure you would have to use nested aggregation.

See the example on ES DOC.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you the query is success but i want to add some filter for example result.result_rules.status:true, how do i perform it? @anand
you can combine the nested aggregation with filter see the example: elastic.co/guide/en/elasticsearch/reference/current/…
i've got an error named_object_not_found_exception: unable to parse BaseAggregationBuilder with name
the query looks like this myjson.com/d3o7m and the error is :unable to parse BaseAggregationBuilder with name [group_by_rule_id]: parser not found
i've fixed it, misplaced second aggs did that error

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.