Manage and use dictionaries

Updated at:

Alibaba Cloud Elasticsearch Serverless (abbreviated as ES Serverless) lets you upload and manage dictionaries for more flexible and efficient search capabilities. This feature lets you customize and adjust tokenization strategies based on your business needs, helping you build intelligent, high-performance search and analytics systems.

Prerequisites

You have created a search-as-a-service application.

Dictionary management

  1. Log on to the Elasticsearch Serverless console and switch to the desired region in the top navigation bar.

  2. In the navigation pane on the left, click Application Management.

  3. Click the name of the target application.

  4. In the navigation pane on the left, click Dictionary Management.

    In the dictionary list, you can view information such as the Dictionary File Name, Dictionary Type, Dictionary Source, File Size, and Number of Entries. You can also add, update, download, or delete dictionary files.

Manage dictionaries

Add a custom dictionary

Click Add Custom Dictionary and configure the parameters in the dialog box that appears.

Parameter

Description

Dictionary Source

  • Upload File: Upload a dictionary from your local machine.

  • Add OSS Path: Upload a dictionary from an Object Storage Service (OSS) bucket.

Dictionary Type

  • IK analysis

  • IK stopword

    Note

    The system automatically merges an uploaded stopword dictionary into the main analysis dictionary.

Select OSS Bucket

Select an OSS Bucket that is in the same region as your application and contains the target dictionary file.

Select File

  • If Dictionary Source is set to Upload File, click or drag to upload a local dictionary file.

  • If Dictionary Source is set to Add OSS Path:

    • The directory name must contain es-serverless, or you must add the es-serverless:es-serverless tag to the OSS Bucket.

    • Add dictionary file: Select the OSS directory that contains the dictionary and then select the dictionary file.

Note
  • Only .dic dictionary files with UTF-8 encoding are supported.

  • If you upload a dictionary file with the same name and type as an existing one, the system overwrites the existing dictionary file.

Update a dictionary

In the Actions column for the target dictionary, click Update. In the dialog box that appears, modify the dictionary source and select the updated dictionary file.

Note
  • You cannot change the dictionary name or type.

  • The new dictionary file must have the same name as the original one.

Download a dictionary

In the Actions column for the target dictionary, click Download to save the corresponding .txt or .dic file locally.

Delete a dictionary

  • Deletion: In the Actions column for the target dictionary, select Delete and then click Confirm.

  • Impact of deletion: Deleting a custom dictionary, such as an extension or stopword dictionary for the IK analyzer, affects search accuracy and tokenization behavior in the following ways.

    • Change in tokenization granularity

      • Newly written data: After a dictionary is deleted, newly indexed documents will no longer be tokenized by using the rules from that dictionary. For example, after the financial terminology dictionary is deleted, the term "quantitative trading" may be split into individual characters such as "q", "u", "a", "n" instead of being recognized as a complete term.

      • For historical data: Deleting a dictionary does not affect the tokenization of already indexed documents because tokenization occurs at index time. However, if search queries use the new tokenization rules, the recall rate might decrease.

    • Degraded search performance

      • Decreased recall rate: Search keywords may fail to match compound words defined in the original dictionary. For example, after you remove machine learning from the dictionary, a search for machine learning may fail to recall documents that contain this term (the search term will be split into 'machine' and 'learning').

      • Precision fluctuation: After the stopword dictionary is deleted, stopwords (such as "the", "a") are included in searches again, which increases noise in the results.

    • Shifts in relevance scores: Calculations such as term frequency (TF) and inverse document frequency (IDF) depend on tokenization results. Deleting a dictionary can alter the scoring logic, which affects the relevance score and the quality of search rankings.

Important

Recommendations:

  • Do not delete dictionaries unless necessary. We recommend updating the dictionary content to adjust your tokenization strategy.

  • If you must delete a dictionary and need to fix historical data, you can ensure data tokenization consistency by first updating the dictionary and then running the _reindex operation on the old index.

Use IK analysis dictionaries

Note

You can run the commands in this topic in the Kibana console. For information about how to log on to the Kibana console, see Log on to the Kibana console.

Prerequisites

You have uploaded one or more custom IK analysis dictionaries or stopword dictionaries.

Test tokenization

Run the following request to test the tokenization.

# Test tokenization by specifying the analyzer as "ik_smart" or "ik_max_word".

GET /_analyze
{
  "text": "National Anthem of the People's Republic of China",
  "analyzer": "ik_smart"
}

The request returns the following result.

{
  "tokens" : [
    {
      "token" : "national anthem",
      "start_offset" : 0,
      "end_offset" : 15,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "people's republic of china",
      "start_offset" : 23,
      "end_offset" : 49,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

Insert data and verify tokenization

Run the following requests to insert data and verify tokenization.

# Create an index, specify "ik_max_word" as the analyzer for a field in the mapping, 
# insert data, and then run an analysis to verify the tokenization.

PUT /my_index
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

# Insert data.
POST /my_index/_doc/
{
  "text": "I am Chinese and I love my country"
}


GET /my_index/_analyze
{
  "analyzer": "ik_max_word",
  "text": "I am Chinese and I love my country"
}

The request returns the following result.

{
  "tokens" : [
    {
      "token" : "i",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "ENGLISH",
      "position" : 0
    },
    {
      "token" : "am",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "ENGLISH",
      "position" : 1
    },
    {
      "token" : "chinese",
      "start_offset" : 5,
      "end_offset" : 12,
      "type" : "ENGLISH",
      "position" : 2
    },
    {
      "token" : "and",
      "start_offset" : 13,
      "end_offset" : 16,
      "type" : "ENGLISH",
      "position" : 3
    },
    {
      "token" : "i",
      "start_offset" : 17,
      "end_offset" : 18,
      "type" : "ENGLISH",
      "position" : 4
    },
    {
      "token" : "love",
      "start_offset" : 19,
      "end_offset" : 23,
      "type" : "ENGLISH",
      "position" : 5
    },
    {
      "token" : "my",
      "start_offset" : 24,
      "end_offset" : 26,
      "type" : "ENGLISH",
      "position" : 6
    },
    {
      "token" : "country",
      "start_offset" : 27,
      "end_offset" : 34,
      "type" : "ENGLISH",
      "position" : 7
    }
  ]
}