Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do an ElasticSearch Select Distinct

I just want to do the following request with elasticsearch.

In SQL :

Select distinct(id) from my_table where userid = '20' or activity = '9'; 

I just have :

{    "query" : {         "bool" : {                "should" : [                    { "term" : { "userid" : "20" } },                    { "term" : { "activity" : "9" } }                ]          }     } } 

Thanks in advance :)

like image 631
DyM Avatar asked Nov 02 '16 13:11

DyM


People also ask

How do I get distinct values in Elasticsearch?

The solution recommended by elasticsearch for this situation is to use a composite aggregation. Advantages of using a composite aggregation: Allows you to paginate and scroll through all the unique values. You will not need to know how many unique values are present before hand.

How do I make a field unique in Elasticsearch?

One solution will be to use uniqueId field value for specifying document ID and use op_type=create while storing the documents in ES. With this you can make sure your uniqueId field will have unique value and will not be overridden by another same valued document.

What is Elasticsearch DSL?

Elasticsearch DSL is a high-level library whose aim is to help with writing and running queries against Elasticsearch. It is built on top of the official low-level client ( elasticsearch-py ). It provides a more convenient and idiomatic way to write and manipulate queries.


1 Answers

You're almost there, you simply need to add a terms aggregation to your query

{    "query" : {         "bool" : {                "should" : [                    { "term" : { "userid" : "20" } },                    { "term" : { "activity" : "9" } }                ]          }     },     "aggs":{         "unique_ids": {             "terms": {                 "field": "id"             }         }     } } 
like image 161
Val Avatar answered Oct 11 '22 02:10

Val