mongoid_search 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,7 +26,8 @@ module Mongoid::Search
26
26
  self.search_fields = (self.search_fields || []).concat args
27
27
 
28
28
  field :_keywords, :type => Array
29
- index :_keywords, :background => true
29
+
30
+ Gem.loaded_specs["mongoid"].version.to_s.include?("3.0") ? (index({_keywords: 1}, {background: true})) : (index :_keywords, :background => true)
30
31
 
31
32
  before_save :set_keywords
32
33
  end
@@ -97,11 +98,8 @@ module Mongoid::Search
97
98
  end
98
99
  end
99
100
 
100
- module InstanceMethods #:nodoc:
101
- # Indexes the document keywords
102
- def index_keywords!
103
- update_attribute(:_keywords, set_keywords)
104
- end
101
+ def index_keywords!
102
+ update_attribute(:_keywords, set_keywords)
105
103
  end
106
104
 
107
105
  private
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Util
2
3
 
3
4
  def self.keywords(klass, field, stem_keywords, ignore_list)
@@ -12,6 +13,12 @@ module Util
12
13
  else
13
14
  attribute.map(&method).map { |t| Util.normalize_keywords t, stem_keywords, ignore_list }
14
15
  end
16
+ elsif attribute.is_a?(Hash)
17
+ if method.is_a?(Array)
18
+ method.map {|m| Util.normalize_keywords attribute[m.to_sym], stem_keywords, ignore_list }
19
+ else
20
+ Util.normalize_keywords(attribute[method.to_sym], stem_keywords, ignore_list)
21
+ end
15
22
  else
16
23
  Util.normalize_keywords(attribute.send(method), stem_keywords, ignore_list)
17
24
  end
@@ -25,14 +32,17 @@ module Util
25
32
  end
26
33
 
27
34
  def self.normalize_keywords(text, stem_keywords, ignore_list)
35
+ ligatures = {"œ"=>"oe", "æ"=>"ae"}
36
+
28
37
  return [] if text.blank?
29
38
  text = text.to_s.
30
39
  mb_chars.
31
40
  normalize(:kd).
41
+ downcase.
32
42
  to_s.
33
43
  gsub(/[._:;'"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/, ' '). # strip punctuation
34
44
  gsub(/[^[:alnum:]\s]/,''). # strip accents
35
- downcase.
45
+ gsub(/[#{ligatures.keys.join("")}]/) {|c| ligatures[c]}.
36
46
  split(' ').
37
47
  reject { |word| word.size < 2 }
38
48
  text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
@@ -4,10 +4,12 @@ class Product
4
4
  field :brand
5
5
  field :name
6
6
  field :attrs, :type => Array
7
+ field :info, :type => Hash
7
8
 
8
9
  references_many :tags
9
10
  referenced_in :category
10
11
  embeds_many :subproducts
11
12
 
12
- search_in :brand, :name, :outlet, :attrs, :tags => :name, :category => :name, :subproducts => [:brand, :name]
13
+ search_in :brand, :name, :outlet, :attrs, :tags => :name, :category => :name,
14
+ :subproducts => [:brand, :name], :info => [ :summary, :description ]
13
15
  end
@@ -11,7 +11,30 @@ describe Mongoid::Search do
11
11
  :name => "iPhone",
12
12
  :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) },
13
13
  :category => Category.new(:name => "Mobile"),
14
- :subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")]
14
+ :subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")],
15
+ :info => { :summary => "Info-summary",
16
+ :description => "Info-description"}
17
+ end
18
+
19
+ describe "Serialized hash fields" do
20
+ context "when the hash is populated" do
21
+ it "should return the product" do
22
+ Product.search("Info-summary").first.should eq @product
23
+ Product.search("Info-description").first.should eq @product
24
+ end
25
+ end
26
+
27
+ context "when the hash is empty" do
28
+ before(:each) do
29
+ @product.info = nil
30
+ @product.save
31
+ end
32
+
33
+ it "should not return the product" do
34
+ Product.search("Info-description").size.should eq 0
35
+ Product.search("Info-summary").size.should eq 0
36
+ end
37
+ end
15
38
  end
16
39
 
17
40
  context "utf-8 characters" do
@@ -26,7 +49,11 @@ describe Mongoid::Search do
26
49
  }
27
50
 
28
51
  it "should leave utf8 characters" do
29
- @product._keywords.should == ["amazing", "awesome", "ole", "Процессор", "Эльбрус", "процессоры"]
52
+ @product._keywords.should == ["amazing", "awesome", "ole", "процессор", "процессоры", "эльбрус"]
53
+ end
54
+
55
+ it "should return results in search when case doesn't match" do
56
+ Product.search("ЭЛЬБРУС").size.should == 1
30
57
  end
31
58
  end
32
59
 
@@ -36,7 +63,7 @@ describe Mongoid::Search do
36
63
  lambda { Product.create! }.should_not raise_error
37
64
  end
38
65
  end
39
-
66
+
40
67
  subject { Product.create :brand => "Apple", :name => "iPhone" }
41
68
 
42
69
  its(:_keywords) { should == ["apple", "iphone"] }
@@ -59,16 +86,26 @@ describe Mongoid::Search do
59
86
  Variant.search(:name => 'Apple', :color => :white).should eq [variant]
60
87
  end
61
88
 
89
+ it "should expand the ligature to ease searching" do
90
+ # ref: http://en.wikipedia.org/wiki/Typographic_ligature, only for french right now. Rules for other languages are not know
91
+ variant1 = Variant.create :tags => ["œuvre"].map {|tag| Tag.new(:name => tag)}
92
+ variant2 = Variant.create :tags => ["æquo"].map {|tag| Tag.new(:name => tag)}
93
+
94
+ Variant.search("œuvre").should eq [variant1]
95
+ Variant.search("oeuvre").should eq [variant1]
96
+ Variant.search("æquo").should eq [variant2]
97
+ Variant.search("aequo").should eq [variant2]
98
+ end
62
99
  it "should set the _keywords field with stemmed words if stem is enabled" do
63
100
  Product.stem_keywords = true
64
101
  @product.save!
65
- @product._keywords.should == ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "ol"]
102
+ @product._keywords.sort.should == ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "ol", "info", "descript", "summari"].sort
66
103
  end
