Просмотр исходного кода

Supported minus and phrase search

Norio Suzuki 9 лет назад
Родитель
Сommit
d2ddcbf5c8
1 измененных файлов с 27 добавлено и 4 удалено
  1. 27 4
      lib/util/search.js

+ 27 - 4
lib/util/search.js

@@ -393,10 +393,26 @@ SearchClient.prototype.appendCriteriaForKeywordContains = function(query, keywor
       });
       });
     });
     });
 
 
-    // minus + phrase (ex. -"foo bar" ) is not support yet.
-
     query.body.query.bool.must.push(phraseQueries);
     query.body.query.bool.must.push(phraseQueries);
   }
   }
+
+  if (parsedKeywords.not_phrase.length > 0) {
+    var notPhraseQueries = [];
+    parsedKeywords.not_phrase.forEach(function(phrase) {
+      notPhraseQueries.push({
+        multi_match: {
+          query: phrase, // each phrase is quoteted words
+          type: 'phrase',
+          fields: [ // Not use "*.ja" fields here, because we want to analyze (parse) search words
+            "path_raw^2",
+            "body_raw",
+          ],
+        }
+      });
+    });
+
+    query.body.query.bool.must_not.push(notPhraseQueries);
+  }
 };
 };
 
 
 SearchClient.prototype.appendCriteriaForPathFilter = function(query, path)
 SearchClient.prototype.appendCriteriaForPathFilter = function(query, path)
@@ -453,19 +469,25 @@ SearchClient.prototype.getParsedKeywords = function(keyword)
   var matchWords = [];
   var matchWords = [];
   var notMatchWords = [];
   var notMatchWords = [];
   var phraseWords = [];
   var phraseWords = [];
+  var notPhraseWords = [];
 
 
   keyword.trim();
   keyword.trim();
   keyword = keyword.replace(/\s+/g, ' ');
   keyword = keyword.replace(/\s+/g, ' ');
 
 
   // First: Parse phrase keywords
   // First: Parse phrase keywords
-  var phraseRegExp = new RegExp(/("[^"]+")/g);
+  var phraseRegExp = new RegExp(/(-?"[^"]+")/g);
   var phrases = keyword.match(phraseRegExp);
   var phrases = keyword.match(phraseRegExp);
+
   if (phrases !== null) {
   if (phrases !== null) {
     keyword = keyword.replace(phraseRegExp, '');
     keyword = keyword.replace(phraseRegExp, '');
 
 
     phrases.forEach(function(phrase) {
     phrases.forEach(function(phrase) {
       phrase.trim();
       phrase.trim();
-      phraseWords.push(phrase);
+      if (phrase.match(/^\-/)) {
+        notPhraseWords.push(phrase.replace(/^\-/, ''));
+      } else {
+        phraseWords.push(phrase);
+      }
     });
     });
   }
   }
 
 
@@ -486,6 +508,7 @@ SearchClient.prototype.getParsedKeywords = function(keyword)
     match: matchWords,
     match: matchWords,
     not_match: notMatchWords,
     not_match: notMatchWords,
     phrase: phraseWords,
     phrase: phraseWords,
+    not_phrase: notPhraseWords,
   };
   };
 }
 }