1
votes

I'm looking for a way to fuzzy match against a field where the words are equal.

ie. if I have a field with values like so:-

foobar
foobar number2
barfoo
barfoo number2

and I search with fooba I would like to only return the document foobar and not foobar number2

If I search with number2 I would like to not return anything as both number2 values have 2 words.

I know a term query can't be fuzzy. Is this possible with any query?

I'm using elasticsearch 5.4

Thanks

1

1 Answers

1
votes

After fighting with this for 2 days I worked it out after 20 mins of posting the question.

You need to create a custom analyzer for the field using the keyword tokenizer:-

"analyzer": {
   "all_words_analyzer": {
      "filter": [
          "lowercase"
      ],
      "type": "custom",
      "tokenizer": "keyword"
   }
}

Then set that on the field:-

"term": {
    "type": "text",
    "analyzer": "all_words_analyzer"
}

now a normal fuzzy match will work as above.

"query": {
   "fuzzy": {
       term": {
           "value": "fooba"
       }
   }
}