Home > OS >  Stop elastic search tokenizing a query
Stop elastic search tokenizing a query

Time:10-21

I'm trying to filter out some documents in elastic search 8.4. The issue I'm having is something like this...

must_not: [
    match: { ingredients: { query : 'peanut butter' } }
]

seems to break the query into 'peanut' and 'butter'. Then, documents which contain the ingredient 'butter' get incorrectly filtered. Is there a way to prevent this tokenizing without defining a custom analyzer? Or perhaps a different way to search to get that result?

CodePudding user response:

If you don't want to filter documents with just "peanut" or "butter" you need to use the "and" operator. In this way only documents with "peanut butter" will be filtered.

{
   "query": {
     "bool": {
       "must_not": [
         {
           "match": {
             "ingredients": {
               "query": "peanut butter",
               "operator": "and"
             }
           }
         }
       ]
     }
   }
}
  • Related