Use FTS vocabulary for search suggestions

This commit is contained in:
M66B
2021-06-14 08:10:00 +02:00
parent 55b289ad6b
commit 280545a343
2 changed files with 37 additions and 6 deletions

View File

@@ -39,7 +39,7 @@ public class FtsDbHelper extends SQLiteOpenHelper {
private static FtsDbHelper instance = null;
private static final int DATABASE_VERSION = 4;
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "fts.db";
private FtsDbHelper(Context context) {
@@ -68,12 +68,18 @@ public class FtsDbHelper extends SQLiteOpenHelper {
", `notes`" +
", tokenize = \"unicode61 remove_diacritics 2\")");
// https://www.sqlite.org/fts5.html#unicode61_tokenizer
// https://www.sqlite.org/fts5.html#the_fts5vocab_virtual_table_module
db.execSQL("CREATE VIRTUAL TABLE message_terms USING fts5vocab('message', 'row');");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("FTS upgrade from " + oldVersion + " to " + newVersion);
db.execSQL("DROP TABLE `message`");
db.execSQL("DROP TABLE IF EXISTS `message`");
db.execSQL("DROP TABLE IF EXISTS `message_terms`");
onCreate(db);
DB.getInstance(context).message().resetFts();
@@ -114,6 +120,21 @@ public class FtsDbHelper extends SQLiteOpenHelper {
db.delete("message", "rowid = ?", new Object[]{id});
}
static List<String> getSuggestions(SQLiteDatabase db, String query, int max) {
List<String> result = new ArrayList<>();
Cursor cursor = db.query(
"SELECT term FROM message_terms" +
" WHERE term LIKE ?" +
" ORDER BY cnt" +
" LIMIT " + max,
new Object[]{query});
while (cursor != null && cursor.moveToNext())
result.add(cursor.getString(0));
return result;
}
static List<Long> match(
SQLiteDatabase db,
Long account, Long folder,