Tokenization splits the content of a Text field into multiple tokens based on the tokenizer type that you set. You can set a tokenizer type only for a field of the Text type.
Background information
Text fields do not support sorting or aggregation. If a Text field also needs to be sorted or aggregated, use a virtual column of the Keyword type.
Text fields are commonly used for match query (MatchQuery) and match phrase query (MatchPhraseQuery). In a small number of scenarios, they are also used for term query (TermQuery), terms query (TermsQuery), prefix query (PrefixQuery), and wildcard query (WildcardQuery).
Tokenizer types
Tablestore supports five tokenizer types: single-word tokenization, delimiter tokenization, minimum semantic tokenization, maximum semantic tokenization, and fuzzy tokenization. By default, a Text field uses single-word tokenization.
All tokenizer types can be used in fuzzy query scenarios.
The following table compares the five tokenizer types across several key dimensions to help you choose the one that fits your scenario.
| Item | Single-word tokenization | Delimiter tokenization | Minimum semantic tokenization | Maximum semantic tokenization | Fuzzy tokenization |
| Index expansion | Small | Small | Small | Medium | Large |
| Impact on relevance | Weak | Weak | Medium | Relatively strong | Relatively strong |
| Applicable languages | All | All | Chinese | Chinese | All |
| Length limit | None | None | None | None | 1,024 characters |
| Recall rate | High | Low | Low | Medium | High |
The following sections describe each tokenizer type and its parameters.
Single-word tokenization (SingleWord)
The single-word tokenization type applies to all languages, including Chinese, English, and Japanese. It is the default tokenizer type for Text fields.
After you set the tokenizer type to single-word tokenization, tokenization works as follows:
Chinese text is split character by character. Chinese examples in this topic are romanized in pinyin so that the token boundaries stay visible. For example, the two-character city name "hangzhou" (Hangzhou) is split into "hang" and "zhou". A match query or match phrase query for "hang" returns rows whose content contains "hangzhou".
English letters and digits are split by spaces or punctuation marks.
If
caseSensitiveis false, tokenization is case-insensitive, and Tablestore converts all tokenized English letters to lowercase before storing them. For example, "Hang Zhou" is split into "hang" and "zhou", and a match query or match phrase query for "hang", "HANG", or "Hang" returns this row.If
caseSensitiveis true, tokenization is case-sensitive, and Tablestore preserves the original case of the tokenized English letters when storing them. For example, "Hang Zhou" is split into "Hang" and "Zhou", and a match query or match phrase query for "Hang" or "Zhou" returns this row.
Terms in which digits and English letters are joined, such as product model numbers, are also split by spaces or punctuation marks. However, by default, the digits and letters within such a term are not split apart. For example, "iphone6" remains the single token "iphone6". A match query or match phrase query returns the row only if you specify the complete term "iphone6", and a query for "iphone" returns nothing. To split the digits from the letters, set the
The following table describes the parameters for single-word tokenization.delimitWordparameter to true. In this case, "iphone6" is split into "iphone" and "6", and a match query or match phrase query for either "iphone" or "6" returns this row.
| Parameter | Description |
| caseSensitive | Specifies whether tokenization is case-sensitive. The default value is false, which means that all English letters are converted to lowercase. If you do not want the system to automatically convert English letters to lowercase and you need to preserve case, set caseSensitive to true. |
| delimitWord | Specifies whether to split the English letters from the digits in a term that joins the two. The default value is false, which means that digits and letters are not split apart. If you need to split them, set delimitWord to true. In this case, "iphone6" is split into "iphone" and "6". |
Delimiter tokenization (Split)
Tablestore provides tokenization based on a general-purpose dictionary. However, some specialized industries need custom dictionaries for tokenization. To address this, Tablestore provides delimiter tokenization, which is also called custom tokenization: you tokenize the content yourself, join the tokens with a specific delimiter, and then write the result to Tablestore.
The delimiter tokenization type applies to all languages, including Chinese, English, and Japanese.
After you configure delimiter tokenization for a Text field, the field value is split at each occurrence of the value that the delimiter parameter specifies. For example, if the field value is "badminton,table tennis,rap" and the delimiter is a comma (,), the value is split into "badminton", "table tennis", and "rap", and these tokens are indexed. A match query or match phrase query for "badminton", "table tennis", "rap", or "badminton,table tennis" returns this row.
The following table describes the parameters for delimiter tokenization.
| Parameter | Description |
| caseSensitive | Specifies whether tokenization is case-sensitive. The default value is false, which means that all English letters are converted to lowercase. If you do not want the system to automatically convert English letters to lowercase and you need to preserve case, set caseSensitive to true. This configuration is supported in Tablestore Java SDK 5.17.2 and later. |
| delimiter | The delimiter. The default value is a whitespace character. You can specify a custom delimiter. The delimiter in the field tokenization settings of a search index must be the same as the delimiter used when you write the data. Otherwise, queries may return no data. If your custom delimiter is a special character such as a number sign (#) or a tilde (~), specify it in the field tokenization settings by using the escape character \. For example, use \# for a number sign. |
Minimum semantic tokenization (MinWord)
The minimum semantic tokenization type applies to Chinese and is generally used in full-text search scenarios.
After you set the tokenizer type to minimum semantic tokenization, the system splits the content of the Text field into the smallest possible number of semantic tokens. For example, the three-character term "lihuacha" (pear blossom tea) is split into "li" and "huacha", and the resulting tokens do not overlap. As another example, the seven-character term "zhonghuarenmingongheguo" is kept as the single token "zhonghuarenmingongheguo".
Maximum semantic tokenization (MaxWord)
The maximum semantic tokenization type applies to Chinese and is generally used in full-text search scenarios.
After you set the tokenizer type to maximum semantic tokenization, the system splits out as many semantic tokens as possible. The tokens overlap one another, their accumulated length is greater than the length of the original text, and the index size also expands. For example, the three-character term "lihuacha" (pear blossom tea) is split into "lihua" and "huacha", which overlap at "hua". As another example, the seven-character term "zhonghuarenmingongheguo" is split into "zhonghuarenmingongheguo", "zhonghuarenmin", "zhonghua", "huaren", "renmingongheguo", "renmin", "gongheguo", "gonghe", and "guo".
This tokenizer type produces more tokens and therefore a higher probability of a match at query time, but the index size expands considerably. It is suitable for match query rather than match phrase query. If you use match phrase query, the query keyword is tokenized in the same way, so the position information overlaps, which may prevent the data from being found.
Fuzzy tokenization (Fuzzy)
The fuzzy tokenization type applies to all languages, including Chinese, English, and Japanese. It is generally used in scenarios that involve short text, such as titles, movie names, book names, file names, and directory names.
The fuzzy tokenization type returns results with very low latency and performs better than wildcard query, but the index size expands to a certain extent.
After you set the tokenizer type to fuzzy tokenization, the system performs N-gram tokenization on the text content, and the length of each resulting token is between minChars and maxChars. This is useful for features such as drop-down suggestions.
To implement a fuzzy query, you must use match phrase query on the column that uses fuzzy tokenization; no other query type can be used. If you have multiple types of query requirements for that column, use the virtual column feature. For more information about how to use virtual columns, see Virtual columns.
Limits
When the tokenizer type of a Text field is fuzzy tokenization, the field value cannot exceed 1,024 characters. If the value exceeds this limit, the system truncates and discards the excess characters and retains only the first 1,024 characters.
To prevent excessive expansion of the indexed data volume, the difference between the maximum and minimum token lengths (
maxChars-minChars) cannot exceed 15.
Parameters
| Parameter | Description |
| minChars | The minimum token length. The number of characters in each token combination must be greater than or equal to this value. The default value is 1. |
| maxChars | The maximum token length. The number of characters in each token combination must be less than or equal to this value. The default value is 7. |
| caseSensitive | Specifies whether tokenization is case-sensitive. The default value is false, which means that all English letters are converted to lowercase. If you do not want the system to automatically convert English letters to lowercase and you need to preserve case, set caseSensitive to true. This configuration is supported in Tablestore Java SDK 5.17.2 and later. |
Examples
Use the following examples to see how each tokenizer type splits content at index time and at query time.
At index time, the value of the tokenized string is split and indexed. At query time, the query keyword is also split, and the system attempts to match the tokens against the index that was built previously. Every token in the following tables is romanized in pinyin.
The field value is "zhonghuarenmingongheguo-guoge" (a hyphen-joined Chinese phrase romanized in pinyin). The following table shows the tokens that each tokenizer type produces at index time.
| Type | Parameters | Tokens at index time |
Single-word tokenization (SingleWord) | Default | "zhong", "hua", "ren", "min", "gong", "he", "guo", "ge" |
Delimiter tokenization (Split) | delimiter: "-" | "zhonghuarenmingongheguo", "guoge" |
Minimum semantic tokenization (MinWord) | Default | "zhonghuarenmingongheguo", "guoge" |
Maximum semantic tokenization (MaxWord) | Default | "zhonghuarenmingongheguo", "zhonghuarenmin", "zhonghua", "huaren", "renmingongheguo", "renmin", "gongheguo", "gonghe", "guo", "guoge" |
Fuzzy tokenization (Fuzzy) | minChars:1, maxChars:3 | "zhong", "zhonghua", "zhonghuaren", "hua", "huaren", "huarenmin", "ren", "renmin", "renmingong", "min", "mingong", "mingonghe", "gong", "gonghe", "gongheguo", "he", "heguo", "guo", "guoge", "ge" |
For the same field value "zhonghuarenmingongheguo-guoge", the following table shows the tokens that each tokenizer type produces at query time.
| Type | Parameters | Tokens at query time |
Single-word tokenization (SingleWord) | Default | "zhong", "hua", "ren", "min", "gong", "he", "guo", "ge" |
Delimiter tokenization (Split) | delimiter: "-" | "zhonghuarenmingongheguo", "guoge" |
Minimum semantic tokenization (MinWord) | Default | "zhonghuarenmingongheguo", "guoge" |
Maximum semantic tokenization (MaxWord) | Default | "zhonghuarenmingongheguo", "zhonghuarenmin", "zhonghua", "huaren", "renmingongheguo", "renmin", "gongheguo", "gonghe", "guo", "guoge" |
Fuzzy tokenization (Fuzzy) | Default (minChars:1, maxChars:7) | "zhonghuarenmingongheguo", "guoge" |