0

I have a nested field with two properties:

{
    "rating": 2
    "victim": [{
        "ip":  "...",
        "instanceId": "....""
    }]
}

I want to aggregate over the rating and get the IP or InstanceId for each document, not both in the bucket list.

What i've got so far is:

"__rating": {
    "terms": {"field": "rating"},
    "aggs": {
        "__hosts": {
            "nested": {"path": "victim"},
            "aggs": {
                "ips": {"terms": {"field": "victim.ip"} } ,
                "instances": {"terms": {"field": "victim.instanceId"} }
            }
        }
    }
}

Above aggregation gets me the buckets for both fields but i need one or other.

Thanks.

2 Answers 2

0

You need to use source filtering in elasticsearch. Add this piece of code in the top of your query:

"_source": {
  "includes": [ "victim.ip"]
}

If you want more detail about source filtering, you should check this out.

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

Comments

0

After a lot of searching, i found the way to do it via painless script:

"terms" : {
    "script" : {
        "source": "doc['victim.instanceId'].value == null ? doc['victim.ip'] : doc['victim.instanceId']",
        "lang": "painless"
    },
    "missing": "N/A"
}

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.