22

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.

shoonya ek
  • 317
  • 1
  • 2
  • 12

2 Answers2

28

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

{  
   "query":{  
      "bool":{  
         "filter":[{  
            "bool":{  
               "should":[  
                  {  // Query 1 },
                  {  // Query 2 }
               ]
            }
         }]
      }
   }
}
Pierre-Nicolas Mougel
  • 2,209
  • 12
  • 18
  • Thank you for the answer. The other answer also included a range part so I accepted that. – shoonya ek Aug 07 '19 at 22:48
  • I was looking to use a `must` for search query term but then only return a result that matched one of a list of values for a specific property, and this approach worked adding a secondary `bool` with `should` - thanks! – aroundtheworld Oct 07 '21 at 05:42
18

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.

0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • 2
    `minimum_should_match` will not force filter context but it can be used to ensure that at least one constraint is true (which is the default behavior of `should` in a filter context). However, in a filter context the score will **NOT** be computed and the results may be cached for faster subsequent execution. – Pierre-Nicolas Mougel Aug 07 '19 at 15:37
  • In your query the `must` clause is not useful. You are already in a filter context so `must` will work the same as `filter` – Pierre-Nicolas Mougel Aug 07 '19 at 15:37
  • @Pierre-NicolasMougel Thanks, I didn't know that minimum match field does that. Maybe I was not clear enough in my answer but I wanted to point out that I had 3 conditions and one more level of nesting of "bool" than what the OP has asked. I thought giving that would help as all the OP is asking about/missing is this nesting, and extra information shouldn't hurt. – 0xc0de Aug 08 '19 at 09:34
  • Ok, but really you don't need the must clause. Your match query can be added directly to the filter condition, it won't change the result. The bool should can also be added directly to the filter, no need for the must. Sorry if my comment seemed harsh. – Pierre-Nicolas Mougel Aug 08 '19 at 09:58
  • @Pierre-NicolasMougel No, it's not harsh, I first probably mistook your comment thinking you're saying that the extra nesting is not needed **for answering the OP's question.** I guess you're saying that **it's unneeded for even what I wanted to achieve out of my query**. – 0xc0de Aug 08 '19 at 11:35