DSLS Elasticsearch query from SQL statement

I am new to Elasticsearch. I don’t think I fully understand the concept of queries and filters. In my case, I just want to use filters, because I do not want to use a function in advance, such as scoring.

How to convert the following SQL statement to an elasticsearch query?

SELECT * FROM advertiser WHERE company like '%com%' AND sales_rep IN (1,2) 

What I still have:

 curl -XGET 'localhost:9200/advertisers/advertiser/_search?pretty=true' -d ' { "query" : { "bool" : { "must" : { "wildcard" : { "company" : "*com*" } } } }, "size":1000000 }' 

How to add OR filters to the sales_rep field?

thanks

+6
source share
2 answers

Add the sentence "should" after the sentence. In a bool request, one or more prerequisites must match by default. In fact, you can set "minimum_number_should_match" as any number, see bool docs .

In your case, this should work.

  "should" : [ { "term" : { "sales_rep_id" : "1" } }, { "term" : { "sales_rep_id" : "2" } } ], 

The same concept works for bool filters. Just change the "query" to "filter". The bool filter docs are here .

+2
source

I stumbled upon this post for 4 years ...

In any case, perhaps the following code might be useful ...

 { "query": { "filtered": { "query": { "wildcard": { "company": "*com*" } }, "filter": { "bool": { "should": [ { "terms": { "sales_rep_id": [ "1", "2" ] } } ] } } } } } 
0
source

Source: https://habr.com/ru/post/904102/


All Articles