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 :)
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.
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.
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.
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" } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With