2

I'm new to Elastic Search and can't find information on how to use not or must_not as term filter with elastic search.

My query is set up as follows:

        {
            "size":4,
            "from":0,
            "query":{
                "bool":{
                    "filter":[
                        {
                            "term":{
                                "published":1
                            }
                        },
                        {
                            "term":{
                                "brand.keyword":"Honda"
                            }
                        },
                        {
                            "not": {
                                "term": {
                                    "tags": "Red"
                                }
                            }
                        },
                        {
                            "wildcard":{
                                "image":"*"
                            }
                        }
                    ]
                }
            }
        }

But when I test in Postman I get the error:

 "type": "parsing_exception",
 "reason": "no [query] registered for [not]"

Would anyone know how I could fix this?

L.Vallet
  • 665
  • 1
  • 11
  • 16
MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • https://stackoverflow.com/questions/60646098/what-is-difference-between-match-query-and-bool-must-match-query/60646944#60646944 this also gives some more information – Amit Mar 17 '20 at 06:32

2 Answers2

2

Note that the filter clause works as a must clause. The difference between the two is only that any query inside the filter clause will not be influencing the score of the document or in other words for the filter clause, the score is not calculated whereas for must, must_not and should the score will be calculated. This is known as filter context and query context.

Now if you don't want must_not to be part of score calculation then the query will be:

{
  "size": 4,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "published": 1
          }
        },
        {
          "term": {
            "brand.keyword": "Honda"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "tags": "Red"
                }
              }
            ]
          }
        },
        {
          "wildcard": {
            "image": "*"
          }
        }
      ]
    }
  }
}
Nishant
  • 7,504
  • 1
  • 21
  • 34
  • Thanks, this gets rid of the error but does not affect the results – MeltingDog Mar 17 '20 at 03:45
  • @MeltingDog Please add index mapping, sample docs and expected result for more clarity. You can add it here in the same question or write up a separate question for the same. – Nishant Mar 17 '20 at 03:47
  • I think I got it. Since the tags were an array I needed to use `"tags.keyword": "Red"` – MeltingDog Mar 17 '20 at 04:05
1

You can use must_not clauses directly because they are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Exactly like filter clauses and therefore differently from the must and should clauses.

Here is the boolean query documentation.

Here is your query:

{
  "size": 4,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "published": 1
          }
        },
        {
          "term": {
            "brand.keyword": "Honda"
          }
        },
        {
          "wildcard": {
            "image": "*"
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "tags.keyword": "Red"
          }
        }
      ]
    }
  }
}
L.Vallet
  • 665
  • 1
  • 11
  • 16