Hot update of synonyms

Updated at:

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

Define a synonym dictionary

car,automobile
center,middle,core

Each 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

  1. Log on to the Lindorm console.

  2. In the upper-left corner of the page, select the region where the instance is deployed.

  3. On the Instances page, click the ID of the target instance or click View Instance Details in the Actions column for the instance.

  4. In the left-side navigation pane, choose Search Engine.

  5. Click the Dictionary Hot Update tab, and then click Update Dictionary File.

  6. In the Main Dictionary box, upload your local synonym file.

    Note
    • The synonym dictionary filename must start with the prefix ld_synonyms_file_.

    • The file extension is not restricted. In this example, we use .dic to 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 specify my_synonyms.dic in 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 analyzer expands 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_analyzer expands 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 to true. Otherwise, you cannot perform a hot update of synonyms for this index.

  • synonyms_path: Set this to the part of the filename that follows the ld_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:

  1. 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,core
  1. Run the _refresh_search_analyzers API to apply the synonym updates to the target index.

POST _plugins/_refresh_search_analyzers/test
  1. Run 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.

  1. Close the index.

    Warning

    This operation makes the index unavailable for read and write operations. We recommend that you perform this action during off-peak hours.

    POST test/_close
  2. Update the index settings to add an analyzer definition 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"
            ]
          }
        }
      }
    }
  3. Update the index mappings to add a search_analyzer definition 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"
        }
      }
    }
  4. 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"
        }
      }
    }