Hot update of synonyms
The search engine of Lindorm supports custom synonym dictionaries and allows for hot updates. This topic describes how to use custom synonyms and hot update the synonym dictionaries.
Prerequisites
The Lindorm search engine is enabled.
Define a synonym dictionary
car,automobile
center,middle,coreEach line represents a set of synonyms separated by commas (,). You can define multiple synonyms on a single line. For example, the second line in the preceding example defines 'center', 'middle', and 'core' as synonyms.
Upload the synonym dictionary file
Log on to the Lindorm console.
In the upper-left corner of the page, select the region where the instance is deployed.
On the Instances page, click the ID of the target instance or click View Instance Details in the Actions column for the instance.
In the left-side navigation pane, choose Search Engine.
Click the Dictionary Hot Update tab, and then click Update Dictionary File.
In the Main Dictionary box, upload your local synonym file.
NoteThe synonym dictionary filename must start with the prefix
ld_synonyms_file_.The file extension is not restricted. In this example, we use
.dicto indicate that it is a dictionary file.When you reference the dictionary file in your index configuration, use the filename without the prefix. For example, if you upload a file named
ld_synonyms_file_my_synonyms.dic, you must specifymy_synonyms.dicin the index mapping.
Specify the synonym file when creating an index
You can define synonyms for tokenized fields by using the analyzer (applied at index time) and search_analyzer (applied at query time) parameters.
Configuring synonyms in the
analyzerexpands them at index time. After you update the synonym dictionary, you must rebuild the index for the changes to take effect.Configuring synonyms in the
search_analyzerexpands them at query time. This method supports hot updates and does not require you to rebuild the index.
To enable the hot update of synonyms, define them in the search_analyzer.
PUT /test
{
"mappings" : {
"properties" : {
"content" : {
"type" : "text",
"analyzer": "ik_max_word",
"search_analyzer" : "my_synonym_analyzer"
}
}
},
"settings": {
"index.number_of_shards":4,
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms_path": "my_synonyms.dic",
"updateable": true
}
},
"analyzer": {
"my_synonym_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}Key parameters:
updateable: Must be set totrue. Otherwise, you cannot perform a hot update of synonyms for this index.synonyms_path: Set this to the part of the filename that follows theld_synonyms_file_prefix.
Verify the synonym dictionary
In the synonym dictionary you uploaded, you defined 'car' and 'automobile' as synonyms. A query for 'automobile' can now find documents that contain 'car'.
# Index a document
POST test/_doc?refresh
{
"content": "I bought a new car"
}
# Query using a synonym for 'car'
GET test/_search
{
"query": {
"match": {
"content": "automobile"
}
}
}Hot update of synonyms
When you need to add or modify synonyms, you can update the dictionary file and refresh the analyzer without having to rebuild the index.
The following example shows how to add 'vehicle' as a synonym for 'car' and 'automobile'. A query for 'vehicle' at this stage returns no results:
GET test/_search
{
"query": {
"match": {
"content": "vehicle"
}
}
}To perform a hot update of synonyms, follow these steps:
Update your local synonym dictionary file by adding 'vehicle' to the line that contains 'car,automobile'. Then, re-upload the updated dictionary file by following the steps in Upload the synonym dictionary file.
car,automobile,vehicle
center,middle,coreRun the
_refresh_search_analyzersAPI to apply the synonym updates to the target index.
POST _plugins/_refresh_search_analyzers/testRun the query again. The query now returns the document, which indicates that the hot update of synonyms was successful.
GET test/_search
{
"query": {
"match": {
"content": "vehicle"
}
}
}Update synonyms for an existing index
To add a synonym dictionary to an existing index, follow these steps.
Close the index.
WarningThis operation makes the index unavailable for read and write operations. We recommend that you perform this action during off-peak hours.
POST test/_closeUpdate the index settings to add an
analyzerdefinition that uses the synonym dictionary.PUT /test/_settings { "analysis": { "filter": { "my_synonym_filter": { "type": "synonym", "synonyms_path": "my_synonyms.dic", "updateable": true } }, "analyzer": { "my_synonym_analyzer": { "type": "custom", "tokenizer": "ik_max_word", "filter": [ "lowercase", "my_synonym_filter" ] } } } }Update the index mappings to add a
search_analyzerdefinition to the fields that need to reference the synonym analyzer.PUT test/_mapping { "properties" : { "content" : { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "my_synonym_analyzer" } } }Reopen the index and run a query to verify that the synonym dictionary is effective.
POST test/_open GET test/_search { "query": { "match": { "content": "automobile" } } }