[Groonga-commit] groonga/gcs [master] Add QueryTranslator

Zurück zum Archiv-Index

null+****@clear***** null+****@clear*****
2012年 8月 1日 (水) 16:54:31 JST


Yoji SHIDARA	2012-08-01 16:54:31 +0900 (Wed, 01 Aug 2012)

  New Revision: 0191b24ca3822335020c38d69d612bf75859deef
  https://github.com/groonga/gcs/commit/0191b24ca3822335020c38d69d612bf75859deef

  Log:
    Add QueryTranslator
    
    Only Individual Terms are supported for now.

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

  Added: lib/q-translator.js (+52 -0) 100644
===================================================================
--- /dev/null
+++ lib/q-translator.js    2012-08-01 16:54:31 +0900 (c34b0a8)
@@ -0,0 +1,52 @@
+// -*- indent-tabs-mode: nil; js2-basic-offset: 2 -*-
+/*
+  QueryTranslator translates Queries into Boolean Queries in Amazon
+  CloudSearch.
+
+  Expression Syntax for Queries:
+    http://docs.amazonwebservices.com/cloudsearch/latest/developerguide/Search.Requests.html#Search.ReqParams
+
+  Expression Syntax for Boolean Queries:
+    http://docs.amazonwebservices.com/cloudsearch/latest/developerguide/Search.Requests.html#Search.MatchSetExpression
+
+  FIXME: Returns null if the given query is not supported.
+         Shuold raise.
+*/
+
+
+function QueryTranslator() {
+}
+
+function throwTranslateError(query, context, detail) {
+  var message = "";
+  message += "<";
+  message += query.substring(0, context.offset);
+  message += "|" + query[context.offset] + "|";
+  message += query.substring(context.offset + 1);
+  message += ">";
+  message += ": " + detail;
+  throw new Error(message);
+}
+
+QueryTranslator.prototype = {
+  translateIndividualTerm: function(query, context) {
+    var term = '';
+    for (; context.offset < query.length; context.offset++) {
+      if (/[^ \+\-\|]/.test(query[context.offset])) {
+        term += query[context.offset];
+      } else {
+        break;
+      }
+    }
+    return context.defaultField + ":'" + term + "'";
+  },
+  skipSpaces: function(query, context) {
+    for (; context.offset < query.length; context.offset++) {
+      if (query[context.offset] != " ") {
+        return;
+      }
+    }
+  }
+};
+
+exports.QueryTranslator = QueryTranslator;

  Added: test/q-translator.test.js (+35 -0) 100644
===================================================================
--- /dev/null
+++ test/q-translator.test.js    2012-08-01 16:54:31 +0900 (21b99e6)
@@ -0,0 +1,35 @@
+// -*- indent-tabs-mode: nil; js2-basic-offset: 2 -*-
+
+var utils = require('./test-utils');
+var assert = require('chai').assert;
+
+var QueryTranslator = require('../lib/q-translator').QueryTranslator;
+
+function testIndividualTerm(label, individualTerm, expectedBooleanQuery,
+                            expectedOffset) {
+  test('term: ' + label + ': ' +
+       '<' + individualTerm + '> -> <' + expectedBooleanQuery + '>', function() {
+    var translator = new QueryTranslator();
+    var context = {
+      offset: 0,
+      defaultField: 'field'
+    };
+    var actualBooleanQuery =
+          translator.translateIndividualTerm(individualTerm, context);
+    assert.deepEqual({
+                       booleanQuery: actualBooleanQuery,
+                       offset: context.offset
+                     },
+                     {
+                       booleanQuery: expectedBooleanQuery,
+                       offset: expectedOffset
+                     });
+  });
+}
+
+suite('QueryTranslator', function() {
+  testIndividualTerm("an individual term",
+                     "star wars",
+                     "field:'star'",
+                     "star".length);
+});
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Zurück zum Archiv-Index