Hot update of IK tokenizer dictionaries

Updated at:

The IK tokenizer used by Lindorm search engine supports hot updates of the main dictionary and the stopwords dictionary. This topic uses the testAnalyzer index as an example to walk you through updating and verifying both dictionaries.

Overview

The main dictionary determines which strings are recognized as complete words, while the stopwords dictionary determines which words are excluded during indexing and querying. Both take effect after you upload a custom dictionary file and the server rebuilds the dictionary. Choose which dictionary to update based on your needs:

Dictionary

When to update

Main dictionary

When the default tokenization results do not meet your requirements, add the expected terms to a custom main dictionary.

Stopwords dictionary

Exclude words with no search intent (such as "the", "a", or "of") so that they do not participate in queries and skew the results.

Important

After you update a dictionary, existing data must be re-ingested so that the server can re-tokenize the data and rebuild the index with the updated dictionary.

Prerequisites

  • A Lindorm instance is created and the search engine is enabled. For more information, see Activation guide.

  • The IP address of the client is added to the whitelist of the Lindorm instance. For more information, see Configure a whitelist.

Update the main dictionary

Sample environment

The following example creates a testAnalyzer index and writes three documents to it.

PUT testAnalyzer
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}
POST testAnalyzer/_bulk?refresh
{ "index": {"_id": "1"} }
{ "description": "hot update improves search accuracy" }
{ "index": {"_id": "2"} }
{ "description": "the weather is hot today" }
{ "index": {"_id": "3"} }
{ "description": "system update completed successfully" }

Reproduce the issue

Use the Analyzer API to inspect the tokenization result of "hot update".

GET _analyze
{
  "text": "hot update",
  "analyzer": "ik_smart"
}

The string "hot update" is not recognized as a single complete word.

Use "hot update" as the query condition to search the testAnalyzer index.

GET testAnalyzer/_search
{
  "query": {
    "match": {
      "description": {
        "query": "hot update",
        "analyzer": "ik_smart"
      }
    }
  }
}

All three documents are returned because the query "hot update" is split into two terms — "hot" and "update" — and any document that matches either term is returned. To make "hot update" match as a single word, you must hot-update the main dictionary.

Procedure

  1. Create a main dictionary file named ext_main.dic. Each line of the file contains one term, for example:

    hot update
  2. Log on to the Lindorm console.

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

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

  5. In the left-side navigation pane, click Engine Management > Search Engine.

  6. On the tab bar at the top of the page, select Dictionary Hot Update, and then click Update Dictionary File.

  7. In the Main Dictionary section, drag your local custom dictionary file into the dashed box, or click the box to select the file, and then confirm the upload.

    Important

    The main dictionary file must be named ext_main.dic before it is uploaded.

  8. Wait for the hot update to take effect. The server periodically checks for dictionary file updates and rebuilds the dictionary when a change is detected. We recommend that you wait about 2 minutes.

  9. Use the Analyzer API to verify that the main dictionary has been updated.

    GET _analyze
    {
      "text": "hot update",
      "analyzer": "ik_smart"
    }

    If the tokenization result recognizes "hot update" as a single complete word, the main dictionary has been updated.

Verify the result

After the dictionary is updated, re-ingest the three documents from the sample environment and query the index again with the same condition "hot update".

Only the document whose _id=1 is returned. Before the update, all three documents were returned; after the update, only _id=1 is returned, which indicates that "hot update" now participates in matching as a single complete word.

Update the stopwords dictionary

Scenario

Words such as "the", "is", and "a" do not carry search intent, and letting them participate in queries can skew the results. You can update the stopwords dictionary to filter such words out.

Note

This section reuses the testAnalyzer index created in the "Update the main dictionary" section, in which "hot update" is already registered in the main dictionary. To reproduce this section on its own, first set up the sample environment and update the main dictionary as needed.

Reproduce the issue

Change the query condition to "hot update the" and run the following query.

GET testAnalyzer/_search
{
  "query": {
    "match": {
      "description": {
        "query": "hot update the",
        "analyzer": "ik_smart"
      }
    }
  }
}

This time the document whose _id=2 is also returned, because the "the" in the query matches "the weather is hot today".

Procedure

  1. Create a stopwords dictionary file named ext_stopwords.dic. Each line of the file contains one term, for example:

    the
  2. Log on to the Lindorm console.

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

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

  5. In the left-side navigation pane, click Engine Management > Search Engine.

  6. On the tab bar at the top of the page, select Dictionary Hot Update, and then click Update Dictionary File.

  7. In the Stop Word section, drag your local custom dictionary file into the dashed box, or click the box to select the file, and then confirm the upload.

    Important

    The stopwords file must be named ext_stopwords.dic before it is uploaded.

  8. Wait for the hot update to take effect. The mechanism is the same as for the main dictionary: the server rebuilds the dictionary after detecting the update. We recommend that you wait about 2 minutes.

  9. Use the Analyzer API to verify that the stopwords dictionary has been updated.

    GET _analyze
    {
      "text": "hot update the",
      "analyzer": "ik_smart"
    }

    If "the" no longer appears in the tokenization result, the stopwords dictionary has been updated.

Verify the result

After the dictionary is updated, re-ingest the three documents from the sample environment and query the index again with the same condition "hot update the".

GET testAnalyzer/_search
{
  "query": {
    "match": {
      "description": {
        "query": "hot update the",
        "analyzer": "ik_smart"
      }
    }
  }
}

Only the document whose _id=1 is returned. The document whose _id=2, which was previously returned because "the" matched "the weather is hot today", is no longer returned. This indicates that the stopwords dictionary has taken effect.