elastic_searchable 0.5.0 → 0.6.0

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{elastic_searchable}
8
- s.version = "0.5.0"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Sonnek"]
12
- s.date = %q{2011-03-04}
12
+ s.date = %q{2011-03-08}
13
13
  s.description = %q{integrate the elastic search engine with rails}
14
14
  s.email = %q{ryan@codecrate.com}
15
15
  s.extra_rdoc_files = [
@@ -12,10 +12,10 @@ module ElasticSearchable
12
12
  self.class.delete_id_from_index_backgrounded self.id
13
13
  end
14
14
  def update_index_on_create
15
- index_in_elastic_search :create
15
+ reindex :create
16
16
  end
17
17
  def update_index_on_update
18
- index_in_elastic_search :update
18
+ reindex :update
19
19
  end
20
20
  end
21
21
  end
@@ -53,6 +53,35 @@ module ElasticSearchable
53
53
  ['', index_name, action].compact.join('/')
54
54
  end
55
55
 
56
+ # reindex all records using bulk api
57
+ # options:
58
+ # :scope - scope the find_in_batches to only a subset of records
59
+ # :batch - counter to start indexing at
60
+ # :include - passed to find_in_batches to hydrate objects
61
+ # see http://www.elasticsearch.org/guide/reference/api/bulk.html
62
+ def reindex(options = {})
63
+ batch = options.delete(:batch) || 1
64
+ options[:batch_size] ||= 1000
65
+ options[:start] ||= (batch - 1) * options[:batch_size]
66
+ scope = options.delete(:scope) || self
67
+ scope.find_in_batches(options) do |records|
68
+ puts "reindexing batch ##{batch}..."
69
+ batch += 1
70
+ actions = []
71
+ records.each do |record|
72
+ next unless record.should_index?
73
+ begin
74
+ doc = record.as_json_for_index.to_json
75
+ actions << {:index => {'_index' => index_name, '_type' => index_type, '_id' => record.id}}.to_json
76
+ actions << doc
77
+ rescue => e
78
+ puts "Unable to bulk index record: #{record.inspect} [#{e.message}]"
79
+ end
80
+ end
81
+ ElasticSearchable.request :put, '/_bulk', :body => "\n#{actions.join("\n")}\n"
82
+ end
83
+ end
84
+
56
85
  private
57
86
  def index_name
58
87
  self.elastic_options[:index] || ElasticSearchable.default_index
@@ -63,13 +92,13 @@ module ElasticSearchable
63
92
  end
64
93
 
65
94
  module InstanceMethods
66
- # index the object in elasticsearch
67
- # fire after_index callbacks after operation is complete
95
+ # reindex the object in elasticsearch
96
+ # fires after_index callbacks after operation is complete
68
97
  # see http://www.elasticsearch.org/guide/reference/api/index_.html
69
- def index_in_elastic_search(lifecycle = nil)
98
+ def reindex(lifecycle = nil)
70
99
  query = {}
71
100
  query.merge! :percolate => "*" if self.class.elastic_options[:percolate]
72
- response = ElasticSearchable.request :put, self.class.index_type_path(self.id), :query => query, :body => self.indexed_json_document.to_json
101
+ response = ElasticSearchable.request :put, self.class.index_type_path(self.id), :query => query, :body => self.as_json_for_index.to_json
73
102
 
74
103
  self.run_callbacks("after_index_on_#{lifecycle}".to_sym) if lifecycle
75
104
  self.run_callbacks(:after_index)
@@ -80,7 +109,7 @@ module ElasticSearchable
80
109
  end
81
110
  end
82
111
  # document to index in elasticsearch
83
- def indexed_json_document
112
+ def as_json_for_index
84
113
  self.as_json self.class.elastic_options[:json]
85
114
  end
86
115
  def should_index?
@@ -92,7 +121,7 @@ module ElasticSearchable
92
121
  # can be done automatically when indexing using :percolate => true config option
93
122
  # http://www.elasticsearch.org/blog/2011/02/08/percolator.html
94
123
  def percolate
95
- response = ElasticSearchable.request :get, self.class.index_type_path('_percolate'), :body => {:doc => self.indexed_json_document}.to_json
124
+ response = ElasticSearchable.request :get, self.class.index_type_path('_percolate'), :body => {:doc => self.as_json_for_index}.to_json
96
125
  response['matches']
97
126
  end
98
127
 
@@ -109,6 +109,25 @@ class TestElasticSearchable < Test::Unit::TestCase
109
109
  end
110
110
  end
111
111
 
112
+ context 'with empty index when multiple database records' do
113
+ setup do
114
+ Post.create_index
115
+ @first_post = Post.create :title => 'foo', :body => "first bar"
116
+ @second_post = Post.create :title => 'foo', :body => "second bar"
117
+ Post.clean_index
118
+ end
119
+ context 'Post.reindex' do
120
+ setup do
121
+ Post.reindex
122
+ Post.refresh_index
123
+ end
124
+ should 'have reindexed both records' do
125
+ ElasticSearchable.request :get, "/elastic_searchable/posts/#{@first_post.id}"
126
+ ElasticSearchable.request :get, "/elastic_searchable/posts/#{@second_post.id}"
127
+ end
128
+ end
129
+ end
130
+
112
131
  context 'with index containing multiple results' do
113
132
  setup do
114
133
  Post.create_index
@@ -182,7 +201,7 @@ class TestElasticSearchable < Test::Unit::TestCase
182
201
  context 'activerecord class with optional :if=>proc configuration' do
183
202
  context 'when creating new instance' do
184
203
  setup do
185
- Blog.any_instance.expects(:index_in_elastic_search).never
204
+ Blog.any_instance.expects(:reindex).never
186
205
  @blog = Blog.create! :title => 'foo'
187
206
  end
188
207
  should 'not index record' do end #see expectations
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_searchable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 0.5.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Sonnek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-04 00:00:00 -06:00
18
+ date: 2011-03-08 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency