0

I have objects of which some are grouped by a 'masterID'. I need an aggregation/query that shows me as result the object with the highest 'relevance' per object group by 'masterID'.

With aggregation of term 'masterID' I can get buckets for each 'masterID'. But how do I get the highest 'relevance' object within each bucket?

The queries so far are:

curl -XGET 'http://localhost:9200/pwo/_search?search_type=count&size=0&pretty=true' -d '{
  "aggregations": {
    "masterIDs": {
      "terms": {
        "field": "masterID",
        "size": 0
      }
    }
  }
}
'

and

curl -XGET 'http://localhost:9200/pwo/_search?size=0&pretty=true' -d '{
  "aggregations": {
    "relevance": {
      "max": {
        "field": "relevance"
      }
    }
  }
}
'

Is there a way to solve this with a single query?

5
  • 1
    Yes! you can add the relevance aggregation block in the masterIDs block Commented Apr 20, 2015 at 9:45
  • Thanks, that worked fine. Any way to embed the field '_id' of the object with max 'relevance' to the output? Commented Apr 20, 2015 at 10:22
  • Seems to be working with a top_hits aggregation added to the relevance aggregation. Commented Apr 20, 2015 at 11:12
  • If problem is solved please answer your own question and accept it instead posting the answer in the question. Commented Apr 20, 2015 at 11:54
  • @eliasah , do you want to post the answer, I'll accept it then. Thanks. Commented Apr 20, 2015 at 12:24

1 Answer 1

1

You can add the relevance aggregation block in the masterIDs block like following :

curl -XGET 'http://localhost:9200/pwo/_search?size=0&pretty=true' -d '{
  "aggregations": {
    "masterIDs": {
      "terms": {
        "field": "masterID",
        "size": 0
      },
      "aggregations": {
        "relevance": {
          "max": { "field" : "relevance" }
        },
        "aggregations": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}
'
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.