Synonyms can replace existing tokens or be added to the token stream by using the
{ref}/analysis-synonym-tokenfilter.html[synonym
token filter]:
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym", (1)
"synonyms": [ (2)
"british,english",
"queen,monarch"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter" (3)
]
}
}
}
}
}
-
First, we define a token filter of type
synonym
. -
We discuss synonym formats in [synonym-formats].
-
Then we create a custom analyzer that uses the
my_synonym_filter
.
Tip
|
Synonyms can be specified inline with the |
Testing our analyzer with the analyze
API shows the following:
GET /my_index/_analyze?analyzer=my_synonyms
Elizabeth is the English queen
Pos 1: (elizabeth)
Pos 2: (is)
Pos 3: (the)
Pos 4: (british,english) (1)
Pos 5: (queen,monarch) (1)
-
All synonyms occupy the same position as the original term.
A document like this will match queries for any of the following: English queen
,
British queen
, English monarch
, or British monarch
.
Even a phrase query will work, because the position of
each term has been preserved.
Tip
|
Using the same Whether to do synonym expansion at search or index time can be a difficult choice. We will explore the options more in [synonyms-expand-or-contract]. |