anewbie
anewbie

Reputation: 81

Elastic search query and only return top unique results

Trying to run a terms query on elastic search and couldn't figure out how to limit the returns to only unique results? Assuming this is the query.

"query": {
    "bool": {
        "must": [{
            "terms": {
                "id": [
                    "1",
                    "2",
                    "3",
                ],
                "boost": 1.0
            }
        }],
        "adjust_pure_negative": true,
        "boost": 1.0
    }
},
"aggs": {
    "top-results": {
        "terms": {
            "field": "id"
        },
        "aggs": {
            "test": {
                "top_hits": {
                    "size": 1
                }
            }
        }
    }
} 

Ideally I would like to only have 3 results returned each one matching a id of 1, 2, or 3, but this query returns a lot more than that.

Upvotes: 0

Views: 1004

Answers (1)

Shraddha
Shraddha

Reputation: 884

In order to mimic your scenario, have pushed a set of 5 records of employees in elasticsearch having different salaries. So, I am trying to fetch the salaries listed with one record (top-hit) each.

GET /employee/_doc/_search
{
  "query": {
    "bool": {
          "should": [
            { "match": { "salary": 90000 }},
            { "match": { "salary": 80000 }} 
          ]
    }
  },
  "size" : 0,
  "aggs": {
    "salaries": {
      "terms": {
        "field":   "salary",      
        "order": { "top_score": "desc" } 
      },
      "aggs": {
        "top_score": { "max":      { "script":  "_score"           }}, 
        "salary-num": { "top_hits": { "size": 1 }}   
      }
    }
  }
}

OUTPUT


{
...
  "aggregations" : {
    "salaries" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 80000,
          "doc_count" : 2,
          "top_score" : {
            "value" : 1.0
          },
          "salary-num" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "employee",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : 1.0,
                  "_source" : {
                    "id" : 10,
                    "name" : "Lydia",
                    "dept" : "HR",
                    "salary" : 80000
                  }
                }
              ]
            }
          }
        },
        {
          "key" : 90000,
          "doc_count" : 1,
          "top_score" : {
            "value" : 1.0
          },
          "salary-num" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "employee",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : 1.0,
                  "_source" : {
                    "id" : 20,
                    "name" : "Flora",
                    "dept" : "Accounts",
                    "salary" : 90000
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Upvotes: 0

Related Questions