Repository navigation
full-text search optimization of ngm, multi-column and filtering #24749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release-8.5
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,22 @@ The full-text search feature in TiDB provides the following capabilities: | |
|
|
||
| - **Order by relevance**: the search result can be ordered by relevance using the widely adopted [BM25 ranking](https://en.wikipedia.org/wiki/Okapi_BM25) algorithm. | ||
|
|
||
| - **Fully compatible with SQL**: all SQL features, such as pre-filtering, post-filtering, grouping, and joining, can be used with full-text search. | ||
| - **Multi-column search**: you can define multiple scored columns in one full-text index and search across them in a single query. BM25 scores are fused at the index level. | ||
|
|
||
| - **Filter pushdown**: you can add filter columns (such as tenant IDs, status, and file paths) to a full-text index. Filter conditions on these columns are evaluated during the index scan, without accessing the table rows. | ||
|
|
||
| - **Substring matching**: with the NGRAM parser, queries can match prefixes and substrings, such as matching `panic_handler` when searching for `panic`. | ||
|
|
||
| - **SQL-native**: you can combine full-text search with filter conditions, aggregation, inner joins, and subqueries in ordinary SQL statements, without maintaining a separate search engine. | ||
|
|
||
| > **Tip:** | ||
| > | ||
| > For Python usage, see [Full-Text Search with Python](/ai/guides/vector-search-full-text-search-python.md). | ||
| > | ||
| > To use full-text search and vector search together in your AI apps, see [Hybrid Search](/ai/guides/vector-search-hybrid-search.md). | ||
| > - For Python usage, see [Full-Text Search with Python](/ai/guides/vector-search-full-text-search-python.md). | ||
| > - To use full-text search and vector search together in your AI apps, see [Hybrid Search](/ai/guides/vector-search-hybrid-search.md). | ||
|
|
||
| ## Get started | ||
|
|
||
| Full-text search is still in the early stages, and we are continuously rolling it out to more customers. Currently, full-text search is only available on {{{ .starter }}} in the following regions: | ||
| Full-text search is still in the early stages, and we are continuously rolling it out to more customers. Currently, full-text search is **available only on {{{ .starter }}}** in the following regions: | ||
|
|
||
| - AWS: `Oregon (us-west-2)`, `N. Virginia (us-east-1)`, `Tokyo (ap-northeast-1)`, `Frankfurt (eu-central-1)`, and `Singapore (ap-southeast-1)` | ||
|
|
||
|
|
@@ -65,6 +70,8 @@ CREATE TABLE stock_items( | |
| -- You might insert some data here. | ||
| -- The full-text index can be created even if data is already in the table. | ||
|
|
||
| -- ADD_COLUMNAR_REPLICA_ON_DEMAND is optional. If you omit it, | ||
| -- make sure that a TiFlash replica is already created for the table. | ||
| ALTER TABLE stock_items ADD FULLTEXT INDEX (title) WITH PARSER MULTILINGUAL ADD_COLUMNAR_REPLICA_ON_DEMAND; | ||
| ``` | ||
|
|
||
|
|
@@ -74,40 +81,11 @@ The following parsers are accepted in the `WITH PARSER <PARSER_NAME>` clause: | |
|
|
||
| - `MULTILINGUAL`: supports multiple languages, including English, Chinese, Japanese, and Korean. | ||
|
|
||
| ### Manage full-text indexes | ||
|
|
||
| When creating a full-text index, specifying an index name is optional. If you do not specify one, TiDB uses the name of the first indexed column as the index name by default. | ||
|
|
||
| ```sql | ||
| -- Without specifying an index name, TiDB uses the first indexed column name ("title") as the index name | ||
| ALTER TABLE stock_items ADD FULLTEXT INDEX (title) WITH PARSER MULTILINGUAL; | ||
|
|
||
| -- Specifying an index name | ||
| ALTER TABLE stock_items ADD FULLTEXT INDEX ft_title (title) WITH PARSER MULTILINGUAL; | ||
| ``` | ||
|
|
||
| **View existing index names:** | ||
|
|
||
| ```sql | ||
| -- The Key_name column shows the index name | ||
| SHOW INDEX FROM stock_items; | ||
|
|
||
| -- Or query INFORMATION_SCHEMA | ||
| SELECT INDEX_NAME, COLUMN_NAME, INDEX_TYPE | ||
| FROM INFORMATION_SCHEMA.STATISTICS | ||
| WHERE TABLE_SCHEMA = 'your_database' AND TABLE_NAME = 'stock_items'; | ||
| ``` | ||
|
|
||
| **Drop a full-text index:** | ||
|
|
||
| ```sql | ||
| -- Use SHOW INDEX to confirm the index name first | ||
| ALTER TABLE stock_items DROP INDEX title; | ||
| ``` | ||
| - `NGRAM`: builds character-level n-grams so that queries can match prefixes and substrings. See [The NGRAM parser](/ai/reference/full-text-search-index.md#the-ngram-parser) for parameters. | ||
|
|
||
| #### Specify an index name | ||
|
|
||
| In both `CREATE TABLE` and `ALTER TABLE` statements, you can specify an index name after `FULLTEXT INDEX` or `FULLTEXT KEY`: | ||
| In both `CREATE TABLE` and `ALTER TABLE` statements, you can specify an index name after `FULLTEXT INDEX` or `FULLTEXT KEY`: | ||
|
|
||
| ```sql | ||
| -- Specifying a name in CREATE TABLE | ||
|
|
@@ -124,6 +102,20 @@ ALTER TABLE users ADD FULLTEXT INDEX ft_name (name) WITH PARSER STANDARD; | |
| CREATE FULLTEXT INDEX ft_name ON users (name) WITH PARSER STANDARD; | ||
| ``` | ||
|
|
||
| #### Create a multi-column full-text index | ||
|
|
||
| A full-text index can contain multiple scored columns. Searching across them in one query fuses the BM25 scores at the index level, which replaces scanning one single-column index per column and merging results with `UNION ALL`. | ||
|
|
||
| ```sql | ||
| ALTER TABLE articles ADD FULLTEXT INDEX ft_article (title, body) WITH PARSER MULTILINGUAL; | ||
|
|
||
| SELECT * FROM articles | ||
| WHERE fts_match_word('database', title, body) | ||
| ORDER BY fts_match_word('database', title, body) DESC LIMIT 10; | ||
| ``` | ||
|
|
||
| Columns in one call are combined with OR semantics: a document matches if any of the columns matches the query. For AND semantics and more details, see [Multi-column search](/ai/reference/full-text-search-functions-tidb.md#multi-column-search). | ||
|
|
||
| ### Insert text data | ||
|
|
||
| Inserting data into a table with a full-text index is identical to inserting data into any other tables. | ||
|
|
@@ -189,6 +181,10 @@ SELECT * FROM stock_items | |
| +------+---------------------------------------------------------------------------------------------------------------+ | ||
| ``` | ||
|
|
||
| > **Note:** | ||
| > | ||
| > When you order results by `fts_match_word()`, the statement must include a `LIMIT` clause. To return the complete ranking, specify a large `LIMIT` value. See [Query limitations](/ai/reference/full-text-search-limitations.md#query-limitations). | ||
|
|
||
| **Example: count the number of documents matching the user query** | ||
|
|
||
| ```sql | ||
|
|
@@ -204,9 +200,9 @@ SELECT COUNT(*) FROM stock_items | |
|
|
||
| #### Multi-word search: tokenization and query semantics | ||
|
|
||
| When you use `fts_match_word()`, the query string is tokenized according to the parser's rules, and each token is matched independently. | ||
| When you use `fts_match_word()`, the query string is tokenized according to the parser's rules, and each token is matched independently. | ||
|
|
||
| The STANDARD parser tokenizes strings into words using spaces and punctuation as delimiters. The MULTILINGUAL parser tokenizes strings according to language-specific segmentation rules. | ||
| The STANDARD parser tokenizes strings into words using spaces and punctuation as delimiters. The MULTILINGUAL parser tokenizes strings according to language-specific segmentation rules. | ||
|
|
||
| ```sql | ||
| -- This query is tokenized into two tokens: "Alice" and "Smith" | ||
|
|
@@ -221,15 +217,53 @@ SELECT * FROM users WHERE fts_match_word('Alice Smith', name); | |
| SELECT * FROM users WHERE fts_match_word('Alice Smith', name); | ||
| ``` | ||
|
|
||
| A common misconception is that `fts_match_word('Alice X', name)` treats `"Alice X"` as a single entity for exact matching. In reality, it is tokenized into `Alice` and `X`, using OR semantics. Because `X` is a very short query term, it can match many irrelevant documents. Avoid using very short query terms or single letters. | ||
| A common misconception is that `fts_match_word('Alice X', name)` treats `"Alice X"` as a single entity for exact matching. In reality, it is tokenized into `Alice` and `X`, using OR semantics. Because `X` is a very short query term, it can match many irrelevant documents. Avoid using very short query terms or single letters. | ||
|
|
||
| > **Note:** | ||
| > | ||
| > TiDB full-text search does not support exact phrase matching, where all query tokens must appear consecutively and in the specified order. | ||
|
|
||
| > **Note:** | ||
| > | ||
| > TiDB full-text search does not support exact phrase matching, where all query tokens must appear consecutively and in the specified order. | ||
| #### Prefix and substring search | ||
|
|
||
| #### Prefix search | ||
| To match prefixes or substrings, use a full-text index with the `NGRAM` parser. To match path prefixes such as `/src/`, use a filter column with the `path_hierarchy` attribute. See [Filter pushdown](#filter-pushdown). | ||
|
|
||
| **Not supported.** | ||
| The `STANDARD` and `MULTILINGUAL` parsers match complete tokens only. For partial-recall scenarios such as code search, create a full-text index with the `NGRAM` parser to match prefixes and substrings: | ||
|
|
||
| ```sql | ||
| ALTER TABLE code_files ADD FULLTEXT INDEX idx_fts_ngram (content_text) | ||
| WITH PARSER NGRAM(min_gram=3, max_gram=3); | ||
|
|
||
| -- Matches HandleRequest, RequestHandler, and handle_error | ||
| SELECT /*+ USE_INDEX(code_files, idx_fts_ngram) */ * | ||
| FROM code_files | ||
| WHERE fts_match_word('handle', content_text) | ||
| ORDER BY fts_match_word('handle', content_text) DESC LIMIT 10; | ||
| ``` | ||
|
|
||
| A table can have multiple full-text indexes, and the same column can participate in several of them with different parsers. To search a column with a specific parser, follow the column with a `WITH PARSER parser_name` clause in the query. Alternatively, when each parser has its own index, select the index with the `USE_INDEX` hint, because the hinted index determines the parser and therefore the result set. Without either, the optimizer selects an index automatically and does not compare parsers. For parameters and query semantics, see [The NGRAM parser](/ai/reference/full-text-search-index.md#the-ngram-parser), [Select a parser for a scored column](/ai/reference/full-text-search-functions-tidb.md#select-a-parser-for-a-scored-column), and [Choose a full-text index at query time](/ai/reference/full-text-search-functions-tidb.md#choose-a-full-text-index-at-query-time). | ||
|
|
||
| #### Filter pushdown | ||
|
|
||
| In addition to scored columns, a full-text index can contain filter columns. Filter conditions on these columns are evaluated during the index scan, so you get correct scoped Top-K results instead of filtering after a global Top-K. Filter columns are defined with the column-property syntax: | ||
|
|
||
| ```sql | ||
| ALTER TABLE files ADD FULLTEXT INDEX idx_fts ( | ||
| content_text WITH (multilingual), | ||
| path WITH (exact, path_hierarchy), | ||
| ext WITH (exact) | ||
| ); | ||
|
|
||
| SELECT * FROM files | ||
| WHERE fts_match_word('database', content_text) | ||
| AND path LIKE '/src/%' | ||
| AND ext IN ('go', 'rs') | ||
| ORDER BY fts_match_word('database', content_text) DESC LIMIT 10; | ||
| ``` | ||
|
|
||
| - The `exact` attribute supports `=` and `IN` matching during the index scan. It is suitable for tenant IDs, status, tags, and other low-cardinality columns. | ||
| - The `path_hierarchy` attribute supports hierarchical prefix matching such as `path LIKE '/src/%'`, where the prefix aligns with the `/` delimiter boundary. | ||
|
|
||
| For the full attribute reference, syntax rules, and pushdown limitations, see [Full-Text Search Index](/ai/reference/full-text-search-index.md) and [Filter pushdown limitations](/ai/reference/full-text-search-limitations.md#filter-pushdown-limitations). | ||
|
|
||
| #### Effect of repeated terms on relevance scores | ||
|
|
||
|
|
@@ -244,7 +278,7 @@ In this example, a document matching `Alice` receives twice the weight contribut | |
|
|
||
| #### Relevance scoring algorithm | ||
|
|
||
| TiDB full-text search uses the **BM25Tantivy** algorithm to calculate relevance scores. This algorithm is a variant of the classic BM25 (Okapi BM25) algorithm that uses Count-Min Sketch to approximate document frequency (DF) for improved performance. | ||
| TiDB full-text search uses the **BM25Tantivy** algorithm to calculate relevance scores. This algorithm is a variant of the classic BM25 (Okapi BM25) algorithm that uses Count-Min Sketch to approximate document frequency (DF) for improved performance. | ||
|
|
||
| **BM25 formula (standard form):** | ||
|
|
||
|
|
@@ -267,9 +301,40 @@ TiDB's implementation uses fixed values of `k1 = 1.2` and `b = 0.75`, which are | |
|
|
||
| The returned score is a non-negative floating-point number. A higher value indicates higher relevance to the query. Scores are not directly comparable across different datasets. | ||
|
|
||
| ## Manage full-text indexes | ||
|
|
||
| When creating a full-text index, specifying an index name is optional. If you do not specify one, TiDB uses the name of the first indexed column as the index name by default. | ||
|
|
||
| ```sql | ||
| -- Without specifying an index name, TiDB uses the first indexed column name ("title") as the index name | ||
| ALTER TABLE stock_items ADD FULLTEXT INDEX (title) WITH PARSER MULTILINGUAL; | ||
|
Comment on lines
+309
to
+310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid creating the existing default index again. The earlier |
||
|
|
||
| -- Specifying an index name | ||
| ALTER TABLE stock_items ADD FULLTEXT INDEX ft_title (title) WITH PARSER MULTILINGUAL; | ||
| ``` | ||
|
|
||
| ### View existing index names | ||
|
|
||
| ```sql | ||
| -- The Key_name column shows the index name | ||
| SHOW INDEX FROM stock_items; | ||
|
|
||
| -- Or query INFORMATION_SCHEMA | ||
| SELECT INDEX_NAME, COLUMN_NAME, INDEX_TYPE | ||
| FROM INFORMATION_SCHEMA.STATISTICS | ||
| WHERE TABLE_SCHEMA = 'your_database' AND TABLE_NAME = 'stock_items'; | ||
| ``` | ||
|
|
||
| ### Drop a full-text index | ||
|
|
||
| ```sql | ||
| -- Use SHOW INDEX to confirm the index name first | ||
| ALTER TABLE stock_items DROP INDEX title; | ||
| ``` | ||
|
|
||
| ## Advanced example: Join search results with other tables | ||
|
|
||
| You can combine full-text search with other SQL features such as joins and subqueries. | ||
| You can combine full-text search with other SQL features such as inner joins and subqueries. | ||
|
|
||
| Assume you have a `users` table and a `tickets` table, and want to find tickets created by authors based on a full-text search of their names: | ||
|
|
||
|
|
@@ -298,7 +363,7 @@ You can use a subquery to find matching user IDs based on the author's name, and | |
|
|
||
| ```sql | ||
| SELECT t.title AS TICKET_TITLE, u.id AS AUTHOR_ID, u.name AS AUTHOR_NAME FROM tickets t | ||
| LEFT JOIN users u ON t.author_id = u.id | ||
| INNER JOIN users u ON t.author_id = u.id | ||
| WHERE t.author_id IN | ||
| ( | ||
| SELECT id FROM users | ||
|
|
@@ -316,6 +381,9 @@ WHERE t.author_id IN | |
| ## See also | ||
|
|
||
| - [Hybrid Search](/ai/guides/vector-search-hybrid-search.md) | ||
| - [Full-Text Search Index](/ai/reference/full-text-search-index.md) | ||
| - [Full-Text Search Functions](/ai/reference/full-text-search-functions-tidb.md) | ||
| - [Full-Text Search Limitations](/ai/reference/full-text-search-limitations.md) | ||
|
|
||
| ## Feedback & help | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qualify when an index name is optional.
The index reference requires a name for
NGRAMand column-property indexes. The current statement says that a name is optional for every full-text index.Proposed wording: For a
MULTILINGUALindex in syntax sugar mode, specifying an index name is optional. If you omit the name, TiDB uses the name of the first indexed column by default. ForNGRAMand column-property indexes, specify an index name.