From c90f9bff5101aa87de5aed34befcc2453f79ded5 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 16 Jan 2020 20:15:32 +0100 Subject: [PATCH] Added FTS OR search --- FAQ.md | 4 ++++ .../java/eu/faircode/email/FtsDbHelper.java | 23 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/FAQ.md b/FAQ.md index 46078bbeb8..100d22ccd3 100644 --- a/FAQ.md +++ b/FAQ.md @@ -693,6 +693,10 @@ to check if the searched text is contained in the file, which is a relative expe In the *miscellaneous settings* you can enable *Build search index* to significantly increase the speed of searching on the device, but be aware that this will increase battery usage and significantly increase storage space usage too. +Searching using the search index is by default AND, so searching for *apple orange* will search for apple AND orange. +Words separated by commas results in searching for OR, so for example *apple, orange* will search for apple OR orange. +Both can be combined, so searching for *apple, orange banana* will search for apple OR (orange AND banana). +Using the search index is a pro feature. Searching messages on the device is a free feature, searching messages on the server is a pro feature. diff --git a/app/src/main/java/eu/faircode/email/FtsDbHelper.java b/app/src/main/java/eu/faircode/email/FtsDbHelper.java index 7e159ba677..9dce2981a0 100644 --- a/app/src/main/java/eu/faircode/email/FtsDbHelper.java +++ b/app/src/main/java/eu/faircode/email/FtsDbHelper.java @@ -94,13 +94,26 @@ public class FtsDbHelper extends SQLiteOpenHelper { } static List match(SQLiteDatabase db, Long folder, String query) { - String[] parts = query.split("\\s+"); StringBuilder sb = new StringBuilder(); - for (String part : parts) { + for (String or : query.split(",")) { if (sb.length() > 0) - sb.append(" AND "); - part = part.replaceAll("\"", "\"\""); - sb.append("\"").append(part).append("\""); + sb.append(" OR "); + + boolean first = true; + sb.append("("); + for (String and : or.trim().split("\\s+")) { + if (first) + first = false; + else + sb.append(" AND "); + + // Escape quotes + String term = and.replaceAll("\"", "\"\""); + + // Quote search term + sb.append("\"").append(term).append("\""); + } + sb.append(")"); } String search = sb.toString();