0

I have been trying to get unique results on a query in elasticsearch. The query that I have written is given below. I am trying to get a solution to get unique results on the field called "penny". Thanks in advance.

    {
  "query": {
      "exists": {
        "field": "penny"
      }
    }
  }
}
4
  • What exactly you mean by unique results? To get list of all values that exists for field penny? Commented May 18, 2024 at 9:40
  • Penny can have duplicate records and the results are huge. For example, the filter will result @MilanGatyás 400012121312 400012121312 400012121396 400012121334 400012121334 I want it to return only the following (only unique penny records): 400012121312 400012121396 400012121334 Commented May 21, 2024 at 8:07
  • you can find it here: Return Unique Values Commented May 27, 2024 at 12:03
  • @AbhinavArya based on your comment I think you might benefit from elastic.co/guide/en/elasticsearch/reference/current/… Commented May 30, 2024 at 10:03

1 Answer 1

0

Try the terms aggregation

Your documents

PUT /unique_results/_bulk
{"create":{"_id":1}}
{"penny":400012121312}
{"create":{"_id":2}}
{"penny":400012121312}
{"create":{"_id":3}}
{"penny":400012121396}
{"create":{"_id":4}}
{"penny":400012121334}
{"create":{"_id":5}}
{"penny":400012121334}

Query

GET /unique_results/_search?filter_path=aggregations
{
    "aggs": {
        "by_penny": {
            "terms": {
                "field": "penny",
                "size": 10
            }
        }
    }
}

Response

{
    "aggregations" : {
        "by_penny" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
                {
                    "key" : 400012121312,
                    "doc_count" : 2
                },
                {
                    "key" : 400012121334,
                    "doc_count" : 2
                },
                {
                    "key" : 400012121396,
                    "doc_count" : 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.