From 89fd7dda232f3dcc63d75be4f284b522ccfbcb85 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Fri, 15 Jun 2018 09:38:08 -0700 Subject: [PATCH] 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". --- .../securesms/database/SearchDatabase.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/SearchDatabase.java b/src/org/thoughtcrime/securesms/database/SearchDatabase.java index 161ffbc2fb..7724e46734 100644 --- a/src/org/thoughtcrime/securesms/database/SearchDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SearchDatabase.java @@ -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 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);