Senhor Obvio
Senhor Obvio

Reputation: 71

FTS4 sqlite MATCH not working

I've tried several methods from here:

SQLite FTS example doesn't work

and here:

Full text search example in Android (best tutorial so far i think)

However, my search returns 0 results!

Here is what I've tried:

   String key = "a";
        Cursor c = db.query(true, "texts_virtual",
                new String[]{"id","title_normalized"},
                "title_normalized MATCH '"+key+"'",
                null, null, null, null, null);

= 0 Results;

 String query = "a";
    String[] params = {"%" +query+ "%"};

    Cursor c = db.rawQuery("SELECT * FROM texts_virtual WHERE title_normalized MATCH ?", params);

= 0 Results too

I know that the virtual table is correctly working because I can do this:

String queryText = "a"; //here i test other texts and they worked too
        String query = "select * from texts_virtual where title_normalized like ? order by number";
        String[] params = {"%" + queryText + "%"};
        Cursor c = db.rawQuery(query, params);

so this prove that the texts_virtual is working, what is not working are the queries, but I don't know why, not error, nothing, just 0 results.

Also after I make it work, I'm planning to use multiple terms search in 2 columns

user type "WordA WordB WordC"

it search for each word in the 2columns and return the results, but this if for a future task....

Edit

Table Code Creation:

CREATE TABLE texts (id INTEGER PRIMARY KEY AUTOINCREMENT, title_normalized....);

INSERT INTO texts (id, titulo_normalized...) VALUES (1, 'aaaaaa', ...);

and go on for more inserts, and at the end the virtual creation

CREATE VIRTUAL TABLE texts_virtual USING fts4(content="texts", id, title_normalized, ..other fields);

i can query texts_virtual using LIKE but not MATCH, match return 0 results =/

Edit 2 how the table looks:

Table: texts_virtual
----------------------------
id --- title_normalized
--------------------------
1  --- aaaaaaaaab
2  --- abbbbbbbbb
3  --- bbbbbabbbb
4  --- bbbbbbbbbb

Upvotes: 2

Views: 2310

Answers (2)

Thomas
Thomas

Reputation: 471

You are using % as a joker. In FTS requests, You have to use * instead.

LIKE "%word%"

MATCH "*word*"

I've noticed that for very short words (less than 3 letters), LIKE is faster than MATCH. For longer words, MATCH is faster.

Upvotes: 1

CL.
CL.

Reputation: 180010

The FTS module searches for words (where the exact definition depends on the tokenizer used), or at best for words with a prefix.

MATCH words as designed; it does not find "a" because there is no word "a" in your data.

If you want to find substrings inside words, you must use LIKE.

Upvotes: 2

Related Questions