@@ -11,21 +11,21 @@ import EEFeature from '@site/src/components/EEFeature';
11
11
12
12
<EEFeature featureName =' NGRAM INDEX ' />
13
13
14
- Creates an Ngram index on one or more columns for a table.
14
+ Creates an Ngram index on a column for a table.
15
15
16
16
## Syntax
17
17
18
18
``` sql
19
19
-- Create an Ngram index on an existing table
20
20
CREATE [OR REPLACE] NGRAM INDEX [IF NOT EXISTS] < index_name>
21
- ON [< database> .]< table_name> (< column1 > [, < column2 > , ...] )
22
- [gram_size = < number > ] [bitmap_size = < number > ]
21
+ ON [< database> .]< table_name> (< column > )
22
+ [gram_size = < number > ] [bloom_size = < number > ]
23
23
24
24
-- Create an Ngram index when creating a table
25
25
CREATE [OR REPLACE] TABLE < table_name> (
26
26
< column_definitions> ,
27
- NGRAM INDEX < index_name> (< column1 > [, < column2 > , ...] )
28
- [gram_size = < number > ] [bitmap_size = < number > ]
27
+ NGRAM INDEX < index_name> (< column > )
28
+ [gram_size = < number > ] [bloom_size = < number > ]
29
29
)...
30
30
```
31
31
@@ -43,67 +43,79 @@ CREATE [OR REPLACE] TABLE <table_name> (
43
43
44
44
## Examples
45
45
46
- The following example creates a table ` amazon_reviews_ngram ` with an Ngram index on the ` review_body ` column. The index is configured with a ` gram_size ` of 10 and a ` bitmap_size ` of 2 MB to optimize fuzzy search performance on large text fields such as user reviews.
46
+ ### Creating a Table with NGRAM Index
47
47
48
48
``` sql
49
- CREATE OR REPLACE TABLE amazon_reviews_ngram (
50
- review_date int (11 ) NULL ,
51
- marketplace varchar (20 ) NULL ,
52
- customer_id bigint (20 ) NULL ,
53
- review_id varchar (40 ) NULL ,
54
- product_id varchar (10 ) NULL ,
55
- product_parent bigint (20 ) NULL ,
56
- product_title varchar (500 ) NULL ,
57
- product_category varchar (50 ) NULL ,
58
- star_rating smallint (6 ) NULL ,
59
- helpful_votes int (11 ) NULL ,
60
- total_votes int (11 ) NULL ,
61
- vine boolean NULL ,
62
- verified_purchase boolean NULL ,
63
- review_headline varchar (500 ) NULL ,
64
- review_body string NULL ,
65
- NGRAM INDEX idx1 (review_body) gram_size = 10 bloom_size = 2097152
66
- ) Engine = Fuse bloom_index_columns= ' review_body' ;
49
+ CREATE TABLE articles (
50
+ id INT ,
51
+ title VARCHAR ,
52
+ content STRING,
53
+ NGRAM INDEX idx_content (content)
54
+ );
67
55
```
68
56
69
- To show the created index, use the [ SHOW INDEXES ] ( ../../50-administration-cmds/show-indexes.md ) command:
57
+ ### Creating an NGRAM Index on an Existing Table
70
58
71
59
``` sql
72
- SHOW INDEXES;
60
+ CREATE TABLE products (
61
+ id INT ,
62
+ name VARCHAR ,
63
+ description STRING
64
+ );
65
+
66
+ CREATE NGRAM INDEX idx_description
67
+ ON products(description);
73
68
```
74
69
70
+ ### Viewing Indexes
71
+
75
72
``` sql
76
- ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
77
- │ name │ type │ original │ definition │ created_on │ updated_on │
78
- ├────────┼────────┼──────────┼──────────────────────────────────────────────────────────────────────┼────────────────────────────┼─────────────────────┤
79
- │ idx1 │ NGRAM │ │ amazon_reviews_ngram(review_body)bloom_size= ' 2097152' gram_size= ' 10' │ 2025 - 05 - 13 01 :22 :34 .123927 │ NULL │
80
- └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
73
+ SHOW INDEXES;
74
+ ```
75
+
76
+ Result:
77
+ ```
78
+ ┌─────────────────┬───────┬──────────┬─────────────────────────┬──────────────────────────┐
79
+ │ name │ type │ original │ definition │ created_on │
80
+ ├─────────────────┼───────┼──────────┼─────────────────────────┼──────────────────────────┤
81
+ │ idx_content │ NGRAM │ │ articles(content) │ 2025-05-13 01:22:34.123 │
82
+ │ idx_description │ NGRAM │ │ products(description) │ 2025-05-13 01:23:45.678 │
83
+ └─────────────────┴───────┴──────────┴─────────────────────────┴──────────────────────────┘
81
84
```
82
85
83
- Alternatively, you can create the table first, then create the Ngram index on the ` review_body ` column:
86
+ ### Using NGRAM Index
84
87
85
88
``` sql
86
- CREATE TABLE amazon_reviews_ngram (
87
- review_date int (11 ) NULL ,
88
- marketplace varchar (20 ) NULL ,
89
- customer_id bigint (20 ) NULL ,
90
- review_id varchar (40 ) NULL ,
91
- product_id varchar (10 ) NULL ,
92
- product_parent bigint (20 ) NULL ,
93
- product_title varchar (500 ) NULL ,
94
- product_category varchar (50 ) NULL ,
95
- star_rating smallint (6 ) NULL ,
96
- helpful_votes int (11 ) NULL ,
97
- total_votes int (11 ) NULL ,
98
- vine boolean NULL ,
99
- verified_purchase boolean NULL ,
100
- review_headline varchar (500 ) NULL ,
101
- review_body string NULL
89
+ -- Create a table with NGRAM index
90
+ CREATE TABLE phrases (
91
+ id INT ,
92
+ text STRING,
93
+ NGRAM INDEX idx_text (text )
102
94
);
95
+
96
+ -- Insert sample data
97
+ INSERT INTO phrases VALUES
98
+ (1 , ' apple banana cherry' ),
99
+ (2 , ' banana date fig' ),
100
+ (3 , ' cherry elderberry fig' ),
101
+ (4 , ' date grape kiwi' );
102
+
103
+ -- Query using fuzzy matching with the NGRAM index
104
+ SELECT * FROM phrases WHERE text LIKE ' %banana%' ;
105
+ ```
106
+
107
+ Result:
108
+ ```
109
+ ┌────┬─────────────────────┐
110
+ │ id │ text │
111
+ ├────┼─────────────────────┤
112
+ │ 1 │ apple banana cherry │
113
+ │ 2 │ banana date fig │
114
+ └────┴─────────────────────┘
103
115
```
104
116
117
+ ### Dropping an NGRAM Index
118
+
105
119
``` sql
106
- CREATE NGRAM INDEX idx1
107
- ON amazon_reviews_ngram(review_body)
108
- gram_size = 10 bloom_size = 2097152 ;
109
- ```
120
+ DROP NGRAM INDEX idx_text ON phrases;
121
+ ```
0 commit comments