Skip to content

Commit

Permalink
fix: There are issues with searching for special characters and numbe…
Browse files Browse the repository at this point in the history
…rs based on keywords in the file content

Modify the word segmentation when establishing an index

Log: There are issues with searching for special characters and numbers based on keywords in the file content
Bug: https://pms.uniontech.com/bug-view-270025.html
  • Loading branch information
liyigang1 committed Sep 14, 2024
1 parent 2170200 commit 597b5a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ bool ChineseTokenizer::incrementToken()
length = 0;
start = offset;

bool last_is_en = false, last_is_num = false;
while (true) {
wchar_t c;
++offset;
Expand All @@ -97,41 +96,13 @@ bool ChineseTokenizer::incrementToken()
c = ioBuffer[bufferIndex++];
}

if (UnicodeUtil::isLower(c) || UnicodeUtil::isUpper(c)) {
if (last_is_num) {
--bufferIndex;
--offset;
return flush();
}

push(c);
if (length == kMaxWordLen) {
return flush();
}
last_is_en = true;
} else if (UnicodeUtil::isDigit(c)) {
if (last_is_en) {
--bufferIndex;
--offset;
return flush();
}

push(c);
if (length == kMaxWordLen) {
return flush();
}
last_is_num = true;
} else if (UnicodeUtil::isOther(c)) {
if (length > 0) {
--bufferIndex;
--offset;
return flush();
}
push(c);
return flush();
} else if (length > 0) {
if (length > 0) {
--bufferIndex;
--offset;
return flush();
}
push(c);
return flush();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ bool FullTextSearcherPrivate::doSearch(const QString &path, const QString &keywo
} catch (...) {
fmWarning() << "Search failed!";
}

return true;
}

Expand Down Expand Up @@ -436,6 +435,19 @@ QString FullTextSearcherPrivate::dealKeyword(const QString &keyword)
return newStr.trimmed();
}

QString FullTextSearcherPrivate::dealKeywordEx(const QString &keyword)
{
auto key = keyword;
for(int i = 0; i < key.length(); i++) {
if(QChar(key[i]).isPrint()) {
key.insert(i, "\\");
i++;
}
}

return key;
}

FullTextSearcher::FullTextSearcher(const QUrl &url, const QString &key, QObject *parent)
: AbstractSearcher(url, key, parent),
d(new FullTextSearcherPrivate(this))
Expand Down Expand Up @@ -477,7 +489,7 @@ bool FullTextSearcher::search()
return false;

const QString path = UrlRoute::urlToPath(searchUrl);
const QString key = d->dealKeyword(keyword);
const QString key = d->dealKeywordEx(keyword).trimmed();
if (path.isEmpty() || key.isEmpty()) {
d->status.storeRelease(kCompleted);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FullTextSearcherPrivate : public QObject

Lucene::DocumentPtr fileDocument(const QString &file);
QString dealKeyword(const QString &keyword);
QString dealKeywordEx(const QString &keyword);
void doIndexTask(const Lucene::IndexReaderPtr &reader, const Lucene::IndexWriterPtr &writer, const QString &path, TaskType type);
void indexDocs(const Lucene::IndexWriterPtr &writer, const QString &file, IndexType type);
bool checkUpdate(const Lucene::IndexReaderPtr &reader, const QString &file, IndexType &type);
Expand Down

0 comments on commit 597b5a1

Please sign in to comment.