Search optimizations

pull/209/head
M66B 2 years ago
parent b2c1badfcb
commit 2e58059bca

@ -277,7 +277,6 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
}
int found = 0;
String query = (criteria.query == null ? null : criteria.query.toLowerCase());
if (criteria.fts && criteria.query != null) {
if (state.ids == null) {
@ -333,33 +332,33 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
boolean matched = false;
if (!matched && criteria.in_senders) {
if (contains(message.from, query))
if (contains(message.from, criteria.query))
matched = true;
}
if (!matched && criteria.in_recipients) {
if (contains(message.to, query) ||
contains(message.cc, query) ||
contains(message.bcc, query))
if (contains(message.to, criteria.query) ||
contains(message.cc, criteria.query) ||
contains(message.bcc, criteria.query))
matched = true;
}
if (!matched && criteria.in_subject) {
if (contains(message.subject, query))
if (contains(message.subject, criteria.query))
matched = true;
}
if (!matched && criteria.in_keywords) {
if (message.keywords != null)
for (String keyword : message.keywords)
if (contains(keyword, query)) {
if (contains(keyword, criteria.query)) {
matched = true;
break;
}
}
if (!matched && criteria.in_notes) {
if (contains(message.notes, query))
if (contains(message.notes, criteria.query))
matched = true;
}
@ -368,9 +367,9 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
File file = EntityMessage.getFile(context, id);
if (file.exists()) {
String html = Helper.readText(file);
if (contains(html, query)) {
if (contains(html, criteria.query)) {
String text = HtmlHelper.getFullText(html);
if (contains(text, query))
if (contains(text, criteria.query))
matched = true;
}
}
@ -437,26 +436,24 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
TupleMatch match = state.matches.get(i);
boolean matched = (match.matched != null && match.matched);
if (query != null) {
if (!matched && (criteria.in_message || criteria.in_html))
try {
File file = EntityMessage.getFile(context, match.id);
if (file.exists()) {
String html = Helper.readText(file);
if (html.toLowerCase().contains(query)) {
if (criteria.in_html)
if (!matched && criteria.query != null && (criteria.in_message || criteria.in_html))
try {
File file = EntityMessage.getFile(context, match.id);
if (file.exists()) {
String html = Helper.readText(file);
if (contains(html, criteria.query)) {
if (criteria.in_html)
matched = true;
else {
String text = HtmlHelper.getFullText(html);
if (contains(text, criteria.query))
matched = true;
else {
String text = HtmlHelper.getFullText(html);
if (text != null && text.toLowerCase().contains(query))
matched = true;
}
}
}
} catch (IOException ex) {
Log.e(ex);
}
}
} catch (IOException ex) {
Log.e(ex);
}
if (matched) {
found += db.message().setMessageFound(match.id, true);
@ -791,12 +788,17 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
private static boolean contains(String text, String query) {
if (TextUtils.isEmpty(text))
return false;
text = text.toLowerCase();
if (text.contains(query))
return true;
String normalized = Normalizer.normalize(text, Normalizer.Form.NFKD)
query = query.toLowerCase();
query = Normalizer.normalize(query.toLowerCase(), Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
text = Normalizer.normalize(text, Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return normalized.contains(query);
return text.contains(query);
}
State getState() {

@ -134,13 +134,18 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
db.delete("message", "rowid = ?", new String[]{Long.toString(id)});
}
static String breakText(String text) {
private static String breakText(String text) {
if (TextUtils.isEmpty(text))
return "";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
return text;
// https://www.sqlite.org/fts3.html#tokenizer
text = Normalizer.normalize(text.toLowerCase(), Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
StringBuilder sb = new StringBuilder();
android.icu.text.BreakIterator boundary = android.icu.text.BreakIterator.getWordInstance();
boundary.setText(text);
@ -148,8 +153,6 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
for (int end = boundary.next(); end != android.icu.text.BreakIterator.DONE; end = boundary.next()) {
String word = text.substring(start, end).trim().toLowerCase();
if (!TextUtils.isEmpty(word)) {
word = Normalizer.normalize(word, Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
if (sb.length() > 0)
sb.append(' ');
sb.append(word);

Loading…
Cancel
Save