Fix Assistant crash with operational words.
authorkh1 <karsten.heimrich@digia.com>
Tue, 26 Nov 2013 12:59:56 +0000 (13:59 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 27 Nov 2013 19:51:47 +0000 (20:51 +0100)
Phrase queries with a single term are not allow in clucene,
so once we encounter a search string like "and Next", which
in turn will result in a single word, we use a term query.

Task-number: QTBUG-34973

Change-Id: I9328abf0e96428577cdee15b1671393074cc91c2
Reviewed-by: Christian Kandeler <christian.kandeler@digia.com>
src/assistant/help/qhelpsearchindexreader_clucene.cpp

index 4fb4817..bc0b726 100644 (file)
@@ -321,27 +321,25 @@ bool QHelpSearchIndexReaderClucene::addWithoutQuery(const QHelpSearchQuery &quer
 bool QHelpSearchIndexReaderClucene::addPhraseQuery(const QHelpSearchQuery &query,
     const QString &fieldName, QCLuceneBooleanQuery &booleanQuery)
 {
-    bool queryIsValid = false;
-    const QString &term = query.wordList.at(0).toLower();
-    if (term.contains(QLatin1Char(' '))) {
-        const QStringList termList = term.split(QLatin1String(" "));
-        QCLucenePhraseQuery *q = new QCLucenePhraseQuery();
-        const QStringList stopWords = QCLuceneStopAnalyzer().englishStopWords();
-        foreach (const QString &term, termList) {
-            if (!stopWords.contains(term, Qt::CaseInsensitive))
-                q->addTerm(QCLuceneTerm(fieldName, term.toLower()));
-        }
-        if (!q->getTerms().isEmpty()) {
-            booleanQuery.add(q, true, true, false);
-            queryIsValid = true;
-        }
-    } else {
-        QCLuceneQuery *lQuery = new QCLuceneTermQuery(QCLuceneTerm(
-                fieldName, term.toLower()));
-        booleanQuery.add(lQuery, true, true, false);
-        queryIsValid = true;
+    const QString phrase = query.wordList.at(0).toLower();
+    QStringList terms = phrase.split(QLatin1String(" "));
+    foreach (const QString &word, QCLuceneStopAnalyzer().englishStopWords())
+        terms.removeAll(word);
+
+    if (terms.isEmpty())
+        return false;
+
+    if (terms.count() == 1) {
+        QCLuceneQuery *term = new QCLuceneTermQuery(QCLuceneTerm(fieldName, terms[0].toLower()));
+        booleanQuery.add(term, true, true, false);
+        return true;
     }
-    return queryIsValid;
+
+    QCLucenePhraseQuery *phraseQuery = new QCLucenePhraseQuery();
+    foreach (const QString &term, terms)
+        phraseQuery->addTerm(QCLuceneTerm(fieldName, term.toLower()));
+    booleanQuery.add(phraseQuery, true, true, false);
+    return true;
 }
 
 bool QHelpSearchIndexReaderClucene::addAllQuery(const QHelpSearchQuery &query,