[Groonga-commit] ranguba/rroonga at ec5e2c4 [master] Add :mode and :weight options to IndexColumn#search

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Mar 25 23:41:12 JST 2014


Kouhei Sutou	2014-03-25 23:41:12 +0900 (Tue, 25 Mar 2014)

  New Revision: ec5e2c49b9fc61c23583064e25c0b7a1c0b5206c
  https://github.com/ranguba/rroonga/commit/ec5e2c49b9fc61c23583064e25c0b7a1c0b5206c

  Message:
    Add :mode and :weight options to IndexColumn#search

  Modified files:
    ext/groonga/rb-grn-index-column.c
    test/test-index-column.rb

  Modified: ext/groonga/rb-grn-index-column.c (+23 -4)
===================================================================
--- ext/groonga/rb-grn-index-column.c    2014-03-21 17:45:07 +0900 (b293975)
+++ ext/groonga/rb-grn-index-column.c    2014-03-25 23:41:12 +0900 (fb710fd)
@@ -666,15 +666,16 @@ rb_grn_index_column_search (int argc, VALUE *argv, VALUE self)
     grn_obj *query = NULL, *id_query = NULL, *string_query = NULL;
     grn_obj *result;
     grn_operator operator;
+    grn_search_optarg options;
     grn_rc rc;
-    VALUE rb_query, options, rb_result, rb_operator;
+    VALUE rb_query, rb_options, rb_result, rb_operator, rb_mode, rb_weight;
 
     rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                     NULL, NULL,
                                     NULL, NULL, NULL, NULL, &range,
                                     &id_query, &string_query);
 
-    rb_scan_args(argc, argv, "11", &rb_query, &options);
+    rb_scan_args(argc, argv, "11", &rb_query, &rb_options);
 
     if (CBOOL2RVAL(rb_obj_is_kind_of(rb_query, rb_cInteger))) {
         grn_id id;
@@ -688,9 +689,11 @@ rb_grn_index_column_search (int argc, VALUE *argv, VALUE self)
         query = string_query;
     }
 
-    rb_grn_scan_options(options,
+    rb_grn_scan_options(rb_options,
                         "result", &rb_result,
                         "operator", &rb_operator,
+                        "mode", &rb_mode,
+                        "weight", &rb_weight,
                         NULL);
 
     if (NIL_P(rb_result)) {
@@ -705,7 +708,23 @@ rb_grn_index_column_search (int argc, VALUE *argv, VALUE self)
 
     operator = RVAL2GRNOPERATOR(rb_operator);
 
-    rc = grn_obj_search(context, column, query, result, operator, NULL);
+    if (NIL_P(rb_mode)) {
+        options.mode = GRN_OP_EXACT;
+    } else {
+        options.mode = RVAL2GRNOPERATOR(rb_mode);
+    }
+    options.similarity_threshold = 0;
+    options.max_interval = 0;
+    options.weight_vector = NULL;
+    if (NIL_P(rb_weight)) {
+        options.vector_size = 0;
+    } else {
+        options.vector_size = NUM2UINT(rb_weight);
+    }
+    options.proc = NULL;
+    options.max_size = 0;
+
+    rc = grn_obj_search(context, column, query, result, operator, &options);
     rb_grn_rc_check(rc, self);
 
     return rb_result;

  Modified: test/test-index-column.rb (+146 -0)
===================================================================
--- test/test-index-column.rb    2014-03-21 17:45:07 +0900 (963793e)
+++ test/test-index-column.rb    2014-03-25 23:41:12 +0900 (c4c2aa6)
@@ -349,4 +349,150 @@ class IndexColumnTest < Test::Unit::TestCase
                    @index.disk_usage)
     end
   end
+
+  class SearchTest < self
+    class WeightTest < self
+      def setup
+        super
+        setup_schema
+      end
+
+      def setup_schema
+        Groonga::Schema.define do |schema|
+          schema.create_table("Memos",
+                              :type => :hash,
+                              :key_type => :short_text) do |table|
+            table.short_text("tags",
+                             :type => :vector,
+                             :with_weight => true)
+          end
+
+          schema.create_table("Tags",
+                              :type => :hash,
+                              :key_type => :short_text) do |table|
+            table.index("Memos.tags",
+                        :name => "memos_index",
+                        :with_weight => true)
+          end
+        end
+
+        @memos = context["Memos"]
+        @index = context["Tags.memos_index"]
+      end
+
+      def search(keyword, options={})
+        @index.search(keyword, options).collect do |record|
+          {
+            :key => record._key,
+            :score => record.score,
+          }
+        end
+      end
+
+      def test_index
+        record =****@memos*****("Rroonga is fun!")
+        record.tags = [
+          {
+            :value => "rroonga",
+            :weight => 9,
+          }
+        ]
+
+        expected = [
+          {
+            :key => "Rroonga is fun!",
+            :score => 10,
+          }
+        ]
+        assert_equal(expected, search("rroonga"))
+      end
+
+      def test_search
+        record =****@memos*****("Rroonga is fun!")
+        record.tags = [
+          {
+            :value => "rroonga",
+          }
+        ]
+
+        expected = [
+          {
+            :key => "Rroonga is fun!",
+            :score => 5,
+          }
+        ]
+        assert_equal(expected, search("rroonga", :weight => 5))
+      end
+
+      def test_index_and_search
+        record =****@memos*****("Rroonga is fun!")
+        record.tags = [
+          {
+            :value => "rroonga",
+            :weight => 9,
+          }
+        ]
+
+        expected = [
+          {
+            :key => "Rroonga is fun!",
+            :score => 50
+          }
+        ]
+        assert_equal(expected, search("rroonga", :weight => 5))
+      end
+    end
+
+    class OperatorTest < self
+      def setup
+        super
+        setup_schema
+      end
+
+      def setup_schema
+        Groonga::Schema.define do |schema|
+          schema.create_table("Memos",
+                              :type => :hash,
+                              :key_type => :short_text) do |table|
+            table.short_text("tags", :type => :vector)
+          end
+
+          schema.create_table("Tags",
+                              :type => :hash,
+                              :key_type => :short_text) do |table|
+            table.index("Memos.tags",
+                        :name => "memos_index")
+          end
+        end
+
+        @memos = context["Memos"]
+        @index = context["Tags.memos_index"]
+      end
+
+      def test_adjust
+        @memos.add("Rroonga is fun!", :tags => ["rroonga", "groonga"])
+        @memos.add("Groonga is fast!", :tags => ["groonga"])
+
+        result =****@index*****("groonga")
+        @index.search("rroonga", :result => result, :operator => :adjust)
+        expected = [
+          {
+            :key => "Rroonga is fun!",
+            :score => 2,
+          },
+          {
+            :key => "Groonga is fast!",
+            :score => 1,
+          }
+        ]
+        actual = result.collect do |record|
+          {
+            :key => record._key,
+            :score => record.score,
+          }
+        end
+        assert_equal(expected, actual)
+      end
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index