Question

I have a large database in my app. One column is made of text strings that are about a sentence to a paragraph long. I would like to make this column searchable by word(s) that the user inputs.

How would I make a quick search? I've heard of making an index but I don't know how to do that for a text search.

Was it helpful?

Solution

SQLite has a mechanism for storing a lot of text in a database, it's called FTS (short for full text search). Android supports all SQLite commands, so you can easily just use FTS3.

How is explained in the documentation linked above.

Example for creating a table:

CREATE VIRTUAL TABLE enrondata1 USING fts3(content TEXT);  /* FTS3 table */
CREATE TABLE enrondata2(content TEXT);                     /* Ordinary table */

Query:

SELECT count(*) FROM enrondata1 WHERE content MATCH 'linux';  /* 0.03 seconds */
SELECT count(*) FROM enrondata2 WHERE content LIKE '%linux%'; /* 22.5 seconds */
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top