Break FTS queries into multiple prefix queries.

Previously, we made each full-text search query a single prefix query.
That means that the query "do c" would turn into "do c*". That means it
would match "do cat" but not "dog cat".

Now, we make each token a prefix query. So "do c" would turn into
"do* c*". That means it would match both "do cat" and "dog cat".
This commit is contained in:
Greyson Parrelli 2018-06-15 09:38:08 -07:00
parent 3563efc7de
commit 89fd7dda23

View File

@ -2,12 +2,16 @@ package org.thoughtcrime.securesms.database;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import com.annimon.stream.Stream;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
import org.thoughtcrime.securesms.util.Util;
import java.util.List;
/**
* Contains all databases necessary for full-text search (FTS).
@ -81,7 +85,10 @@ public class SearchDatabase extends Database {
public Cursor queryMessages(@NonNull String query) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String prefixQuery = query + '*';
List<String> tokens = Stream.of(query.split(" ")).filter(s -> s.trim().length() > 0).toList();
String prefixQuery = Util.join(tokens, "* ");
prefixQuery += "*";
Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { prefixQuery, prefixQuery });
setNotifyConverationListListeners(cursor);