NAME: Demonstrating some of the syntax used in creating and using FTS tables.
DESCRIPTION:
SQLite supports Full Text Search (FTS3 and FTS4). The purpose of this snippet is to introduce some of its basic syntax and operators. This is a rudimentary introduction to FTS as I just recently started learning about it. FTS is a fast way to look for specific words or sentences in text columns of a SQLite table. You can also search the entire table with one SELECT statement. For newer applications, FTS4 is recommended.
To create a table:with one column. Tables can also have several columns.
Actually when creating a table with name: fts_table, three to five real (non-virtual) tables referred to as shadow tables are also created to store the underlying data. Fts_table_content, fts_table_docsize and fts_table_segdir and fts_table_segments. FTS created tables require a lot more memory than regular tables, but their queries execute a lot faster.
To delete a table, the syntax is the same as a normal table:
To Insert or update records, the syntax is the same as a normal SQLite table, here to insert:
To make a simple SELECT statement in column ‘content’, To search for records where the full word fiduciary (case insensitive search) is found anywhere in any record:
To select all records which contain both words Europe and Australia
To search for records that have the word sabbat and any other words like Sabbath, sabbatini. The word sabbat can be anywhere in the record.
To search for records that contain both cheese and cream, but both words cannot be more than 8 words apart. Cheese can be before cream or after in the record. If the integer 8 is omitted, the default is 10 words apart.
To select records which contain Europe but not spain. Note, it is case insensitive. There are more complex queries supported:
To select all records that have a word or sentence starting with europea and that also have a word or sentence starting with bird:
To select all records which either have flamingo or bird. Notice OR must be uppercase. Otherwise, it is considered part of the search string:
To select all records where the three words: flamingo, or and bird are found. Notice lowercase or. This statement yields a very different result set from the previous one despite the same syntax:
BENCHMARK:
I made a comparison between an FTS table and a normal table, where both contain the same 91300 text records, using different SELECT statements on a Samsung Galaxy Tab A tablet. In all cases the query ran a lot faster with FTS. The FTS query took an average of 0.01 second and the ordinary table query ran in about 1 second, in some cases 100 times faster with FTS, but generally a large percentage or few times faster.. For instance, searching for ‘lyterian’ it took 0 second querying the FTS table as opposed to 1.04 seconds querying an ordinary table.
I encourage adding to and improving this thread.
Tags: FTS3, FTS4, MATCH
Dependency: SQL Library
DESCRIPTION:
SQLite supports Full Text Search (FTS3 and FTS4). The purpose of this snippet is to introduce some of its basic syntax and operators. This is a rudimentary introduction to FTS as I just recently started learning about it. FTS is a fast way to look for specific words or sentences in text columns of a SQLite table. You can also search the entire table with one SELECT statement. For newer applications, FTS4 is recommended.
B4X:
Dim DBTableName As String="fts_table"
To create a table:with one column. Tables can also have several columns.
B4X:
txt="CREATE VIRTUAL TABLE IF NOT EXISTS " & DBTableName & " USING fts4(content TEXT)"
SQL1.ExecNonQuery(txt)
To delete a table, the syntax is the same as a normal table:
B4X:
txt="DROP TABLE IF EXISTS " & DBTableName
SQL1.ExecNonQuery(txt)
To Insert or update records, the syntax is the same as a normal SQLite table, here to insert:
B4X:
txt="INSERT INTO fts_table (content) VALUES('Your testing score ranked you highest')"
SQL1.ExecNonQuery(txt)
To make a simple SELECT statement in column ‘content’, To search for records where the full word fiduciary (case insensitive search) is found anywhere in any record:
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH 'fiduciary'"
Dim RS As ResultSet =SQL1.ExecQuery(txt)
Do While RS.NextRow
Log(RS.GetString("content") )
Loop
To select all records which contain both words Europe and Australia
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH 'Europe Australia'"
To search for records that have the word sabbat and any other words like Sabbath, sabbatini. The word sabbat can be anywhere in the record.
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH 'sabbat*'"
To search for records that contain both cheese and cream, but both words cannot be more than 8 words apart. Cheese can be before cream or after in the record. If the integer 8 is omitted, the default is 10 words apart.
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH 'Cheese NEAR/8 cream'"
To select records which contain Europe but not spain. Note, it is case insensitive. There are more complex queries supported:
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH 'Europe -spain'"
To select all records that have a word or sentence starting with europea and that also have a word or sentence starting with bird:
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH ‘europea* bird*'"
To select all records which either have flamingo or bird. Notice OR must be uppercase. Otherwise, it is considered part of the search string:
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH ‘flamingo OR bird'"
To select all records where the three words: flamingo, or and bird are found. Notice lowercase or. This statement yields a very different result set from the previous one despite the same syntax:
B4X:
txt="SELECT * FROM " & DBTableName & " WHERE content MATCH ‘flamingo or bird'"
BENCHMARK:
I made a comparison between an FTS table and a normal table, where both contain the same 91300 text records, using different SELECT statements on a Samsung Galaxy Tab A tablet. In all cases the query ran a lot faster with FTS. The FTS query took an average of 0.01 second and the ordinary table query ran in about 1 second, in some cases 100 times faster with FTS, but generally a large percentage or few times faster.. For instance, searching for ‘lyterian’ it took 0 second querying the FTS table as opposed to 1.04 seconds querying an ordinary table.
I encourage adding to and improving this thread.
Tags: FTS3, FTS4, MATCH
Dependency: SQL Library