[Groonga-commit] groonga/gcs [master] q: support phrase term

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 3日 (金) 22:14:05 JST


Kouhei Sutou	2012-08-03 22:14:05 +0900 (Fri, 03 Aug 2012)

  New Revision: ecbf3104299358bb19267974b491506dcc474824
  https://github.com/groonga/gcs/commit/ecbf3104299358bb19267974b491506dcc474824

  Log:
    q: support phrase term

  Modified files:
    lib/q-translator.js
    test/q-translator.test.js

  Modified: lib/q-translator.js (+21 -0)
===================================================================
--- lib/q-translator.js    2012-08-03 21:57:58 +0900 (2b9fa1d)
+++ lib/q-translator.js    2012-08-03 22:14:05 +0900 (e241476)
@@ -34,6 +34,27 @@ QueryTranslator.prototype = {
     }
     return this.defaultField + ":'" + escapeTerm(term) + "'";
   },
+  translatePhraseTerm: function() {
+    if (this.query[this.offset] != '"') {
+      this.thorwTranslateError("phrase must start with <\">");
+    }
+    this.offset++;
+    var phrase = "";
+    for (; this.offset < this.query.length; this.offset++) {
+      var character = this.query[this.offset];
+      if (character == '"') {
+        this.offset++;
+        return "'\"" + phrase + "\"'";
+      }
+
+      if (character == "\\") {
+        this.offset++;
+        character = this.query[this.offset];
+      }
+      phrase += character;
+    }
+    this.thorwTranslateError("phrase is unterminated");
+  },
   translateTerm: function() {
     this.skipSpaces(this.query, this);
     if (this.query[this.offset] == '"') {

  Modified: test/q-translator.test.js (+23 -0)
===================================================================
--- test/q-translator.test.js    2012-08-03 21:57:58 +0900 (fe230f9)
+++ test/q-translator.test.js    2012-08-03 22:14:05 +0900 (a10b078)
@@ -23,6 +23,24 @@ function testIndividualTerm(label, individualTerm,
   });
 }
 
+function testPhraseTerm(label, phraseTerm,
+                        expectedOffset, expectedBooleanQuery) {
+  test('phrase term: ' + label + ': ' +
+       '<' + phraseTerm + '> -> <' + expectedBooleanQuery + '>', function() {
+    var translator = new QueryTranslator(phraseTerm);
+    translator.defaultField = "field";
+    var actualBooleanQuery = translator.translatePhraseTerm();
+    assert.deepEqual({
+                       booleanQuery: actualBooleanQuery,
+                       offset: translator.offset
+                     },
+                     {
+                       booleanQuery: expectedBooleanQuery,
+                       offset: expectedOffset
+                     });
+  });
+}
+
 function testTerm(label, term, expectedOffset, expectedBooleanQuery) {
   test('term: ' + label + ': ' +
        '<' + term + '> -> <' + expectedBooleanQuery + '>', function() {
@@ -50,6 +68,11 @@ suite('QueryTranslator', function() {
                      "let's".length,
                      "field:'let\\'s'");
 
+  testPhraseTerm("no special character",
+                 '"star wars" luke',
+                 '"star wars"'.length,
+                 "'\"star wars\"'");
+
   testTerm("a term",
            "  star wars",
            "  star".length,
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Back to archive index