feat(app-search):应用搜索支持去掉空格匹配

This commit is contained in:
aming 2024-06-11 14:11:38 +08:00
parent 7887fa9a2e
commit 9583147c58
1 changed files with 4 additions and 6 deletions

View File

@ -494,12 +494,10 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationPropertie
field.append(ApplicationPropertyHelper(ApplicationProperty::Property::DesktopFilePath).dataBaseField() + ","); field.append(ApplicationPropertyHelper(ApplicationProperty::Property::DesktopFilePath).dataBaseField() + ",");
} }
field.append(ApplicationPropertyHelper(ApplicationProperty::Property::LocalKeywords).dataBaseField()); field.append(ApplicationPropertyHelper(ApplicationProperty::Property::LocalKeywords).dataBaseField());
QString condition; QString condition;
for (const ApplicationProperty::Property prop: restrictions.keys()) { for (const ApplicationProperty::Property prop: restrictions.keys()) {
condition.append(ApplicationPropertyHelper(prop).dataBaseField() + "=? AND "); condition.append(ApplicationPropertyHelper(prop).dataBaseField() + "=? AND ");
} }
QString keywordCondition; QString keywordCondition;
QString findWithLocalKeywords = "((select count(*) from (" QString findWithLocalKeywords = "((select count(*) from ("
"WITH RECURSIVE split(str, rest) AS " "WITH RECURSIVE split(str, rest) AS "
@ -513,9 +511,9 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationPropertie
"SELECT str FROM split WHERE lower(str)=?)) <> 0)"; "SELECT str FROM split WHERE lower(str)=?)) <> 0)";
for(const QString& keyword : keywords) { for(const QString& keyword : keywords) {
if(keyword.size() < 2) { if(keyword.size() < 2) {
keywordCondition.append(QString("(ifnull(LOCAL_NAME, '') like ? or ifnull(NAME_EN, '') like ? or ifnull(NAME_ZH, '') like ? or ifnull(FIRST_LETTER_OF_PINYIN, '') like ? or %0) AND").arg(findWithLocalKeywords)); keywordCondition.append(QString("(ifnull(LOCAL_NAME, '') like ? or ifnull(replace(LOCAL_NAME,' ',''), '') like ? or ifnull(NAME_EN, '') like ? or ifnull(replace(NAME_EN,' ',''), '') like ? or ifnull(NAME_ZH, '') like ? or ifnull(replace(NAME_ZH,' ',''), '') like ? or ifnull(FIRST_LETTER_OF_PINYIN, '') like ? or ifnull(replace(FIRST_LETTER_OF_PINYIN,' ',''), '') like ? or %0) AND").arg(findWithLocalKeywords));
} else { } else {
keywordCondition.append(QString("(ifnull(LOCAL_NAME, '') like ? or ifnull(NAME_EN, '') like ? or ifnull(NAME_ZH, '') like ? or ifnull(FIRST_LETTER_OF_PINYIN, '') like ? or ifnull(PINYIN_NAME, '') like ? or %0) AND").arg(findWithLocalKeywords)); keywordCondition.append(QString("(ifnull(LOCAL_NAME, '') like ? or ifnull(replace(LOCAL_NAME,' ',''), '') like ? or ifnull(NAME_EN, '') like ? or ifnull(replace(NAME_EN,' ',''), '') like ? or ifnull(NAME_ZH, '') like ? or ifnull(replace(NAME_ZH,' ',''), '') like ? or ifnull(FIRST_LETTER_OF_PINYIN, '') like ? or ifnull(replace(FIRST_LETTER_OF_PINYIN,' ',''), '') like ? or ifnull(PINYIN_NAME, '') like ? or ifnull(replace(PINYIN_NAME,' ',''), '') like ? or %0) AND").arg(findWithLocalKeywords));
} }
} }
if(!keywordCondition.isEmpty()) { if(!keywordCondition.isEmpty()) {
@ -534,9 +532,9 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationPropertie
} }
for(const QString &keyword : keywords) { for(const QString &keyword : keywords) {
int i = 6; int i = 11;
if(keyword.size() < 2) { if(keyword.size() < 2) {
i--; i = i - 2;
} }
for (int bindCount = 0; bindCount < i; bindCount++) { for (int bindCount = 0; bindCount < i; bindCount++) {
query.bindValue(count, (bindCount - i + 1) ? "%" + keyword + "%" : keyword.toLower()); query.bindValue(count, (bindCount - i + 1) ? "%" + keyword + "%" : keyword.toLower());