mirror of https://github.com/M66B/FairEmail.git
Use FTS vocabulary for search suggestions
This commit is contained in:
parent
55b289ad6b
commit
280545a343
|
@ -56,6 +56,8 @@ import java.util.ArrayList;
|
|||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import io.requery.android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
public class FragmentDialogSearch extends FragmentDialogBase {
|
||||
private static final int MAX_SUGGESTIONS = 3;
|
||||
|
||||
|
@ -137,15 +139,23 @@ public class FragmentDialogSearch extends FragmentDialogBase {
|
|||
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "suggestion"});
|
||||
if (TextUtils.isEmpty(typed))
|
||||
return cursor;
|
||||
if (fts && pro)
|
||||
return cursor;
|
||||
|
||||
String query = "%" + typed + "%";
|
||||
if (cbSearchIndex.isEnabled() && cbSearchIndex.isChecked()) {
|
||||
SQLiteDatabase db = FtsDbHelper.getInstance(context);
|
||||
List<String> suggestions = FtsDbHelper.getSuggestions(
|
||||
db,
|
||||
typed + "%",
|
||||
MAX_SUGGESTIONS);
|
||||
for (int i = 0; i < suggestions.size(); i++)
|
||||
cursor.addRow(new Object[]{i + 1, suggestions.get(i)});
|
||||
return cursor;
|
||||
}
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
return db.message().getSuggestions(
|
||||
account < 0 ? null : account,
|
||||
folder < 0 ? null : folder,
|
||||
"%" + query + "%",
|
||||
"%" + typed + "%",
|
||||
MAX_SUGGESTIONS);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue