0
votes

Would you please explain for me how Elastic Search boolean query works? I've read the documentation here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html but seems like it's too simple I can not understand. look at this query:

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        },
        "must_not" : {
            "range" : {
                "age" : { "from" : 10, "to" : 20 }
            }
        },
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}

I can not understand the usage of 'should' and 'minimum_should_match'. Would you please explain it to me?

1

1 Answers

1
votes

In the query you have provided should will bring the documents up( means they will come first) if they satisfy the must and must_not part. In this should will match if any one of condition will satisfy provided in the should array (it will join should with OR operator)

Now consider this case

    {
          "bool": {
            "should": [
              {
                "term": {
                  "tag": "wow"
                }
              },
              {
                "term": {
                  "tag": "elasticsearch"
                }
              }
            ],
            "minimum_should_match": 1,
            "boost": 1
          }
        } 

In this there is no must and must_not then it will match all the conditions in should array . It will return documents which contains both tags wow & elasticsearch (will join should clauses with AND operator )and in your query (in which it contains must part also) it will join should clauses with OR operator .

And for getting clear with minimum_should_match please refer this

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html

Please let me know if i was able to clarify the difference and functionality ..