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

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 27日 (金) 16:18:51 JST


Kouhei Sutou	2012-07-27 16:18:51 +0900 (Fri, 27 Jul 2012)

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

  Log:
    BqTranslator: support phrase

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

  Modified: lib/bq-translator.js (+56 -5)
===================================================================
--- lib/bq-translator.js    2012-07-27 15:56:25 +0900 (3a6cfcd)
+++ lib/bq-translator.js    2012-07-27 16:18:51 +0900 (df33afd)
@@ -19,6 +19,11 @@ BooleanQueryTranslator.prototype = {
   },
   translateExpression: function(query, context) {
     if (query[context.offset] == "'") {
+      if (query[context.offset + 1] == "\"") {
+        return this.translateExpressionValuePhrase(query,
+                                                   context.defaultField,
+                                                   context);
+      }
       return this.translateExpressionValueString(query,
                                                  context.defaultField,
                                                  context);
@@ -30,22 +35,67 @@ BooleanQueryTranslator.prototype = {
     }
     return "";
   },
+  translateExpressionValuePhrase: function(query, field, context) {
+    if (!(query[context.offset] == "'" && query[context.offset + 1] == "\"")) {
+      // TODO: report error
+      return "";
+    }
+
+    context.offset += 2;
+    var value = "";
+    for (; context.offset < query.length; context.offset++) {
+      var character = query[context.offset];
+      if (character == "'") {
+        // TODO: report error: missing close quote <">
+        return "";
+      }
+
+      if (character == "\\") {
+        context.offset++;
+        character = query[context.offset];
+        value += character;
+      } else if (character == "\"") {
+        context.offset++;
+        if (query[context.offset] != "'") {
+          // TODO: report error: missing close quote <'> after <">
+          return "";
+        }
+        context.offset++;
+        return field + " @ " + "\"" + value + "\"";
+      } else {
+        value += character;
+      }
+    }
+
+    // TODO: report error: missing close quote <"'>
+    return "";
+  },
   translateExpressionValueString: function(query, field, context) {
     if (query[context.offset] != "'") {
       // TODO: report error
       return "";
     }
 
-    var value = "\"";
     context.offset++;
+    var values = [];
+    var value = "";
     for (; context.offset < query.length; context.offset++) {
       var character = query[context.offset];
       if (character == "'") {
         context.offset++;
-        break;
+        values.push("\"" + value + "\"");
+        var expressions = values.map(function (value) {
+          return field + " @ " + value;
+        });
+        return expressions.join(" && ");
       }
 
-      if (character == "\\") {
+      if (character == " ") {
+        if (value.length > 0) {
+          values.push("\"" + value + "\"");
+          value = "";
+        }
+      } else if (character == "\\") {
         context.offset++;
         character = query[context.offset];
         value += character;
@@ -55,8 +105,9 @@ BooleanQueryTranslator.prototype = {
         value += character;
       }
     }
-    value += "\"";
-    return field + " @ " + value;
+
+    // TODO: report error: missing close quote <'>
+    return "";
   },
   translateExpressionValueUnsignedInteger: function(query, field, context) {
     var value = "";

  Modified: test/bq-translator.test.js (+6 -2)
===================================================================
--- test/bq-translator.test.js    2012-07-27 15:56:25 +0900 (aff2d3f)
+++ test/bq-translator.test.js    2012-07-27 16:18:51 +0900 (a8da45b)
@@ -45,10 +45,14 @@ suite('BoolanQueryTranslator', function() {
             '"query query" type:"ModelName"',
             "(and 'query query' type:'ModelName')");
 
-  testExpression("value only: stirng",
-                 "field @ \"keyword1 keyword2\"",
+  testExpression("value only: stirng: keywords",
+                 "field @ \"keyword1\" && field @ \"keyword2\"",
                  "'keyword1 keyword2'".length,
                  "'keyword1 keyword2' 'other keyword'");
+  testExpression("value only: stirng: phrase",
+                 "field @ \"keyword1 keyword2\"",
+                 "'\"keyword1 keyword2\"'".length,
+                 "'\"keyword1 keyword2\"' 'other keyword'");
   testExpression("value only: unsigned integer",
                  "field == 29",
                  "29".length,
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
下载 



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