0
votes

I was following the guide here https://dzone.com/articles/23-useful-elasticsearch-example-queries and the bool query below confuses me:

{
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "title": "Elasticsearch" }},
                      { "match": { "title": "Solr" }} ] }
            },
            "must": { "match": { "authors": "clinton gormely" }},
            "must_not": { "match": {"authors": "radu gheorge" }}
        }
    }
}

According to the tutorial, the explanation of the query is:

Search for a book with the word “Elasticsearch” OR “Solr” in the title, AND is authored by “clinton gormley” but NOT authored by “radu gheorge”

My question is, there are 3 conditions but also 3 logical operator in the bool query (must, must, must_not) instead of 2. My understanding is that 3 conditions should only have 2 logical operator like COND1 AND COND2 AND !COND3.

Is there something I am missing here?

1

1 Answers

1
votes

That query is wrong and will not work, it is not possible to have two bool/must clauses, they must be merged like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "title": "Elasticsearch"
                }
              },
              {
                "match": {
                  "title": "Solr"
                }
              }
            ]
          }
        },
        {
          "match": {
            "authors": "clinton gormley"
          }
        }
      ],
      "must_not": {
        "match": {
          "authors": "radu gheorge"
        }
      }
    }
  }
}

And the reason why the bool/should clause is burried inside the bool/must one is to give and equal weight to the match on authors than the two matches on the title.