Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - search using abbreviations

I am trying to setup an existing/custom analyzer that enable search using abbreviations. For example, if the text field is "Bank Of America", searching for BOfA or BOA, BofA etc should match this record.

How can I do that?

like image 927
Kiran Reddy Avatar asked Oct 30 '22 07:10

Kiran Reddy


1 Answers

You can probably use synonym filter token for a custom analyzer.

For example the following mappings

{
"settings": {
    "analysis": {
        "analyzer": {
            "my_analyzer": {
                "tokenizer": "standard",
                "filter": ["lowercase", "synonym_filter"]
            }
        },
        "filter": {
            "synonym_filter": {
                "type": "synonym",
                "synonyms": [
                    "bank of america,boa"
                ],
                "expand": true
            }
        }
    }
},
"mappings": {
    "document": {
        "properties": {
            "text": {
                "type": "text",
                "analyzer": "my_analyzer",
                "fielddata": true
            }
        }
    }
}

}

Definitely you can add more to the list or use a synonym file.

For query usecases BOfA or BOA, BofA - two approaches can be worked.

1) More synonyms with these possible combination

 "synonyms": [
                    "bank of america,boa"
                    "bank of america,bofa"
                ]

2) or keep the abrevations intact and use fuzzy query

{
  "query": {
    "match": {
      "text" : {
        "query": "bofa",
        "fuzziness": 2
      }
    }
  }
}

You will need synoyms to supply abrevations to ES.

like image 114
user3775217 Avatar answered Nov 15 '22 08:11

user3775217