mongoid_search 0.3.2 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +1 -1
- data/README.md +152 -105
- data/Rakefile +5 -2
- data/VERSION +1 -1
- data/lib/mongoid_search/log.rb +2 -1
- data/lib/mongoid_search/mongoid_search.rb +61 -42
- data/lib/mongoid_search/railtie.rb +2 -5
- data/lib/mongoid_search/tasks/mongoid_search.rake +16 -0
- data/lib/mongoid_search/util.rb +19 -18
- data/lib/mongoid_search.rb +14 -7
- data/spec/models/category.rb +2 -0
- data/spec/models/product.rb +15 -6
- data/spec/models/subproduct.rb +2 -1
- data/spec/models/tag.rb +3 -2
- data/spec/models/variant.rb +3 -1
- data/spec/mongoid_search_spec.rb +231 -133
- data/spec/spec_helper.rb +4 -1
- data/spec/util_spec.rb +28 -29
- metadata +56 -52
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 65d13fcd90c890df7b15f2175b701e951259f8de
|
|
4
|
+
data.tar.gz: '097843e60c25fc38ddb96789093dc798a08c5739'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a13b1d7f1bce08ffed931346ec1b1b6750ad984aeb263d93fc1c1db3dd8d03761a763958fb8d000ec19b0ea98b791e3d15020cd152375dbc5d5e19338cc5944f
|
|
7
|
+
data.tar.gz: 71008ecd40e79df705f769d03e0b03953a3278b3cb46ceb325b212e48b8c1321c58aa540750b094d8a94cc9082cbd66e13bba145ebfc7c0af6f78ce7a72fa796
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,168 +1,215 @@
|
|
|
1
|
-
Mongoid Search
|
|
2
|
-
============
|
|
1
|
+
# Mongoid Search
|
|
3
2
|
|
|
4
|
-
Mongoid Search is a simple full text search implementation for Mongoid ORM. It performs well for small data sets. If your searchable model is big (i.e. 1.000.000+ records),
|
|
3
|
+
Mongoid Search is a simple full text search implementation for Mongoid ORM. It supports Mongoid 3, 4, 5 and 6 and performs well for small data sets. If your searchable model is big (i.e. 1.000.000+ records), [mongoid_fulltext](https://github.com/mongoid/mongoid_fulltext), ElasticSearch, Solr or Sphinx may suit you better.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
[](https://travis-ci.org/mongoid/mongoid_search)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
9
|
In your Gemfile:
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'mongoid_search'
|
|
13
|
+
```
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
Then:
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
```
|
|
18
|
+
bundle install
|
|
19
|
+
```
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
## Examples
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
```ruby
|
|
24
|
+
class Product
|
|
25
|
+
include Mongoid::Document
|
|
26
|
+
include Mongoid::Search
|
|
27
|
+
field :brand
|
|
28
|
+
field :name
|
|
29
|
+
field :unit
|
|
30
|
+
field :info, type: Hash
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
has_many :tags
|
|
33
|
+
belongs_to :category
|
|
23
34
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
field :brand
|
|
28
|
-
field :name
|
|
35
|
+
search_in :brand, :name, tags: :name, category: :name, info: %i[summary description]
|
|
36
|
+
search_in :unit, index: :_unit_keywords
|
|
37
|
+
end
|
|
29
38
|
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
class Tag
|
|
40
|
+
include Mongoid::Document
|
|
41
|
+
field :name
|
|
32
42
|
|
|
33
|
-
|
|
34
|
-
|
|
43
|
+
belongs_to :product
|
|
44
|
+
end
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
class Category
|
|
47
|
+
include Mongoid::Document
|
|
48
|
+
field :name
|
|
39
49
|
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
has_many :products
|
|
51
|
+
end
|
|
52
|
+
```
|
|
42
53
|
|
|
43
|
-
|
|
44
|
-
include Mongoid::Document
|
|
45
|
-
field :name
|
|
54
|
+
Now when you save a product, you get a `_keywords` field automatically:
|
|
46
55
|
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
```ruby
|
|
57
|
+
p = Product.new brand: 'Apple', name: 'iPhone', unit: 'kilogram', info: { summary: 'Info-summary', description: 'Info-description' }
|
|
58
|
+
p.tags << Tag.new(name: 'Amazing')
|
|
59
|
+
p.tags << Tag.new(name: 'Awesome')
|
|
60
|
+
p.tags << Tag.new(name: 'Superb')
|
|
61
|
+
p.save
|
|
62
|
+
# => true
|
|
63
|
+
p._keywords
|
|
64
|
+
# => ["amazing", "apple", "awesome", "iphone", "superb", "Info-summary", "Info-description"]
|
|
65
|
+
p._unit_keywords
|
|
66
|
+
# => ["kilogram"]
|
|
67
|
+
```
|
|
49
68
|
|
|
50
|
-
Now
|
|
69
|
+
Now you can run search, which will look in the `_keywords` field and return all matching results:
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
p.save
|
|
57
|
-
=> true
|
|
58
|
-
p._keywords
|
|
59
|
-
=> ["amazing", "apple", "awesome", "iphone", "superb"]
|
|
71
|
+
```ruby
|
|
72
|
+
Product.full_text_search("apple iphone").size
|
|
73
|
+
# => 1
|
|
74
|
+
```
|
|
60
75
|
|
|
61
|
-
|
|
76
|
+
Of course, some models could have more than one index. For instance, two different searches with different fields, so you could even specify from which index should be searched:
|
|
62
77
|
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
```ruby
|
|
79
|
+
Product.full_text_search("kilogram", index: :_unit_keywords).size
|
|
80
|
+
# => 1
|
|
81
|
+
```
|
|
65
82
|
|
|
66
83
|
Note that the search is case insensitive, and accept partial searching too:
|
|
67
84
|
|
|
68
|
-
|
|
69
|
-
|
|
85
|
+
```ruby
|
|
86
|
+
Product.full_text_search('ipho').size
|
|
87
|
+
# => 1
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Assuming you have a category with multiple products you can use the following code to search for 'iphone' in products cheaper than $499.
|
|
70
91
|
|
|
71
|
-
|
|
72
|
-
|
|
92
|
+
```ruby
|
|
93
|
+
category.products.where(:price.lt => 499).full_text_search('iphone').asc(:price)
|
|
94
|
+
```
|
|
73
95
|
|
|
74
|
-
|
|
96
|
+
To index or reindex all existing records, run this rake task
|
|
75
97
|
|
|
98
|
+
```
|
|
99
|
+
$ rake mongoid_search:index
|
|
100
|
+
```
|
|
76
101
|
|
|
77
|
-
Options
|
|
78
|
-
-------
|
|
102
|
+
## Options
|
|
79
103
|
|
|
80
|
-
match
|
|
104
|
+
### match
|
|
81
105
|
|
|
82
|
-
|
|
106
|
+
* `:any` - match any occurrence
|
|
107
|
+
* `:all` - match all occurrences
|
|
83
108
|
|
|
84
|
-
|
|
109
|
+
Default is `:any`.
|
|
85
110
|
|
|
86
|
-
|
|
111
|
+
```ruby
|
|
112
|
+
Product.full_text_search('apple motorola', match: :any).size
|
|
113
|
+
# => 1
|
|
87
114
|
|
|
88
|
-
|
|
89
|
-
|
|
115
|
+
Product.full_text_search('apple motorola', match: :all).size
|
|
116
|
+
# => 0
|
|
117
|
+
```
|
|
90
118
|
|
|
91
|
-
|
|
92
|
-
=> 0
|
|
119
|
+
### allow\_empty\_search
|
|
93
120
|
|
|
94
|
-
|
|
121
|
+
* `true` - will return `Model.all`
|
|
122
|
+
* `false` - will return `[]`
|
|
95
123
|
|
|
96
|
-
|
|
124
|
+
Default is `false`.
|
|
97
125
|
|
|
98
|
-
|
|
126
|
+
```ruby
|
|
127
|
+
Product.full_text_search('', allow_empty_search: true).size
|
|
128
|
+
# => 1
|
|
129
|
+
```
|
|
99
130
|
|
|
100
|
-
|
|
131
|
+
### relevant_search
|
|
101
132
|
|
|
102
|
-
|
|
103
|
-
|
|
133
|
+
* `true` - adds relevance information to the results
|
|
134
|
+
* `false` - no relevance information
|
|
104
135
|
|
|
105
|
-
|
|
136
|
+
Default is `false`.
|
|
106
137
|
|
|
107
|
-
|
|
138
|
+
```ruby
|
|
139
|
+
Product.full_text_search('amazing apple', relevant_search: true)
|
|
140
|
+
# => [#<Product _id: 5016e7d16af54efe1c000001, _type: nil, brand: "Apple", name: "iPhone", attrs: nil, info: nil, category_id: nil, _keywords: ["amazing", "apple", "awesome", "iphone", "superb"], relevance: 2.0>]
|
|
141
|
+
```
|
|
108
142
|
|
|
109
|
-
|
|
143
|
+
Please note that relevant_search will return an Array and not a Criteria object. The search method should always be called in the end of the method chain.
|
|
110
144
|
|
|
111
|
-
|
|
145
|
+
### index
|
|
112
146
|
|
|
113
|
-
|
|
114
|
-
=> [#<Product _id: 5016e7d16af54efe1c000001, _type: nil, brand: "Apple", name: "iPhone", attrs: nil, info: nil, category_id: nil, _keywords: ["amazing", "apple", "awesome", "iphone", "superb"], relevance: 2.0>]
|
|
147
|
+
Default is `_keywords`.
|
|
115
148
|
|
|
116
|
-
|
|
149
|
+
```ruby
|
|
150
|
+
Product.full_text_search('amazing apple', index: :_keywords)
|
|
151
|
+
# => [#<Product _id: 5016e7d16af54efe1c000001, _type: nil, brand: "Apple", name: "iPhone", unit: "l", attrs: nil, info: nil, category_id: nil, _keywords: ["amazing", "apple", "awesome", "iphone", "superb"], _unit_keywords: ["l"], relevance: 2.0>]
|
|
117
152
|
|
|
118
|
-
|
|
119
|
-
|
|
153
|
+
Product.full_text_search('kg', index: :_unit_keywords)
|
|
154
|
+
# => [#<Product _id: 5016e7d16af54efe1c000001, _type: nil, brand: "Apple", name: "iPhone", unit: "kg", attrs: nil, info: nil, category_id: nil, _keywords: ["amazing", "apple", "awesome", "iphone", "superb"], _unit_keywords: ["kg"], relevance: 2.0>]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
index enables to have two or more different searches, with different or same fields. It should be noted that indexes are exclusive per each one.
|
|
158
|
+
|
|
159
|
+
## Initializer
|
|
120
160
|
|
|
121
161
|
Alternatively, you can create an initializer to setup those options:
|
|
122
162
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
163
|
+
```ruby
|
|
164
|
+
Mongoid::Search.setup do |config|
|
|
165
|
+
## Default matching type. Match :any or :all searched keywords
|
|
166
|
+
config.match = :any
|
|
167
|
+
|
|
168
|
+
## If true, an empty search will return all objects
|
|
169
|
+
config.allow_empty_search = false
|
|
126
170
|
|
|
127
|
-
|
|
128
|
-
|
|
171
|
+
## If true, will search with relevance information
|
|
172
|
+
config.relevant_search = false
|
|
129
173
|
|
|
130
|
-
|
|
131
|
-
|
|
174
|
+
## Stem keywords
|
|
175
|
+
config.stem_keywords = false
|
|
132
176
|
|
|
133
|
-
|
|
134
|
-
|
|
177
|
+
## Add a custom proc returning strings to replace the default stemmer
|
|
178
|
+
# For example using ruby-stemmer:
|
|
179
|
+
# config.stem_proc = Proc.new { |word| Lingua.stemmer(word, :language => 'nl') }
|
|
135
180
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
# config.stem_proc = Proc.new { |word| Lingua.stemmer(word, :language => 'nl') }
|
|
181
|
+
## Words to ignore
|
|
182
|
+
config.ignore_list = []
|
|
139
183
|
|
|
140
|
-
|
|
141
|
-
|
|
184
|
+
## An array of words
|
|
185
|
+
# config.ignore_list = %w{ a an to from as }
|
|
142
186
|
|
|
143
|
-
|
|
144
|
-
|
|
187
|
+
## Or from a file
|
|
188
|
+
# config.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]
|
|
145
189
|
|
|
146
|
-
|
|
147
|
-
|
|
190
|
+
## Search using regex (slower)
|
|
191
|
+
config.regex_search = true
|
|
148
192
|
|
|
149
|
-
|
|
150
|
-
config.regex_search = true
|
|
193
|
+
## Regex to search
|
|
151
194
|
|
|
152
|
-
|
|
195
|
+
## Match partial words on both sides (slower)
|
|
196
|
+
config.regex = Proc.new { |query| /#{query}/ }
|
|
153
197
|
|
|
154
|
-
|
|
155
|
-
|
|
198
|
+
## Match partial words on the beginning or in the end (slightly faster)
|
|
199
|
+
# config.regex = Proc.new { |query| /^#{query}/ }
|
|
200
|
+
# config.regex = Proc.new { |query| /#{query}$/ }
|
|
156
201
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
202
|
+
# Ligatures to be replaced
|
|
203
|
+
# http://en.wikipedia.org/wiki/Typographic_ligature
|
|
204
|
+
config.ligatures = { "œ"=>"oe", "æ"=>"ae" }
|
|
160
205
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
config.ligatures = { "œ"=>"oe", "æ"=>"ae" }
|
|
206
|
+
# Strip symbols regex to be replaced. These symbols will be replaced by space
|
|
207
|
+
config.strip_symbols = /[._:;'\"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/
|
|
164
208
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
end
|
|
209
|
+
# Strip accents regex to be replaced. These sybols will be removed after strip_symbols replacing
|
|
210
|
+
config.strip_accents = /[^\s\p{Alnum}]/
|
|
168
211
|
|
|
212
|
+
# Minimum word size. Words smaller than it won't be indexed
|
|
213
|
+
config.minimum_word_size = 2
|
|
214
|
+
end
|
|
215
|
+
```
|
data/Rakefile
CHANGED
|
@@ -3,8 +3,11 @@ require 'rake'
|
|
|
3
3
|
|
|
4
4
|
require 'rspec/core/rake_task'
|
|
5
5
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
6
|
-
spec.rspec_opts = [
|
|
6
|
+
spec.rspec_opts = ['-c', '-f progress']
|
|
7
7
|
spec.pattern = 'spec/**/*_spec.rb'
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
require 'rubocop/rake_task'
|
|
11
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
12
|
+
|
|
13
|
+
task default: %i[rubocop spec]
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.3.6
|
data/lib/mongoid_search/log.rb
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
module Mongoid::Search
|
|
2
|
-
|
|
3
|
-
base.send(:cattr_accessor, :search_fields)
|
|
4
|
-
|
|
5
|
-
base.extend ClassMethods
|
|
2
|
+
extend ActiveSupport::Concern
|
|
6
3
|
|
|
4
|
+
included do
|
|
5
|
+
cattr_accessor :search_fields
|
|
7
6
|
@@classes ||= []
|
|
8
|
-
@@classes <<
|
|
7
|
+
@@classes << self
|
|
9
8
|
end
|
|
10
9
|
|
|
11
10
|
def self.classes
|
|
@@ -16,17 +15,19 @@ module Mongoid::Search
|
|
|
16
15
|
# Set a field or a number of fields as sources for search
|
|
17
16
|
def search_in(*args)
|
|
18
17
|
args, options = args_and_options(args)
|
|
19
|
-
|
|
18
|
+
set_search_fields(options[:index], args)
|
|
20
19
|
|
|
21
|
-
field :
|
|
20
|
+
field options[:index], type: Array
|
|
22
21
|
|
|
23
|
-
index({ :
|
|
22
|
+
index({ options[:index] => 1 }, background: true)
|
|
24
23
|
|
|
25
24
|
before_save :set_keywords
|
|
26
25
|
end
|
|
27
26
|
|
|
28
|
-
def full_text_search(query, options={})
|
|
27
|
+
def full_text_search(query, options = {})
|
|
29
28
|
options = extract_options(options)
|
|
29
|
+
attr_accessor :relevance if options[:relevant_search].eql? true
|
|
30
|
+
|
|
30
31
|
return (options[:allow_empty_search] ? criteria.all : []) if query.blank?
|
|
31
32
|
|
|
32
33
|
if options[:relevant_search]
|
|
@@ -43,33 +44,46 @@ module Mongoid::Search
|
|
|
43
44
|
# Goes through all documents in the class that includes Mongoid::Search
|
|
44
45
|
# and indexes the keywords.
|
|
45
46
|
def index_keywords!
|
|
46
|
-
all.each { |d| d.index_keywords! ? Log.green(
|
|
47
|
+
all.each { |d| d.index_keywords! ? Log.green('.') : Log.red('F') }
|
|
47
48
|
end
|
|
48
49
|
|
|
49
50
|
private
|
|
51
|
+
|
|
52
|
+
def set_search_fields(index, fields)
|
|
53
|
+
self.search_fields ||= {}
|
|
54
|
+
|
|
55
|
+
(self.search_fields[index] ||= []).concat fields
|
|
56
|
+
end
|
|
57
|
+
|
|
50
58
|
def query(keywords, options)
|
|
51
59
|
keywords_hash = keywords.map do |kw|
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
if Mongoid::Search.regex_search
|
|
61
|
+
escaped_kw = Regexp.escape(kw)
|
|
62
|
+
kw = Mongoid::Search.regex.call(escaped_kw)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
{ options[:index] => kw }
|
|
54
66
|
end
|
|
55
67
|
|
|
56
|
-
criteria.send("#{(options[:match])
|
|
68
|
+
criteria.send("#{(options[:match])}_of", *keywords_hash)
|
|
57
69
|
end
|
|
58
70
|
|
|
59
71
|
def args_and_options(args)
|
|
60
72
|
options = args.last.is_a?(Hash) &&
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
%i[match
|
|
74
|
+
allow_empty_search
|
|
75
|
+
index
|
|
76
|
+
relevant_search].include?(args.last.keys.first) ? args.pop : {}
|
|
64
77
|
|
|
65
78
|
[args, extract_options(options)]
|
|
66
79
|
end
|
|
67
80
|
|
|
68
81
|
def extract_options(options)
|
|
69
82
|
{
|
|
70
|
-
:
|
|
71
|
-
:
|
|
72
|
-
:
|
|
83
|
+
match: options[:match] || Mongoid::Search.match,
|
|
84
|
+
allow_empty_search: options[:allow_empty_search] || Mongoid::Search.allow_empty_search,
|
|
85
|
+
relevant_search: options[:relevant_search] || Mongoid::Search.relevant_search,
|
|
86
|
+
index: options[:index] || :_keywords
|
|
73
87
|
}
|
|
74
88
|
end
|
|
75
89
|
|
|
@@ -79,52 +93,57 @@ module Mongoid::Search
|
|
|
79
93
|
|
|
80
94
|
def search_relevant(query, options)
|
|
81
95
|
results_with_relevance(query, options).sort { |o| o['value'] }.map do |r|
|
|
82
|
-
|
|
83
|
-
new(r['_id'].merge(:relevance => r['value'])) do |o|
|
|
96
|
+
new(r['_id'].merge(relevance: r['value'])) do |o|
|
|
84
97
|
# Need to match the actual object
|
|
85
98
|
o.instance_variable_set('@new_record', false)
|
|
86
99
|
o._id = r['_id']['_id']
|
|
87
100
|
end
|
|
88
|
-
|
|
89
101
|
end
|
|
90
102
|
end
|
|
91
103
|
|
|
92
104
|
def results_with_relevance(query, options)
|
|
93
105
|
keywords = Mongoid::Search::Util.normalize_keywords(query)
|
|
94
106
|
|
|
95
|
-
map = %
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
map = %{
|
|
108
|
+
function() {
|
|
109
|
+
var entries = 0;
|
|
110
|
+
for(i in keywords) {
|
|
111
|
+
for(j in this.#{options[:index]}) {
|
|
112
|
+
if(this.#{options[:index]}[j] == keywords[i]) {
|
|
113
|
+
entries++;
|
|
114
|
+
}
|
|
102
115
|
}
|
|
103
116
|
}
|
|
117
|
+
if(entries > 0) {
|
|
118
|
+
emit(this, entries);
|
|
119
|
+
}
|
|
104
120
|
}
|
|
105
|
-
if(entries > 0) {
|
|
106
|
-
emit(this, entries);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
121
|
}
|
|
110
122
|
|
|
111
|
-
reduce = %
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
123
|
+
reduce = %{
|
|
124
|
+
function(key, values) {
|
|
125
|
+
return(values);
|
|
126
|
+
}
|
|
115
127
|
}
|
|
116
128
|
|
|
117
|
-
query(keywords, options).map_reduce(map, reduce).scope(:
|
|
129
|
+
query(keywords, options).map_reduce(map, reduce).scope(keywords: keywords).out(inline: 1)
|
|
118
130
|
end
|
|
119
131
|
end
|
|
120
132
|
|
|
121
133
|
def index_keywords!
|
|
122
|
-
|
|
134
|
+
search_fields.map do |index, fields|
|
|
135
|
+
update_attribute(index, get_keywords(fields))
|
|
136
|
+
end
|
|
123
137
|
end
|
|
124
138
|
|
|
125
|
-
private
|
|
126
139
|
def set_keywords
|
|
127
|
-
|
|
128
|
-
|
|
140
|
+
search_fields.each do |index, fields|
|
|
141
|
+
send("#{index}=", get_keywords(fields))
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def get_keywords(fields)
|
|
146
|
+
Mongoid::Search::Util.keywords(self, fields)
|
|
147
|
+
.flatten.reject { |k| k.nil? || k.empty? }.uniq.sort
|
|
129
148
|
end
|
|
130
149
|
end
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
require 'mongoid_search'
|
|
2
|
-
require 'rails'
|
|
3
|
-
|
|
4
1
|
module Mongoid::Search
|
|
5
2
|
class Railtie < Rails::Railtie
|
|
6
3
|
rake_tasks do
|
|
7
|
-
Dir[File.join(File.dirname(__FILE__), '
|
|
4
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/*.rake')].each { |f| load f }
|
|
8
5
|
end
|
|
9
6
|
end
|
|
10
|
-
end
|
|
7
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
namespace :mongoid_search do
|
|
2
|
+
desc 'Goes through all documents with search enabled and indexes the keywords.'
|
|
3
|
+
task index: :environment do
|
|
4
|
+
::Rails.application.eager_load!
|
|
5
|
+
if Mongoid::Search.classes.blank?
|
|
6
|
+
Mongoid::Search::Log.log "No model to index keywords.\n"
|
|
7
|
+
else
|
|
8
|
+
Mongoid::Search.classes.each do |klass|
|
|
9
|
+
Mongoid::Search::Log.silent = ENV['SILENT']
|
|
10
|
+
Mongoid::Search::Log.log "\nIndexing documents for #{klass.name}:\n"
|
|
11
|
+
klass.index_keywords!
|
|
12
|
+
end
|
|
13
|
+
Mongoid::Search::Log.log "\n\nDone.\n"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/mongoid_search/util.rb
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
module Mongoid::Search::Util
|
|
3
3
|
def self.keywords(klass, fields)
|
|
4
4
|
if fields.is_a?(Array)
|
|
5
5
|
fields.map do |field|
|
|
6
|
-
|
|
6
|
+
keywords(klass, field)
|
|
7
7
|
end
|
|
8
8
|
elsif fields.is_a?(Hash)
|
|
9
9
|
fields.keys.map do |field|
|
|
10
10
|
attribute = klass.send(field)
|
|
11
11
|
unless attribute.blank?
|
|
12
12
|
if attribute.is_a?(Array)
|
|
13
|
-
attribute.map{ |a|
|
|
13
|
+
attribute.map { |a| keywords(a, fields[field]) }
|
|
14
14
|
else
|
|
15
|
-
|
|
15
|
+
keywords(attribute, fields[field])
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
else
|
|
20
|
-
value = if klass.respond_to?(fields.to_s +
|
|
21
|
-
klass.send(fields.to_s +
|
|
20
|
+
value = if klass.respond_to?(fields.to_s + '_translations')
|
|
21
|
+
klass.send(fields.to_s + '_translations').values
|
|
22
22
|
elsif klass.respond_to?(fields)
|
|
23
23
|
klass.send(fields)
|
|
24
24
|
else
|
|
25
|
-
value = klass[fields]
|
|
25
|
+
value = klass[fields]
|
|
26
26
|
end
|
|
27
27
|
value = value.join(' ') if value.respond_to?(:join)
|
|
28
28
|
normalize_keywords(value) if value
|
|
@@ -34,21 +34,22 @@ module Mongoid::Search::Util
|
|
|
34
34
|
ignore_list = Mongoid::Search.ignore_list
|
|
35
35
|
stem_keywords = Mongoid::Search.stem_keywords
|
|
36
36
|
stem_proc = Mongoid::Search.stem_proc
|
|
37
|
+
strip_symbols = Mongoid::Search.strip_symbols
|
|
38
|
+
strip_accents = Mongoid::Search.strip_accents
|
|
37
39
|
|
|
38
40
|
return [] if text.blank?
|
|
39
|
-
text = text.to_s
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
text = text.to_s
|
|
42
|
+
.mb_chars
|
|
43
|
+
.normalize(:kd)
|
|
44
|
+
.downcase
|
|
45
|
+
.to_s
|
|
46
|
+
.gsub(strip_symbols, ' ') # strip symbols
|
|
47
|
+
.gsub(strip_accents, '') # strip accents
|
|
48
|
+
.gsub(/[#{ligatures.keys.join("")}]/) { |c| ligatures[c] }
|
|
49
|
+
.split(' ')
|
|
50
|
+
.reject { |word| word.size < Mongoid::Search.minimum_word_size }
|
|
49
51
|
text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
|
|
50
52
|
text = text.map(&stem_proc) if stem_keywords
|
|
51
53
|
text
|
|
52
54
|
end
|
|
53
|
-
|
|
54
55
|
end
|