67
104
 
68
105
  it "should ignore keywords in an ignore list" do
69
106
  Product.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]
70
107
  @product.save!
71
- @product._keywords.should == ["apple", "craddle", "iphone", "mobile", "ole"]
108
+ @product._keywords.sort.should == ["apple", "craddle", "iphone", "mobile", "ole", "info", "description", "summary"].sort
72
109
  end
73
110
 
74
111
  it "should incorporate numbers as keywords" do
@@ -153,5 +190,5 @@ describe Mongoid::Search do
153
190
  Product.index_keywords!.should_not include(false)
154
191
  end
155
192
 
156
-
193
+
157
194
  end
data/spec/util_spec.rb CHANGED
@@ -22,6 +22,10 @@ describe Util do
22
22
  Util.normalize_keywords("CaFé", false, "").should == ["cafe"]
23
23
  end
24
24
 
25
+ it "should downcase utf-8 chars of the text passed" do
26
+ Util.normalize_keywords("Кафе", false, "").should == ["кафе"]
27
+ end
28
+
25
29
  it "should split whitespaces, hifens, dots, underlines, etc.." do
26
30
  Util.normalize_keywords("CaFé-express.com delicious;come visit, and 'win' an \"iPad\"", false, "").should == ["cafe", "express", "com", "delicious", "come", "visit", "and", "win", "an", "ipad"]
27
31
  end
metadata CHANGED
@@ -1,114 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongoid_search
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 7
9
- version: 0.2.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.8
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Mauricio Zaffari
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-08-10 00:00:00 -03:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-07-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: mongoid
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 0
30
- - 0
16
+ requirement: &70364721915420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
31
21
  version: 2.0.0
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: bson_ext
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 1
43
- - 2
44
- version: "1.2"
24
+ version_requirements: *70364721915420
25
+ - !ruby/object:Gem::Dependency
26
+ name: bson_ext
27
+ requirement: &70364721930900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '1.2'
45
33
  type: :runtime
46
- version_requirements: *id002
47
- - !ruby/object:Gem::Dependency
48
- name: fast-stemmer
49
34
  prerelease: false
50
- requirement: &id003 !ruby/object:Gem::Requirement
51
- requirements:
35
+ version_requirements: *70364721930900
36
+ - !ruby/object:Gem::Dependency
37
+ name: fast-stemmer
38
+ requirement: &70364721929380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
52
41
  - - ~>
