Bool Query Should clause in elasticsearch vs java OR condition?


(Santoshkumar) #1
{

"bool": {
"should": [
{ "term": { "title": "brown" }} , -----> 1.
{ "term": { "title": "fox" }}
]
}
}

I am new into the Elasticsearch world need help in understanding the "bool" query "should" clause, as mentioned in few blogs and documentation, "Should" Clause in elasticsearch is like OR condition, where at least one or more conditions should be true to get the response depending on the "minimum_should_match" parameter.
The way i am visualizing OR condition in elasticsearch query is like Java OR condition where it will exit the flow with first encounter of True condition (1 in above code example), skipping remaining part of the flow. But with examples provided in Documentation It is clear that it is executing all the conditions, How can i avoid it??


(Christian Dahlqvist) #2

Are you seeing a performance problem with the query? Where in the documentation is it indicated that this is not optimized?


(Santoshkumar) #3

Thanks @Christian_Dahlqvist for the reply, yes I am more concerned about the performance of the query as i have millions of record, which is growing large day by day.

Requirement is when i fire the below query

 {
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "4": {
              "query": "kavana",
              "_name": "4.WordMatch"
            }
          }
        },
        {
          "match": {
            "4.exactMatch": {
              "query": "kavana",
              "_name": "4.exactMatch"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

I am getting below result which is executing true for both match query

Output

"matched_queries": ["4.WordMatch", "4.exactMatch"]

Requirement is If Match is happend for first match clause then I don't want check remaining match cases of Query because of it will decrease search performance and it will increase documents count. Expectation is Should work like java IF condition.

Expected output is

"matched_queries": ["4.WordMatch"]

or
"matched_queries": ["4.exactMatch"]

Or Is there anyway i can optimise this, so i get best performance out of it.??


(Christian Dahlqvist) #4

I am not sure I understand what the problem is. Can you please provide a minimal recreation example with data and query and show what result you are getting and what you are expecting?


(Santoshkumar) #5

@Christian_Dahlqvist

I have a simple JSON query for Elasticsearch that looks like this:

   {
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "id": "1"
          }
        },
        {
          "match": {
            "tags.name": "a1"
          }
        }
      ]
    }
  }
}

Actual output is,

1, a1

Means it will evaluate the both cases and based on scoring it will return the output.

Expected output

Either 1 OR a1

It should not execute second match case when first match case is executed to true or return some matching result.

In simple terms it should not evaluate second match condition itself when first condition is validated to true or returned some value.

How to achieve it?


(Christian Dahlqvist) #6

What does the documents look like?


(Santoshkumar) #7
{
   "_index": "example",
      "_type": "blogpost",
      "_id": "4",
      "_score": 1.0,
      "_source": {
        "id": "1",
        "title": "I hate cheese cake",
        "body": "I prefer chocolat cake",
        "tags":{
        "name" : [ "a1", "c1" ]
         } ,
        "status": "archived",
        "publish_date": "1985-08-03"
      }
    }, {
      "_index": "example",
      "_type": "blogpost",
      "_id": "5",
      "_score": 1.0,
      "_source": {
        "id": "2",
        "title": "I admire lions",
        "body": "They are so regal",
        "tags": {
         "name" : [ "a1", "b1" ]
         }
        "status": "published",
        "publish_date": "2016-08-02"
      }
    }

(Christian Dahlqvist) #8

minimum_should_match indicates that at least one of the two should clauses should match, not that it should only return 1 document, so I believe what you are seeing is correct. If you are looking to only return a single match as quickly as possible, you might be able to run a filter query with size set to 1.


(Santoshkumar) #9

Thanks @Christian_Dahlqvist could you please provide link to any such example to refer? it would be helpful.


(Santoshkumar) #10

@Christian_Dahlqvist Any update on this? or you need more information? please let me know..


(Christian Dahlqvist) #11

I think it should be enough to create a filter (so score does not have to be calculated for all hits), and then set size for the query to the number of results you want back.


(system) #12

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.