Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: How to write an 'OR' clause in filter context?

I'm looking for syntax/example compatible with ES version is 6.7. I have seen the docs, I don't see any examples for this and the explanation isn't clear enough to me. I have tried writing query according to that, but I keep on getting syntax error. I have seen below questions on SO already but they don't help me:

Filter context for should in bool query (Elasticsearch)

It doesn't have any example.

Multiple OR filter in Elasticsearch

I get a syntax error

"type": "parsing_exception",
"reason": "no [query] registered for [filtered]",
"line": 1,
"col": 31

Maybe it's for a different version of ES.

All I need is a simple example with two 'or'ed conditions (mine is one range and one term but I guess that shouldn't matter much), both I would like to have in filter context (I don't care about scores, nor text search).

If you really need it, I can show my attempts (need to remove some 'sensitive'(duh) parts from it before posting), but they give parsing/syntax errors so I don't think there is any sense in them. I am aware that questions which don't show any efforts are considered bad for SO but I don't see any logic in showing attempts that aren't even parsed successfully, and any example would help me understand the syntax.

like image 732
shoonya ek Avatar asked Aug 07 '19 10:08

shoonya ek


People also ask

How do I merge two queries in Elasticsearch?

You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses.

How do you use terms in Elasticsearch?

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields. By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.

What is the difference between query and filter in Elasticsearch?

Queries are slower it returns a calculated score of how well a document matches the query. Filters are faster because they check only if the document matched or not. Queries produce non-boolean values. Filters produce boolean values.


2 Answers

You need to wrap your should query in a filter query.

{  
   "query":{  
      "bool":{  
         "filter":[{  
            "bool":{  
               "should":[  
                  {  // Query 1 },
                  {  // Query 2 }
               ]
            }
         }]
      }
   }
}
like image 169
Pierre-Nicolas Mougel Avatar answered Nov 10 '22 03:11

Pierre-Nicolas Mougel


I had a similar scenario (even the range and match filter), with one more nested level, two conditions to be 'or'ed (as in your case) and another condition to be logically 'and'ed with its result. As @Pierre-Nicolas Mougel suggested in another answer I had nested bool clauses with one more level around the should clause.

{
  "_source": [
    "my_field"
  ],
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "start": {
                        "gt": "1558878457851",
                        "lt": "1557998559147"
                      }
                    }
                  },
                  {
                    "range": {
                      "stop": {
                        "gt": "1558898457851",
                        "lt": "1558899559147"
                      }
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "my_id": "<My_Id>"
              }
            }
          ],
          "must_not": []
        }
      }
    }
  },
  "from": 0,
  "size": -1,
  "sort": [],
  "aggs": {}
}

I read in the docs that minimum_should_match can be used too for forcing filter context. This might help you if this query doesn't work.

like image 27
0xc0de Avatar answered Nov 10 '22 04:11

0xc0de