Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 4 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
10  lib/elastic_searchable.rb
@@ -10,17 +10,19 @@ module ElasticSearchable
10 10
 
11 11
   class ElasticError < StandardError; end
12 12
   class << self
13  
-    attr_accessor :logger, :index_name, :index_settings, :offline
  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  
-      @offline = true
  18
+      original_value = offline?
  19
+      @@offline = true
18 20
       yield
19 21
     ensure
20  
-      @offline = false
  22
+      @@offline = original_value
21 23
     end
22 24
     def offline?
23  
-      !!@offline
  25
+      !!@@offline
24 26
     end
25 27
     # encapsulate encoding hash into json string
26 28
     # support Yajl encoder if installed
2  lib/elastic_searchable/index.rb
@@ -11,7 +11,7 @@ def delete_mapping
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
14  lib/elastic_searchable/queries.rb
@@ -17,15 +17,13 @@ def search(query, options = {})
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  
-        options[:query] = query
  20
+      options[:query] ||= if query.is_a?(Hash)
  21
+        query
22 22
       else
23  
-        options[:query] = {
24  
-          :query_string => {
25  
-            :query => query,
26  
-            :default_operator => options.delete(:default_operator)
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)
2  lib/elastic_searchable/version.rb
... ...
@@ -1,3 +1,3 @@
1 1
 module ElasticSearchable
2  
-  VERSION = '2.0.0'
  2
+  VERSION = '2.0.1'
3 3
 end
61  test/test_elastic_searchable.rb
@@ -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  
-    # ElasticSearchable.debug_output
  7
+    ElasticSearchable.debug_output
8 8
   end
9 9
 
10 10
   context 'non elastic activerecord class' do
@@ -29,7 +29,8 @@ class Parent < ActiveRecord::Base
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 @@ def indexed_on_update?
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 @@ def indexed_on_update?
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 @@ def indexed_on_update?
141 162
     setup do
142 163
       Post.delete_all
143 164
       ElasticSearchable.create_index
144  
-      @first_post = Post.create :title => 'foo', :body => "first bar"
145  
-      @second_post = Post.create :title => 'foo', :body => "second bar"
  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  
-      ElasticSearchable.expects(:request).raises(ElasticSearchable::ElasticError.new('faux error'))
151  
-      assert_nothing_raised do
152  
-        Post.reindex
153  
-      end
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 @@ def indexed_on_update?
175 196
   context 'with index containing multiple results' do
176 197
     setup do
177 198
       ElasticSearchable.create_index
178  
-      @first_post = Post.create :title => 'foo', :body => "first bar"
179  
-      @second_post = Post.create :title => 'foo', :body => "second bar"
  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 @@ def indexed_on_update?
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 @@ def indexed_on_update?
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 => 'id'
  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 @@ def indexed_on_update?
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 => 'id'
  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 @@ def indexed_on_update?
258 279
 
259 280
     context 'sorting search results' do
260 281
       setup do
261  
-        @results = Post.search 'foo', :sort => 'id:desc'
  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 @@ def indexed_on_update?
268 289
 
269 290
     context 'advanced sort options' do
270 291
       setup do
271  
-        @results = Post.search 'foo', :sort => [{:id => 'desc'}]
  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

No commit comments for this range

Something went wrong with that request. Please try again.