Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

merge

  • Loading branch information...
commit ef07c1e16a81b576f7b7f1c0b92fb6995f102cbe 2 parents d92fe10 + df6a71d
authored February 07, 2012
1  lib/elastic_searchable/active_record_extensions.rb
@@ -45,6 +45,7 @@ class << self
45 45
         backgrounded :delete_id_from_index => ElasticSearchable::Callbacks.backgrounded_options
46 46
       end
47 47
 
  48
+      attr_reader :hit # the hit json for this result
48 49
       attr_accessor :index_lifecycle, :percolations
49 50
       define_model_callbacks :index, :percolate, :only => :after
50 51
       after_commit :update_index_on_create_backgrounded, :if => :should_index?, :on => :create
4  lib/elastic_searchable/queries.rb
@@ -40,6 +40,10 @@ def search(query, options = {})
40 40
       ids = hits['hits'].collect {|h| h['_id'].to_i }
41 41
       results = self.find(ids).sort_by {|result| ids.index(result.id) }
42 42
 
  43
+      results.each do |result|
  44
+        result.instance_variable_set '@hit', hits['hits'][ids.index(result.id)]
  45
+      end
  46
+
43 47
       ElasticSearchable::Paginator.handler.new(results, page, options[:size], hits['total'])
44 48
     end
45 49
 
2  lib/elastic_searchable/version.rb
... ...
@@ -1,3 +1,3 @@
1 1
 module ElasticSearchable
2  
-  VERSION = '2.0.0.pre1'
  2
+  VERSION = '2.0.0'
3 3
 end
14  test/test_elastic_searchable.rb
@@ -180,7 +180,7 @@ def indexed_on_update?
180 180
       ElasticSearchable.refresh_index
181 181
     end
182 182
 
183  
-    context 'searching for results' do
  183
+    context 'searching on a term that returns one result' do
184 184
       setup do
185 185
         @results = Post.search 'first'
186 186
       end
@@ -193,6 +193,18 @@ def indexed_on_update?
193 193
         assert_nil @results.previous_page
194 194
         assert_nil @results.next_page
195 195
       end
  196
+      should 'have populated hit' do
  197
+        assert_equal @results.first.hit['_id'], @first_post.id.to_s
  198
+      end
  199
+    end
  200
+    context 'searching on a term that returns multiple results' do
  201
+      setup do
  202
+        @results = Post.search 'foo'
  203
+      end
  204
+      should 'have populated hit on each record with the correct hit json' do
  205
+        assert_equal @results.first.hit['_id'], @first_post.id.to_s
  206
+        assert_equal @results.last.hit['_id'], @second_post.id.to_s
  207
+      end
196 208
     end
197 209
 
198 210
     context 'searching for results using a query Hash' do

0 notes on commit ef07c1e

Please sign in to comment.
Something went wrong with that request. Please try again.