53
- - !ruby/object:Gem::Version
54
- segments:
55
- - 1
56
- - 0
57
- - 0
42
+ - !ruby/object:Gem::Version
58
43
  version: 1.0.0
59
44
  type: :runtime
60
- version_requirements: *id003
61
- - !ruby/object:Gem::Dependency
62
- name: database_cleaner
63
45
  prerelease: false
64
- requirement: &id004 !ruby/object:Gem::Requirement
65
- requirements:
46
+ version_requirements: *70364721929380
47
+ - !ruby/object:Gem::Dependency
48
+ name: database_cleaner
49
+ requirement: &70364721927540 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
66
52
  - - ~>
67
- - !ruby/object:Gem::Version
68
- segments:
69
- - 0
70
- - 6
71
- - 4
53
+ - !ruby/object:Gem::Version
72
54
  version: 0.6.4
73
55
  type: :development
74
- version_requirements: *id004
75
- - !ruby/object:Gem::Dependency
76
- name: rake
77
56
  prerelease: false
78
- requirement: &id005 !ruby/object:Gem::Requirement
79
- requirements:
57
+ version_requirements: *70364721927540
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70364721926220 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
80
63
  - - ~>
81
- - !ruby/object:Gem::Version
82
- segments:
83
- - 0
84
- - 8
85
- - 7
64
+ - !ruby/object:Gem::Version
86
65
  version: 0.8.7
87
66
  type: :development
88
- version_requirements: *id005
89
- - !ruby/object:Gem::Dependency
90
- name: rspec
91
67
  prerelease: false
92
- requirement: &id006 !ruby/object:Gem::Requirement
93
- requirements:
68
+ version_requirements: *70364721926220
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &70364721925360 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
94
74
  - - ~>
95
- - !ruby/object:Gem::Version
96
- segments:
97
- - 2
98
- - 4
99
- version: "2.4"
75
+ - !ruby/object:Gem::Version
76
+ version: '2.4'
100
77
  type: :development
101
- version_requirements: *id006
78
+ prerelease: false
79
+ version_requirements: *70364721925360
102
80
  description: Simple full text search implementation.
103
- email:
81
+ email:
104
82
  - mauricio@papodenerd.net
105
83
  executables: []
106
-
107
84
  extensions: []
108
-
109
85
  extra_rdoc_files: []
110
-
111
- files:
86
+ files:
112
87
  - lib/mongoid_search/log.rb
113
88
  - lib/mongoid_search/mongoid_search.rb
114
89
  - lib/mongoid_search/railtie.rb
@@ -118,39 +93,40 @@ files:
118
93
  - README.md
119
94
  - Rakefile
120
95
  - VERSION
121
- has_rdoc: true
96
+ - spec/config/ignorelist.yml
97
+ - spec/models/category.rb
98
+ - spec/models/product.rb
99
+ - spec/models/subproduct.rb
100
+ - spec/models/tag.rb
101
+ - spec/models/variant.rb
102
+ - spec/mongoid_search_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/util_spec.rb
122
105
  homepage: http://www.papodenerd.net/mongoid-search-full-text-search-for-your-mongoid-models/
123
106
  licenses: []
124
-
125
107
  post_install_message:
126
108
  rdoc_options: []
127
-
128
- require_paths:
109
+ require_paths:
129
110
  - lib
130
- required_ruby_version: !ruby/object:Gem::Requirement
131
- requirements:
132
- - - ">="
133
- - !ruby/object:Gem::Version
134
- segments:
135
- - 0
136
- version: "0"
137
- required_rubygems_version: !ruby/object:Gem::Requirement
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- segments:
142
- - 1
143
- - 3
144
- - 6
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
145
122
  version: 1.3.6
146
123
  requirements: []
147
-
148
124
  rubyforge_project:
149
- rubygems_version: 1.3.6
125
+ rubygems_version: 1.8.11
150
126
  signing_key:
151
127
  specification_version: 3
152
128
  summary: Search implementation for Mongoid ORM
153
- test_files:
129
+ test_files:
154
130
  - spec/config/ignorelist.yml
155
131
  - spec/models/category.rb
156
132
  - spec/models/product.rb