[Groonga-commit] groonga/gcs [master] Use synonyms table for search API. Refs. #35

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 11日 (水) 19:43:15 JST


Yoji SHIDARA	2012-07-11 19:43:15 +0900 (Wed, 11 Jul 2012)

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

  Log:
    Use synonyms table for search API. Refs. #35

  Added files:
    test/fixture/companies/synonyms.grn
  Modified files:
    lib/api/2011-02-01/search.js
    test/search.test.js

  Modified: lib/api/2011-02-01/search.js (+16 -0)
===================================================================
--- lib/api/2011-02-01/search.js    2012-07-11 19:23:52 +0900 (9514317)
+++ lib/api/2011-02-01/search.js    2012-07-11 19:43:15 +0900 (11cb240)
@@ -32,6 +32,18 @@ function select(database, options, callback) {
   });
 }
 
+function isSynonymTableAvailableSync(database, domain) {
+  var tables = database.commandSync('table_list');
+  var tableFound = false;
+  Database.formatResults(tables).forEach(function(table) {
+    if (table.name === domain.synonymTableName) {
+      tableFound = true;
+      return;
+    }
+  });
+  return tableFound;
+}
+
 exports.createHandler = function(database) {
   return function(request, response) {
     var startedAt = new Date();
@@ -56,6 +68,10 @@ exports.createHandler = function(database) {
       match_columns: matchColumns
     };
 
+    if (isSynonymTableAvailableSync(database, domain)) {
+      options.query_expansion = domain.synonymTableName + '.synonyms';
+    }
+
     select(database, options, function(error, data, numFoundRecords) {
       var finishedAt = new Date();
       var elapsedTime = finishedAt - startedAt;

  Added: test/fixture/companies/synonyms.grn (+8 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/companies/synonyms.grn    2012-07-11 19:43:15 +0900 (11b78fd)
@@ -0,0 +1,8 @@
+table_create companies_synonyms TABLE_HASH_KEY ShortText
+column_create companies_synonyms synonyms COLUMN_VECTOR ShortText
+load --table companies_synonyms
+[
+["_key","synonyms"],
+["tokio",["tokyo"]],
+["dekkaido",["hokkaido"]]
+]

  Modified: test/search.test.js (+105 -2)
===================================================================
--- test/search.test.js    2012-07-11 19:23:52 +0900 (c6c80af)
+++ test/search.test.js    2012-07-11 19:43:15 +0900 (fe9aac4)
@@ -20,8 +20,8 @@ suite('Search API', function() {
     server.close();
   });
 
-  function testSearch(path, host, callback) {
-    test('GET ' + path, function(done) {
+  function testSearch(path, message, host, callback) {
+    test('GET ' + path + ' ' + message, function(done) {
       var options = {
         host: utils.testHost,
         port: utils.testPort,
@@ -47,6 +47,7 @@ suite('Search API', function() {
     });
 
     testSearch('/2011-02-01/search?q=Hongo',
+               'should hit one entry',
                'search-companies-00000000000000000000000000.localhost',
       function(response, body, done) {
         var actual = JSON.parse(body);
@@ -84,6 +85,7 @@ suite('Search API', function() {
     );
 
     testSearch('/2011-02-01/search?q=Tokyo',
+               'should hit three entries',
                'search-companies-00000000000000000000000000.localhost',
       function(response, body, done) {
         var actual = JSON.parse(body);
@@ -149,6 +151,7 @@ suite('Search API', function() {
     );
 
     testSearch('/2011-02-01/search?q=Tokyo&size=2',
+               'should return two hit entries',
                'search-companies-00000000000000000000000000.localhost',
       function(response, body, done) {
         var actual = JSON.parse(body);
@@ -201,6 +204,7 @@ suite('Search API', function() {
     );
 
     testSearch('/2011-02-01/search?q=Tokyo&start=1',
+               'should return offseted hit result',
                'search-companies-00000000000000000000000000.localhost',
       function(response, body, done) {
         var actual = JSON.parse(body);
@@ -250,5 +254,104 @@ suite('Search API', function() {
         done();
       }
     );
+
+    testSearch('/2011-02-01/search?q=tokio',
+               'should not match with any entry',
+               'search-companies-00000000000000000000000000.localhost',
+      function(response, body, done) {
+        var actual = JSON.parse(body);
+        actual.info['time-ms'] = 0; // always set 0 for test
+        var expected = {
+          rank: '-text_relevance',
+          'match-expr': '',
+          hits: {
+            found: 0,
+            start: 0,
+            hit: [
+            ]
+          },
+          info: {
+            rid: '000000000000000000000000000000000000000000000000000000000000000',
+            'time-ms': 0, // always 0
+            'cpu-time-ms': 0
+          }
+        };
+        assert.deepEqual(actual, expected);
+        done();
+      }
+    );
+  });
+
+  suite('with fixture and synonyms loaded', function() {
+    setup(function() {
+      utils.loadDumpFile(database, __dirname + '/fixture/companies/ddl.grn');
+      utils.loadDumpFile(database, __dirname + '/fixture/companies/data.grn');
+      utils.loadDumpFile(database, __dirname + '/fixture/companies/synonyms.grn');
+    });
+
+    testSearch('/2011-02-01/search?q=tokio',
+               'should match with using synonyms',
+               'search-companies-00000000000000000000000000.localhost',
+      function(response, body, done) {
+        var actual = JSON.parse(body);
+        actual.info['time-ms'] = 0; // always set 0 for test
+        var expected = {
+          rank: '-text_relevance',
+          'match-expr': '',
+          hits: {
+            found: 3,
+            start: 0,
+            hit: [
+              {
+                id: 'id1',
+                data: {
+                  _id: [1],
+                  _key: ['id1'],
+                  address: ['Shibuya, Tokyo, Japan'],
+                  description: [''],
+                  email_address: ['info****@razil*****'],
+                  name: ['Brazil'],
+                  age: [1],
+                  product: ['groonga']
+                }
+              },
+              {
+                id: 'id3',
+                data: {
+                  _id: [3],
+                  _key: ['id3'],
+                  address: ['Hongo, Tokyo, Japan'],
+                  description: [''],
+                  email_address: ['info****@clear*****'],
+                  name: ['ClearCode Inc.'],
+                  age: [3],
+                  product: ['groonga']
+                }
+              },
+              {
+                id: 'id9',
+                data: {
+                  _id: [9],
+                  _key: ['id9'],
+                  address: ['Tokyo, Japan'],
+                  description: [''],
+                  email_address: [''],
+                  name: ['Umbrella Corporation'],
+                  age: [9],
+                  product: ['tyrant']
+                }
+              }
+            ]
+          },
+          info: {
+            rid: '000000000000000000000000000000000000000000000000000000000000000',
+            'time-ms': 0, // always 0
+            'cpu-time-ms': 0
+          }
+        };
+        assert.deepEqual(actual, expected);
+        done();
+      }
+    );
   });
 });
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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