elasticsearch


How to apply synonyms at query time instead of index time in Elasticsearch


According to the elasticsearch reference documentation, it is possible to:
Expansion can be applied either at index time or at query time. Each has advantages (⬆)︎ and disadvantages (⬇)︎. When to use which comes down to performance versus flexibility.
The advantages and disadvantages all make sense and for my specific use I want to make use of synonyms at query time. My use case is that I want to allow admin users in my system to curate these synonyms without having to reindex everything on an update. Also, I'd like to do it without closing and reopening the index.
The main reason I believe this is possible is this advantage:
(⬆)︎ Synonym rules can be updated without reindexing documents.
However, I can't find any documentation describing how to apply synonyms at query time instead of index time.
To use a concrete example, if I do the following (example stolen and slightly modified from the reference), it seems like this would apply the synonyms at index time:
/* NOTE: This was all run against elasticsearch 1.5 (if that matters; documentation is identical in 2.x) */
// Create our synonyms filter and analyzer on the index
PUT my_synonyms_test
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"queen,monarch"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}
// Create a mapping that uses this analyzer
PUT my_synonyms_test/rulers/_mapping
{
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string",
"analyzer": "my_synonyms"
}
}
}
// Some data
PUT my_synonyms_test/rulers/1
{
"name": "Elizabeth II",
"title": "Queen"
}
// A query which utilises the synonyms
GET my_synonyms_test/rulers/_search
{
"query": {
"match": {
"title": "monarch"
}
}
}
// And we get our expected result back:
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.4142135,
"hits": [
{
"_index": "my_synonyms_test",
"_type": "rulers",
"_id": "1",
"_score": 1.4142135,
"_source": {
"name": "Elizabeth II",
"title": "Queen"
}
}
]
}
}
So my question is: how could I amend the above example so that I would be using the synonyms at query time?
Or am I barking up completely the wrong tree and can you point me somewhere else please? I've looked at plugins mentioned in answers to similar questions like http://stackoverflow.com/a/34210587/2240218 and http://stackoverflow.com/a/18481495/2240218 but they all seem to be a couple of years old and unmaintained, so I'd prefer to avoid these.
Simply use search_analyzer instead of analyzer in your mapping and your synonym analyzer will only be used at search time
PUT my_synonyms_test/rulers/_mapping
{
"properties": {
"name": {
"type": "string"
},
"title": {
"type": "string",
"search_analyzer": "my_synonyms" <--- change this
}
}
}

Related Links

Do two equal documents in elasticsearch double the needed disc space
In logstash/ElasticSearch/Kibana, how do I augment records following a particular stateful request?
How to know which shard is overloaded in Elasticsearch?
ElasticSearch - searching different doc_types with the same field name but different analyzers
ELK Type Conversion - Not a number but a string
How to get the definitiion of a search analyzer of an index in elasticsearch
ElasticSearch query using match or term?
How to combine aggregations in ElasticSearch/Kibana?
ElasticSerach cluster performance
Nxlog unable to send eventlog after certain time
Sort elasticsearch search hits by document count
Elastic search date range max, min date
Elastic search river mongodb _meta returning action not found error
Seeing many open Elasticsearch connections even after using singleton pattern
What would be a good approach for sending logs from multiple servers a centralized logging server?
does elasticsearch support queries of queries?

Categories

HOME
amazon-web-services
nam
operating-system
plaid
ios-simulator
portable-class-library
iptables
implicit-conversion
bittorrent
phpmqtt
mailmerge
responsivevoice
feature-extraction
dma
android-5.1.1-lollipop
fosuserbundle
string-formatting
clr
unity-container
liferay-6.2
batch-rename
mime-types
acl
tortoisehg
bonita
copy-protection
tilemill
toad
python-2.x
quick-nimble
universal
ms-media-foundation
markov-chains
directfb
bean-validation
hidden-markov-models
cheat-engine
linq.js
sbjson
praat
brightscript
knockout-2.0
nonlinear-functions
nusoap
subforms
exc-bad-access
hmmlearn
streamwriter
maven-release-plugin
android-syncadapter
strpos
rails-engines
textblock
pack
uitest
roxygen2
html-escape-characters
utf8-decode
producer
quantify
angular-ui-select
two.js
nodeload
pageviews
cidr
mft
boinc
indesign-server
.net-remoting
flv
red-system
handshake
linkbutton
uipangesturerecognizer
yuidoc
will-paginate
boost-foreach
space-partitioning
jmenu
3des
bll
libxslt
ccl
ccnet-config
eventaggregator
projectpier
wmd-editor
isapi-redirect
community-server
account-management

Resources

Encrypt Message