elastic_searchable 2.0.0 → 2.0.1
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.
|
@@ -11,7 +11,7 @@ module ElasticSearchable
|
|
|
11
11
|
# http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/put_mapping/
|
|
12
12
|
def create_mapping
|
|
13
13
|
return unless self.elastic_options[:mapping]
|
|
14
|
-
ElasticSearchable.request :put, index_mapping_path('_mapping'), :json_body => {index_type => mapping}
|
|
14
|
+
ElasticSearchable.request :put, index_mapping_path('_mapping'), :json_body => {index_type => self.elastic_options[:mapping]}
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# delete one record from the index
|
|
@@ -17,15 +17,13 @@ module ElasticSearchable
|
|
|
17
17
|
options[:fields] ||= '_id'
|
|
18
18
|
options[:size] ||= per_page_for_search(options)
|
|
19
19
|
options[:from] ||= options[:size] * (page - 1)
|
|
20
|
-
if query.is_a?(Hash)
|
|
21
|
-
|
|
20
|
+
options[:query] ||= if query.is_a?(Hash)
|
|
21
|
+
query
|
|
22
22
|
else
|
|
23
|
-
|
|
24
|
-
:query_string =>
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
23
|
+
{}.tap do |q|
|
|
24
|
+
q[:query_string] = { :query => query }
|
|
25
|
+
q[:query_string][:default_operator] = options.delete(:default_operator) if options.has_key?(:default_operator)
|
|
26
|
+
end
|
|
29
27
|
end
|
|
30
28
|
query = {}
|
|
31
29
|
case sort = options.delete(:sort)
|
data/lib/elastic_searchable.rb
CHANGED
|
@@ -6,21 +6,23 @@ require 'elastic_searchable/active_record_extensions'
|
|
|
6
6
|
module ElasticSearchable
|
|
7
7
|
include HTTParty
|
|
8
8
|
format :json
|
|
9
|
-
base_uri 'localhost:9200'
|
|
9
|
+
base_uri ENV['ELASTICSEARCH_URL'] || 'localhost:9200'
|
|
10
10
|
|
|
11
11
|
class ElasticError < StandardError; end
|
|
12
12
|
class << self
|
|
13
|
-
attr_accessor :logger, :index_name, :index_settings
|
|
13
|
+
attr_accessor :logger, :index_name, :index_settings
|
|
14
|
+
@@offline = false
|
|
14
15
|
|
|
15
16
|
# execute a block of work without reindexing objects
|
|
16
17
|
def offline(&block)
|
|
17
|
-
|
|
18
|
+
original_value = offline?
|
|
19
|
+
@@offline = true
|
|
18
20
|
yield
|
|
19
21
|
ensure
|
|
20
|
-
|
|
22
|
+
@@offline = original_value
|
|
21
23
|
end
|
|
22
24
|
def offline?
|
|
23
|
-
|
|
25
|
+
!!@@offline
|
|
24
26
|
end
|
|
25
27
|
# encapsulate encoding hash into json string
|
|
26
28
|
# support Yajl encoder if installed
|
|
@@ -4,7 +4,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
4
4
|
def setup
|
|
5
5
|
delete_index
|
|
6
6
|
ElasticSearchable.index_settings = {'number_of_replicas' => 0, 'number_of_shards' => 1}
|
|
7
|
-
|
|
7
|
+
ElasticSearchable.debug_output
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
context 'non elastic activerecord class' do
|
|
@@ -29,7 +29,8 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
class Post < ActiveRecord::Base
|
|
32
|
-
elastic_searchable
|
|
32
|
+
elastic_searchable \
|
|
33
|
+
:mapping => {:properties => { :title => {:type => "string", :index => "not_analyzed"} } }
|
|
33
34
|
after_index :indexed
|
|
34
35
|
after_index :indexed_on_create, :on => :create
|
|
35
36
|
after_index :indexed_on_update, :on => :update
|
|
@@ -89,16 +90,17 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
89
90
|
|
|
90
91
|
context 'Model.create' do
|
|
91
92
|
setup do
|
|
93
|
+
ElasticSearchable.create_index
|
|
92
94
|
@post = Post.create :title => 'foo', :body => "bar"
|
|
93
95
|
end
|
|
94
96
|
should 'have fired after_index callback' do
|
|
95
|
-
assert @post.indexed?
|
|
97
|
+
assert @post.indexed?, 'indexed?'
|
|
96
98
|
end
|
|
97
99
|
should 'have fired after_index_on_create callback' do
|
|
98
|
-
assert @post.indexed_on_create?
|
|
100
|
+
assert @post.indexed_on_create?, 'indexed_on_create?'
|
|
99
101
|
end
|
|
100
102
|
should 'not have fired after_index_on_update callback' do
|
|
101
|
-
assert !@post.indexed_on_update?
|
|
103
|
+
assert !@post.indexed_on_update?, 'indexed_on_update?'
|
|
102
104
|
end
|
|
103
105
|
end
|
|
104
106
|
|
|
@@ -120,6 +122,25 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
120
122
|
end
|
|
121
123
|
end
|
|
122
124
|
|
|
125
|
+
context 'Model.create within two ElasticSearchable.offline blocks' do
|
|
126
|
+
setup do
|
|
127
|
+
ElasticSearchable.offline do
|
|
128
|
+
ElasticSearchable.offline do
|
|
129
|
+
@post = Post.create :title => 'foo', :body => "bar"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
should 'not have fired after_index callback' do
|
|
134
|
+
assert !@post.indexed?
|
|
135
|
+
end
|
|
136
|
+
should 'not have fired after_index_on_create callback' do
|
|
137
|
+
assert !@post.indexed_on_create?
|
|
138
|
+
end
|
|
139
|
+
should 'not have fired after_index_on_update callback' do
|
|
140
|
+
assert !@post.indexed_on_update?
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
123
144
|
context 'Model.create within ElasticSearchable.offline block' do
|
|
124
145
|
setup do
|
|
125
146
|
ElasticSearchable.offline do
|
|
@@ -141,17 +162,17 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
141
162
|
setup do
|
|
142
163
|
Post.delete_all
|
|
143
164
|
ElasticSearchable.create_index
|
|
144
|
-
@first_post = Post.create :title => '
|
|
145
|
-
@second_post = Post.create :title => 'foo', :body => "second
|
|
165
|
+
@first_post = Post.create :title => 'bar', :body => "first foo"
|
|
166
|
+
@second_post = Post.create :title => 'foo', :body => "second foo"
|
|
146
167
|
ElasticSearchable.delete_index
|
|
147
168
|
ElasticSearchable.create_index
|
|
148
169
|
end
|
|
149
|
-
should 'not raise error if error occurs reindexing model' do
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
end
|
|
170
|
+
# should 'not raise error if error occurs reindexing model' do
|
|
171
|
+
# ElasticSearchable.expects(:request).raises(ElasticSearchable::ElasticError.new('faux error'))
|
|
172
|
+
# assert_nothing_raised do
|
|
173
|
+
# Post.reindex
|
|
174
|
+
# end
|
|
175
|
+
# end
|
|
155
176
|
should 'not raise error if destroying one instance' do
|
|
156
177
|
Logger.any_instance.expects(:warn)
|
|
157
178
|
assert_nothing_raised do
|
|
@@ -175,8 +196,8 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
175
196
|
context 'with index containing multiple results' do
|
|
176
197
|
setup do
|
|
177
198
|
ElasticSearchable.create_index
|
|
178
|
-
@first_post = Post.create :title => '
|
|
179
|
-
@second_post = Post.create :title => 'foo', :body => "second
|
|
199
|
+
@first_post = Post.create :title => 'bar', :body => "first foo"
|
|
200
|
+
@second_post = Post.create :title => 'foo', :body => "second foo"
|
|
180
201
|
ElasticSearchable.refresh_index
|
|
181
202
|
end
|
|
182
203
|
|
|
@@ -199,7 +220,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
199
220
|
end
|
|
200
221
|
context 'searching on a term that returns multiple results' do
|
|
201
222
|
setup do
|
|
202
|
-
@results = Post.search 'foo'
|
|
223
|
+
@results = Post.search 'foo', :sort => 'title'
|
|
203
224
|
end
|
|
204
225
|
should 'have populated hit on each record with the correct hit json' do
|
|
205
226
|
assert_equal @results.first.hit['_id'], @first_post.id.to_s
|
|
@@ -231,7 +252,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
231
252
|
|
|
232
253
|
context 'when per_page is a string' do
|
|
233
254
|
setup do
|
|
234
|
-
@results = Post.search 'foo', :per_page => 1.to_s, :sort => '
|
|
255
|
+
@results = Post.search 'foo', :per_page => 1.to_s, :sort => 'title'
|
|
235
256
|
end
|
|
236
257
|
should 'find first object' do
|
|
237
258
|
assert_contains @results, @first_post
|
|
@@ -240,7 +261,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
240
261
|
|
|
241
262
|
context 'searching for second page using will_paginate params' do
|
|
242
263
|
setup do
|
|
243
|
-
@results = Post.search 'foo', :page => 2, :per_page => 1, :sort => '
|
|
264
|
+
@results = Post.search 'foo', :page => 2, :per_page => 1, :sort => 'title'
|
|
244
265
|
end
|
|
245
266
|
should 'not find objects from first page' do
|
|
246
267
|
assert_does_not_contain @results, @first_post
|
|
@@ -258,7 +279,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
258
279
|
|
|
259
280
|
context 'sorting search results' do
|
|
260
281
|
setup do
|
|
261
|
-
@results = Post.search 'foo', :sort => '
|
|
282
|
+
@results = Post.search 'foo', :sort => 'title:desc'
|
|
262
283
|
end
|
|
263
284
|
should 'sort results correctly' do
|
|
264
285
|
assert_equal @second_post, @results.first
|
|
@@ -268,7 +289,7 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
|
268
289
|
|
|
269
290
|
context 'advanced sort options' do
|
|
270
291
|
setup do
|
|
271
|
-
@results = Post.search 'foo', :sort => [{:
|
|
292
|
+
@results = Post.search 'foo', :sort => [{:title => 'desc'}]
|
|
272
293
|
end
|
|
273
294
|
should 'sort results correctly' do
|
|
274
295
|
assert_equal @second_post, @results.first
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: elastic_searchable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.
|
|
4
|
+
version: 2.0.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
12
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activerecord
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &2151868260 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,10 +21,10 @@ dependencies:
|
|
|
21
21
|
version: 3.0.5
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *2151868260
|
|
25
25
|
- !ruby/object:Gem::Dependency
|
|
26
26
|
name: httparty
|
|
27
|
-
requirement: &
|
|
27
|
+
requirement: &2151867400 !ruby/object:Gem::Requirement
|
|
28
28
|
none: false
|
|
29
29
|
requirements:
|
|
30
30
|
- - ! '>='
|
|
@@ -32,10 +32,10 @@ dependencies:
|
|
|
32
32
|
version: 0.6.0
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
|
-
version_requirements: *
|
|
35
|
+
version_requirements: *2151867400
|
|
36
36
|
- !ruby/object:Gem::Dependency
|
|
37
37
|
name: backgrounded
|
|
38
|
-
requirement: &
|
|
38
|
+
requirement: &2151866680 !ruby/object:Gem::Requirement
|
|
39
39
|
none: false
|
|
40
40
|
requirements:
|
|
41
41
|
- - ! '>='
|
|
@@ -43,10 +43,10 @@ dependencies:
|
|
|
43
43
|
version: 0.7.0
|
|
44
44
|
type: :runtime
|
|
45
45
|
prerelease: false
|
|
46
|
-
version_requirements: *
|
|
46
|
+
version_requirements: *2151866680
|
|
47
47
|
- !ruby/object:Gem::Dependency
|
|
48
48
|
name: multi_json
|
|
49
|
-
requirement: &
|
|
49
|
+
requirement: &2151866000 !ruby/object:Gem::Requirement
|
|
50
50
|
none: false
|
|
51
51
|
requirements:
|
|
52
52
|
- - ! '>='
|
|
@@ -54,10 +54,10 @@ dependencies:
|
|
|
54
54
|
version: 1.0.0
|
|
55
55
|
type: :runtime
|
|
56
56
|
prerelease: false
|
|
57
|
-
version_requirements: *
|
|
57
|
+
version_requirements: *2151866000
|
|
58
58
|
- !ruby/object:Gem::Dependency
|
|
59
59
|
name: rake
|
|
60
|
-
requirement: &
|
|
60
|
+
requirement: &2151865340 !ruby/object:Gem::Requirement
|
|
61
61
|
none: false
|
|
62
62
|
requirements:
|
|
63
63
|
- - =
|
|
@@ -65,10 +65,10 @@ dependencies:
|
|
|
65
65
|
version: 0.9.2.2
|
|
66
66
|
type: :development
|
|
67
67
|
prerelease: false
|
|
68
|
-
version_requirements: *
|
|
68
|
+
version_requirements: *2151865340
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: sqlite3
|
|
71
|
-
requirement: &
|
|
71
|
+
requirement: &2151864760 !ruby/object:Gem::Requirement
|
|
72
72
|
none: false
|
|
73
73
|
requirements:
|
|
74
74
|
- - =
|
|
@@ -76,10 +76,10 @@ dependencies:
|
|
|
76
76
|
version: 1.3.4
|
|
77
77
|
type: :development
|
|
78
78
|
prerelease: false
|
|
79
|
-
version_requirements: *
|
|
79
|
+
version_requirements: *2151864760
|
|
80
80
|
- !ruby/object:Gem::Dependency
|
|
81
81
|
name: pry
|
|
82
|
-
requirement: &
|
|
82
|
+
requirement: &2151864020 !ruby/object:Gem::Requirement
|
|
83
83
|
none: false
|
|
84
84
|
requirements:
|
|
85
85
|
- - =
|
|
@@ -87,10 +87,10 @@ dependencies:
|
|
|
87
87
|
version: 0.9.6.2
|
|
88
88
|
type: :development
|
|
89
89
|
prerelease: false
|
|
90
|
-
version_requirements: *
|
|
90
|
+
version_requirements: *2151864020
|
|
91
91
|
- !ruby/object:Gem::Dependency
|
|
92
92
|
name: shoulda
|
|
93
|
-
requirement: &
|
|
93
|
+
requirement: &2151863260 !ruby/object:Gem::Requirement
|
|
94
94
|
none: false
|
|
95
95
|
requirements:
|
|
96
96
|
- - =
|
|
@@ -98,10 +98,10 @@ dependencies:
|
|
|
98
98
|
version: 2.11.3
|
|
99
99
|
type: :development
|
|
100
100
|
prerelease: false
|
|
101
|
-
version_requirements: *
|
|
101
|
+
version_requirements: *2151863260
|
|
102
102
|
- !ruby/object:Gem::Dependency
|
|
103
103
|
name: mocha
|
|
104
|
-
requirement: &
|
|
104
|
+
requirement: &2151862580 !ruby/object:Gem::Requirement
|
|
105
105
|
none: false
|
|
106
106
|
requirements:
|
|
107
107
|
- - =
|
|
@@ -109,7 +109,7 @@ dependencies:
|
|
|
109
109
|
version: 0.10.0
|
|
110
110
|
type: :development
|
|
111
111
|
prerelease: false
|
|
112
|
-
version_requirements: *
|
|
112
|
+
version_requirements: *2151862580
|
|
113
113
|
description: integrate the elastic search engine with rails
|
|
114
114
|
email:
|
|
115
115
|
- ryan@codecrate.com
|
|
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
153
153
|
version: '0'
|
|
154
154
|
segments:
|
|
155
155
|
- 0
|
|
156
|
-
hash:
|
|
156
|
+
hash: 808032034448504633
|
|
157
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
none: false
|
|
159
159
|
requirements:
|
|
@@ -162,10 +162,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
162
162
|
version: '0'
|
|
163
163
|
segments:
|
|
164
164
|
- 0
|
|
165
|
-
hash:
|
|
165
|
+
hash: 808032034448504633
|
|
166
166
|
requirements: []
|
|
167
167
|
rubyforge_project: elastic_searchable
|
|
168
|
-
rubygems_version: 1.8.
|
|
168
|
+
rubygems_version: 1.8.15
|
|
169
169
|
signing_key:
|
|
170
170
|
specification_version: 3
|
|
171
171
|
summary: elastic search for activerecord
|