@@ -11,21 +11,21 @@ import EEFeature from '@site/src/components/EEFeature';
1111
1212<EEFeature featureName =' NGRAM INDEX ' />
1313
14- Creates an Ngram index on one or more columns for a table.
14+ Creates an Ngram index on a column for a table.
1515
1616## Syntax
1717
1818``` sql
1919-- Create an Ngram index on an existing table
2020CREATE [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 > ]
2323
2424-- Create an Ngram index when creating a table
2525CREATE [OR REPLACE] TABLE < table_name> (
2626 < 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 > ]
2929)...
3030```
3131
@@ -43,67 +43,79 @@ CREATE [OR REPLACE] TABLE <table_name> (
4343
4444## Examples
4545
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
4747
4848``` 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+ );
6755```
6856
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
7058
7159``` 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);
7368```
7469
70+ ### Viewing Indexes
71+
7572``` 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+ └─────────────────┴───────┴──────────┴─────────────────────────┴──────────────────────────┘
8184```
8285
83- Alternatively, you can create the table first, then create the Ngram index on the ` review_body ` column:
86+ ### Using NGRAM Index
8487
8588``` 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 )
10294);
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+ └────┴─────────────────────┘
103115```
104116
117+ ### Dropping an NGRAM Index
118+
105119``` 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