Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FTS3 in SQLite

I have table with almost 200k entries. When I tried search with LIKE, it was very slow. Now I decided to use FTS. So I created two indexes where search will be held. Then I created fts virtual table.

`CREATE TABLE [search_eng] (
  [id] INTEGER PRIMARY KEY AUTOINCREMENT, 
  [entry_id] INTEGER, 
  [re_value] TEXT, 
  [ke_value] TEXT, 
  [g_value] TEXT);

CREATE INDEX idx_se_re ON search_eng (re_value);

CREATE INDEX idx_se_gv ON search_eng (g_value);


CREATE VIRTUAL TABLE search_eng_fts USING fts3(id, entry_id, re_value, ke_value, g_value);`

I have no idea how to use new created FTS table. So my questions is how to use that virtual table to make search? Can you give an example?

like image 229
Joe Rakhimov Avatar asked Aug 27 '13 13:08

Joe Rakhimov


People also ask

What is FTS3?

FTS3 and FTS4 are SQLite virtual table modules that allows full-text searches to be performed on a set of documents. An FTS entity table always has a column named rowid that is the equivalent of an INTEGER PRIMARY KEY index.

What is SQLite FTS?

Overview of FTS5. FTS5 is an SQLite virtual table module that provides full-text search functionality to database applications.

How do I find data in SQLite?

To Search for the values define edit text and implement text watcher in database enter a query as shown below: editText. addTextChangedListener(new TextWatcher(){ Cursor cusror; cursor=db. rawQuery("SELECT * FROM "+ DB_NAME + " WHERE " + DB_NAME.id + " = " + DB_NAME.Id + " AND " + DB_NAME.

What is a Tokenizer SQLite?

H41020: At each step in the SQL tokenization process, SQLite shall extract the longest possible token from the remaining input text. The tokenizer recognizes tokens one by one and passes them on to the parser. Except whitespace is ignored. The only use for whitespace is as a separator between tokens.


1 Answers

This is explained in the documentation.

You do not need the two indexes for FTS searches.

You should declare the id column as INTEGER PRIMARY KEY. You probably don't need the entry_id column in the FST table.

Copy the text into the FTS table:

INSERT INTO search_eng_fts(id, re_value, ke_value, g_value)
SELECT id, re_value, ke_value, g_value FROM search_eng;

Then you can use the MATCH operator to search in that table:

SELECT id FROM search_eng_fts WHERE re_value MATCH 'hello';
like image 199
CL. Avatar answered Sep 22 '22 12:09

CL.