It is currently not possible to use an ad-hoc synonym token filter due to the implementation requiring access "to the tokenizer factories for the index." (See elasticsearch Github issue.) Unfortunately this limitation is currently undocumented for the docs on using custom token filters on the _analyze endpoint
Here are some example commands to create and update a synonym token filter using the method of re-opening the index:
# create index with filter
curl -v -X PUT -s -H 'Content-Type: application/json' 'localhost:9200/syn_test_idx' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"test_synonym_filter" : {
"type" : "synonym",
"synonyms" : [
"i-pod, i pod => ipod",
"universe, cosmos"
]
}
}
}
}
}
# test token filter
' | jq .
curl -X POST -s -H 'Content-Type: application/json' 'localhost:9200/syn_test_idx/_analyze' -d '{
"tokenizer": "standard",
"filter": ["global_synonym_filter"],
"text": "cow i phone"
}' | jq .
("i phone" not caught by synonym list.)
# update index
curl -X POST -s 'localhost:9200/syn_test_idx/_close' | jq .
curl -X PUT -s -H 'Content-Type: application/json' 'localhost:9200/syn_test_idx/_settings' -d '{
"analysis" : {
"filter": {
"test_synonym_filter":{
"type":"synonym",
"synonyms" : [
"i-pod, i pod => ipod",
"universe, cosmos",
"i-phone, i phone => iphone"
]
}
}
}
}' | jq .
curl -X POST -s 'localhost:9200/syn_test_idx/_open' | jq .
# test token filter
' | jq .
curl -X POST -s -H 'Content-Type: application/json' 'localhost:9200/syn_test_idx/_analyze' -d '{
"tokenizer": "standard",
"filter": ["global_synonym_filter"],
"text": "cow i phone"
}' | jq .
("i phone" translated into "iphone" by synonym list.)
(On an unrelated note, my zsh/YADR setup for some reason doesn't show post response bodies, hence I am piping it through jq
.)