Overview
The database stores its underlying data in the data files organized by columns. Each data file contains the full-text inverted index based on the indexed columns. The values in the indexed columns are tokenized into individual words. Each word after tokenization is treated as an index entry, mapping to the row number where the word appears. Currently supported tokenization methods for English tokenization, Chinese tokenization, multilingual tokenization, and no tokenization. For example, if a data row contains “hello world” and its row number is 123, the full-text inverted index builds index entries based on this tokenization result and row number: hello->123, world->123. During full-text searches, the database can locate index entries containing the search keywords using full-text inverted indexes, and then quickly find the row numbers where the keywords appear, significantly reducing the number of data rows that need to be scanned.Basic operation
Create full-text inverted index
Note When creating a full-text inverted index for a table, you must disable thereplicated_storage feature for the table.
- For v4.0 and later, this feature is automatically disabled when you create the index.
- for versions earlier than v4.0, you must manually set the table property
replicated_storagetofalse.
Create full-text Inverted Index at table creation
Creating a full-text inverted index on columnv with English tokenization.
- The
parserparameter specifies the tokenization method. Supported values and descriptions are as follows:none(default): no tokenization. The entire row of data in the indexed column is treated as a single index item when the full-text inverted index is constructed.english: English tokenization. This tokenization method typically tokenizing at any non-alphabetic character. Also, uppercase English letters are converted to lowercase. Therefore, keywords in the query conditions need to be lowercase English rather than uppercase English to leverage the full-text inverted index to locate data rows.chinese: Chinese tokenization. This tokenization method uses the CJK Analyzer in CLucene for tokenization.standard: Multilingual tokenization. This tokenization method provides grammar based tokenization (based on the Unicode Text Segmentation algorithm) and works well for most languages and cases of mixed languages, such as Chinese and English. For example, this tokenization method can distinguishes between Chinese and English when these two languages coexist. After tokenizing English, it converts uppercase English letters to lowercase. Therefore, keywords in the query conditions need to be lowercase English rather than uppercase English to leverage the full-text inverted index to locate data rows.
- The data type of the indexed column must be CHAR, VARCHAR, or STRING.
Add full-text inverted index after table creation
After table creation, you can add a full-text inverted index usingALTER TABLE ADD INDEX or CREATE INDEX.
Manage full-text inverted index
View full-text inverted index
ExecuteSHOW CREATE TABLE to view full-text inverted indexes.
Delete full-text inverted index
ExecuteALTER TABLE ADD INDEX or DROP INDEX to delete full-text inverted indexes.
Accelerate queries by full-text inverted index
After creating a full-text inverted index, you need to ensure that the system variableenable_gin_filter is enabled, so the inverted index can accelerate queries. Also, you need to consider whether the index column values are tokenized to determine which queries can be accelerated.
Supported queries when indexed column is tokenized
When a full-text inverted index column is enabled with tokenization (parser = standard | english | chinese), only the MATCH, MATCH_ANY, or MATCH_ALL predicates are supported for filtering. The supported formats are:
<col_name> (NOT) MATCH '%keyword%'<col_name> (NOT) MATCH_ANY 'keyword1, keyword2'<col_name> (NOT) MATCH_ALL 'keyword1, keyword2'
NULL are not returned by NOT MATCH, NOT MATCH_ANY, or NOT MATCH_ALL.
-
Create a table and insert a few rows of test data.
-
Use the
MATCHpredicate for querying.
-
Query data rows whose
valuecolumn contains the keywordstarrocks. -
Retrieve data rows whose
valuecolumn contains the keyword starting withdata.
- Use the
MATCH_ANYpredicate for querying.
-
Query data rows whose
valuecolumn contains the keyworddatabaseordata.
- Use the
MATCH_ALLpredicate for querying.
-
Query data rows whose
valuecolumn contains both the keyworddatabaseanddata.
-
During queries, keywords can be matched fuzzily using
%, in the format of%keyword%. However, the keyword must contain a part of a word. For example, if the keyword isstarrocks, it cannot match the wordstarrocksbecause it contains spaces. -
If English or multilingual tokenization is used to construct the full-text inverted index, uppercase English words are converted to lowercase when the full-text inverted index is actually stored. Therefore, during queries, keywords need to be lowercase instead of uppercase to utilize the full-text inverted index to locate data rows.
-
The
MATCH,MATCH_ANY, orMATCH_ALLpredicate in the query conditions must be used as a pushdown predicate, so it must be in the WHERE clause and be performed against the indexed column. Take the following table and test data as an example:The following query statements do not meet the requirement:-
Because the
MATCH,MATCH_ANY, orMATCH_ALLpredicate in the query statement is not in the WHERE clause, it can not be pushed down, resulting in a query error. -
Because the column
value_testagainst which theMATCH,MATCH_ANY, orMATCH_ALLpredicate in the query statement is performed is not an indexed column, the query fails.
-
Because the
Supported queries when indexed column is not tokenized
If the full-text inverted index does not tokenize the indexed column, that is,'parser' = 'none', all pushdown predicates in the query conditions listed below can be used for data filtering using the full-text inverted index:
-
Expression predicates: (NOT) LIKE, (NOT) MATCH, (NOT) MATCH_ANY, (NOT) MATCH_ALL
Note
- In this case,
MATCHis semantically equivalent toLIKE. MATCHandLIKEonly support the format(NOT) <col_name> MATCH|LIKE '%keyword%'. Thekeywordmust be a string literal and does not support expressions. Note that ifLIKEdoes not meet this format, even if the query can be executed normally, it will degrade to a query that does not use the full-text inverted index to filter data.
- In this case,
-
Regular predicates:
==,!=,<=,>=,NOT IN,IN,IS NOT NULL,NOT NULL
Verify whether a query is accelerated by the full-text inverted index
After executing the query, you can view the detailed metricsGinFilterRows and GinFilter in the scan node of the Query Profile to see the number of rows filtered and the filtering time using the full-text inverted index.
Built-in inverted index
Since v4.1, the database provides a built-in inverted index implementation in addition to the default CLucene-based implementation. The built-in implementation is the database’ native inverted index built on top of bitmap index, and supports both shared-nothing and shared-data clusters. Note The CLucene-based inverted index implementation is not supported in shared-data clusters. For shared-data clusters, you must use the built-in implementation.Implementation comparison
You can explicitly specify the implementation type using the
imp_lib parameter when creating an index. If not specified, the system automatically selects the appropriate implementation based on the cluster mode:
- In shared-nothing clusters, the default is CLucene.
- In shared-data clusters, the default is built-in (CLucene is not supported).
Create built-in inverted index
Create at table creation
Add after table creation
Built-in inverted index in shared-data clusters
In shared-data clusters, the built-in implementation is automatically selected even ifimp_lib is not specified. You can create the index using the same syntax as for a regular inverted index:
"imp_lib" = "builtin" for clarity:
dict_gram_num
Thedict_gram_num parameter is available exclusively for the built-in inverted index implementation. It controls the n-gram size used to build an n-gram dictionary index on top of the inverted index dictionary, which can significantly accelerate wildcard and substring queries.
How it works
Whendict_gram_num is set to a positive integer (e.g., 2), the built-in inverted index splits each dictionary entry into n-grams (substrings of the specified character length) during index construction. For example, if dict_gram_num is set to 2 and a dictionary entry is "starrocks", the following 2-grams are generated: "st", "ta", "ar", "rr", "ro", "oc", "ck", "ks".
During a query like MATCH '%rock%', the query string "rock" is also split into 2-grams ("ro", "oc", "ck"), and the n-gram index quickly narrows down candidate dictionary entries, avoiding a full dictionary scan. This greatly improves the performance of wildcard queries, especially on columns with a large number of distinct values.
Parameter details
Guidelines for choosing dict_gram_num
- A smaller value (e.g.,
2) generates more n-grams per dictionary entry, increasing index size but providing better filtering for short query patterns. - A larger value (e.g.,
4) generates fewer n-grams, resulting in smaller index size but less effective filtering for short query patterns. - If your queries primarily use short wildcard patterns (e.g.,
%ab%), a smallerdict_gram_numvalue (such as2) is recommended. - If the query pattern is shorter than the
dict_gram_numvalue, the n-gram index cannot be used for that query and falls back to a full dictionary scan.
Examples
Create built-in inverted index withdict_gram_num
-
Create a table with a built-in inverted index and
dict_gram_numset to2. -
Insert test data.
-
Query with wildcard patterns. The n-gram dictionary index accelerates these queries.
dict_gram_num to an existing table
You can also specify dict_gram_num when adding a built-in inverted index after table creation:
lower_case
Thelower_case parameter is available exclusively for the built-in inverted index implementation with English tokenization ("parser" = "english"). It controls whether English tokens are normalized to lowercase when the index is built and when queries are matched against it.
Parameter details
true(default): Uppercase English letters are converted to lowercase both when the index is built and when query keywords are matched, so matching is case-insensitive. This preserves the existing behavior.false: Case is preserved during both indexing and matching, so matching is case-sensitive. Use this when you need to distinguish words that differ only in case, such asApple,apple, andAPPLE.