Dynamically update synonyms

Updated at:

Alibaba Cloud Elasticsearch Serverless (ES Serverless) applications include the elasticsearch-analysis-dynamic-synonym plugin by default, which lets you dynamically load synonym files. This topic describes how to dynamically update synonyms in an ES Serverless application.

Procedure

  1. Prepare a synonym file.

    Create a synonym file in .txt format that uses UTF-8 encoding. Upload the file to Object Storage Service (OSS). The content of the synonym file is as follows:

    tomato,tomato,cherry tomato,tomato
    potato,potato,potato,potato
    KFC,KFC
  2. Log on to the Kibana console of your Alibaba Cloud ES Serverless application.

    For more information, see Use the Kibana console to manage a Serverless application.

  3. In the Kibana console, click 菜单.png and then choose Management > Dev Tools.

  4. On the Console tab, execute the following command to create an index and configure its settings. In the filter, specify dynamic_synonym and use synonyms_path to load the remote dictionary.

    # Configure settings and mappings when creating an index.
    # Call dynamic_synonym in the filter and use synonyms_path to load the remote dictionary.
    
    PUT /test
    {
      "settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "my_analyzer": {
                "type": "custom",
                "tokenizer": "ik_smart",
                "filter": ["remote_synonym"]
              }
            },
            "filter": {
              "remote_synonym" : {
                "type" : "dynamic_synonym",
                "synonyms_path" : "https://****/synonym.txt",
                "interval": 30
              }
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "title": {
            "type": "text",
            "analyzer": "my_analyzer"
          }
        }
      }
    }

    The following table describes the parameters in the filter. For more information about the parameters, see Dynamic Synonym for ElasticSearch.

    Parameter

    Description

    type

    The type of the dictionary file. Valid values are dynamic_synonym and dynamic_synonym_graph.

    synonyms_path

    The path or URL of the dictionary configuration file. To get the URL of the synonym file in OSS, see Use a signed URL to download or preview a file.

    Note

    Store the dictionary file in an OSS bucket that is in the same region as your Serverless application.

    interval

    The interval at which to refresh the dictionary. The default value is 60. The unit is seconds.

  5. Verify the synonym configuration.

    1. Use _analyze to check the tokenization result for KFC.

      # Verify the synonym configuration.
      
      GET test/_analyze
      {
        "analyzer": "my_analyzer",
        "text": "KFC"
      }
      

      If the command is successful, the following result is returned.

      # Response
      {
        "tokens" : [
          {
            "token" : "KFC",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "SYNONYM",
            "position" : 0
          },
          {
            "token" : "kfc",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "ENGLISH",
            "position" : 0
          }
        ]
      }
      
    2. Update the content of the synonym file in OSS.

      The content of the synonym file is as follows:

      tomato,tomato,small tomato,tomato
      potato,potato,potato,potato
      KFC,KFC,Kentucky Fried Chicken
    3. In Kibana, execute the following code to verify the tokenization result.

      # Verify the synonym configuration.
      
      GET test/_analyze
      {
        "analyzer": "my_analyzer",
        "text": "KFC"
      }
      

      If the command is successful, the following result is returned.

      {
        "tokens" : [
          {
            "token" : "kfc",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "ENGLISH",
            "position" : 0
          },
          {
            "token" : "KFC",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "SYNONYM",
            "position" : 0
          },
          {
            "token" : "kentucky",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "SYNONYM",
            "position" : 0
          },
          {
            "token" : "fried",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "SYNONYM",
            "position" : 1
          },
          {
            "token" : "chicken",
            "start_offset" : 0,
            "end_offset" : 3,
            "type" : "SYNONYM",
            "position" : 2
          }
        ]
      }

FAQ

Q: What do I do if the dynamic synonym address is incorrect?

A: Follow these steps:

  1. Verify the configuration parameters:

    You can check your index mapping or analyzer settings to ensure that plugin-related parameters, such as synonym_path, are configured correctly.

  2. Check the file encoding. Ensure that the synonym file uses the UTF-8 encoding format.

  3. Confirm the synonym address settings.

    Check your plugin configuration to ensure that the URL for the synonym dictionary file is correct and that the file is accessible. The synonym dictionary file is stored in Alibaba Cloud Object Storage Service (OSS). For more information about how to obtain the file URL, see Use a presigned URL to download or preview a file.

  4. Contact Technical Support.

    If the problem persists after you perform the preceding troubleshooting steps, or if you are unsure how to perform operations in the Alibaba Cloud ES Serverless environment, submit a ticket to contact Alibaba Cloud technical support. In the ticket, provide detailed error messages and the troubleshooting steps that you have taken. Alibaba Cloud technical support will provide you with targeted assistance.