[Groonga-commit] ranguba/rroonga at e01b64a [add-regexp-expression-builder] Add RegexpExpressionBuilder

Back to archive index

Masafumi Yokoyama null+****@clear*****
Wed Apr 8 12:17:20 JST 2015


Masafumi Yokoyama	2015-04-08 12:17:20 +0900 (Wed, 08 Apr 2015)

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

  Message:
    Add RegexpExpressionBuilder
    
    GitHub: #67

  Modified files:
    lib/groonga/expression-builder.rb
    test/test-expression-builder.rb

  Modified: lib/groonga/expression-builder.rb (+12 -0)
===================================================================
--- lib/groonga/expression-builder.rb    2015-04-08 11:17:14 +0900 (448c84f)
+++ lib/groonga/expression-builder.rb    2015-04-08 12:17:20 +0900 (5015223)
@@ -173,7 +173,12 @@ module Groonga
           raise ArgumentError,
                  "match word should not be nil: #{full_column_name}"
         end
+
+        if other.is_a?(Regexp)
+          RegexpExpressionBuilder.new(self, normalize(other.source))
+        else
         MatchExpressionBuilder.new(self, normalize(other))
+        end
       end
 
       def <(other)
@@ -333,6 +338,13 @@ module Groonga
     end
 
     # @private
+    class RegexpExpressionBuilder < BinaryExpressionBuilder
+      def initialize(column_value_builder, value)
+        super(Groonga::Operation::REGEXP, column_value_builder, value)
+      end
+    end
+
+    # @private
     class LessExpressionBuilder < BinaryExpressionBuilder
       def initialize(column_value_builder, value)
         super(Groonga::Operation::LESS, column_value_builder, value)

  Modified: test/test-expression-builder.rb (+61 -1)
===================================================================
--- test/test-expression-builder.rb    2015-04-08 11:17:14 +0900 (4294627)
+++ test/test-expression-builder.rb    2015-04-08 12:17:20 +0900 (05fd5e0)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2014  Masafumi Yokoyama <myokoym �� gmail.com>
+# Copyright (C) 2014-2015  Masafumi Yokoyama <yokoyama �� clear-code.com>
 # Copyright (C) 2009-2012  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
@@ -248,6 +248,66 @@ class ExpressionBuilderTest < Test::Unit::TestCase
     end
   end
 
+  class RegexpSearchTest < self
+    def setup_tables
+      Groonga::Schema.define do |schema|
+        schema.create_table("Users",
+                            :type => :hash,
+                            :key_type => "ShortText") do |table|
+          table.short_text("name")
+        end
+
+        schema.create_table("Terms",
+                            :type => :patricia_trie,
+                            :key_type => "ShortText",
+                            :default_tokenizer => "TokenRegexp",
+                            :normalizer => "NormalizerAuto") do |table|
+          table.index("Users.name")
+        end
+      end
+
+      @users = Groonga["Users"]
+    end
+
+    def setup_data
+      @users.add("sato",   :name => "taro sato")
+      @users.add("suzuki", :name => "Shiro SUZUKI")
+      @users.add("ito",    :name => "Takashi Ito")
+    end
+
+    class BlockTest < self
+      def test_match
+        result =****@users***** do |record|
+          record["name"] =~ /sh/
+        end
+        assert_equal(["suzuki", "ito"],
+                     result.collect {|record| record.key.key})
+      end
+
+      def test_not_match
+        result =****@users***** do |record|
+          record["name"] =~ /abcabcabc/
+        end
+        assert_equal([],
+                     result.collect {|record| record.key.key})
+      end
+    end
+
+    class QueryStringTest < self
+      def test_match
+        result =****@users*****("name:~t")
+        assert_equal(["sato", "ito"],
+                     result.collect {|record| record.key.key})
+      end
+
+      def test_not_match
+        result =****@users*****("name:~x")
+        assert_equal([],
+                     result.collect {|record| record.key.key})
+      end
+    end
+  end
+
   class PrefixSearchTest < self
     def setup_tables
       Groonga::Schema.define do |schema|
-------------- next part --------------
HTML����������������������������...
Télécharger 



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