mongoid-elasticsearch 0.7.0 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73ff0a4562b7d652802bc8bc1f26343cef63cefe
4
- data.tar.gz: 878f97484fb4b5a1b52773353d3d1df1a33ba529
3
+ metadata.gz: 5b0899d5817a5ab9f5bfaf6a20b2b243e736602e
4
+ data.tar.gz: 635577852d5d8211b44e7302b72c41aa041b292a
5
5
  SHA512:
6
- metadata.gz: 0a87c268fde4d52029ac6d667369bc555b868bff9d8efbc7621c6584b6de8558b7236bce519e095d5a07f16be94dd78cbae4a76fec7d39f5546d6aeb9cf20732
7
- data.tar.gz: 0325cd6d0b05bb8ddd236971f9b4b2685269c7300b47a07f715b783026665974355ceaf9137aaf84122c8f0f826f2ccd9400f23a9e2854e199110437ac89380b
6
+ metadata.gz: 20c7c9629a178118337e935f94e7431a39e5bcd192bc2547d12a134a4ef0b8e9c441a64327e4a07d98bb639f7e42572c46c9b28f0b474a4cff6f0bbb5016de45
7
+ data.tar.gz: 7eea04cc6d7137cd6b15abcb780e4533729862fa3d9c6b75dc52f643d0fdca47ed832a9bfa26c964692f68d180f9ad6e0b656df078a2a174e2f41071583599dd
@@ -0,0 +1,11 @@
1
+ ## 0.8.0 (March 4, 2014) ##
2
+
3
+ * fix results with :load wrapper #6 (by [@xronos-i-am](https://github.com/xronos-i-am))
4
+ * Added option to prevent automatic creating of index. #7 (thx [@intrica](https://github.com/intrica))
5
+ * use after_initalize to create indexes later in the app boot process
6
+
7
+ Set Mongoid::Elasticsearch.autocreate_indexes to false in an initalizer to prevent automatic creation for all indexes.
8
+
9
+ You can always use ```rake es:create``` to create all indexes or call Mongoid::Elasticsearch.create_all_indexes!.
10
+
11
+ Indexes defined with skip_create: true are not created with all other indexes and must be created manually with Model.es.index.create
data/README.md CHANGED
@@ -205,27 +205,23 @@ Example mapping with boost field:
205
205
 
206
206
  ### Reindexing
207
207
 
208
- #### Simple bulk
208
+ #### All models
209
+
210
+ ```rake es:reindex``` will reindex all indices managed by Mongoid::Elasticsearch
211
+
212
+ #### One model - Simple bulk
209
213
 
210
214
  This is the preferred (fastest) method to reindex everything
211
215
 
212
216
  Music::Video.es.index_all
213
217
 
214
- #### Simple
218
+ #### One model - Simple
215
219
 
216
220
  Communities::Thread.es.index.reset
217
221
  Communities::Thread.enabled.each do |ingr|
218
222
  ingr.es_update
219
223
  end
220
224
 
221
- #### Bulk with [progress bar](http://rubygems.org/gems/ruby-progressbar)
222
-
223
- pb = nil
224
- Music::Video.es.index_all do |steps, step|
225
- pb = ProgressBar.create(title: "videos", total: steps, format: '%t: %p%% %a |%b>%i| %E') if pb.nil?
226
- pb.increment
227
- end
228
-
229
225
  ### Possible wrappers for results:
230
226
 
231
227
  - :hash - raw hash from ES
@@ -235,6 +231,17 @@ This is the preferred (fastest) method to reindex everything
235
231
 
236
232
  See more examples in specs.
237
233
 
234
+ ### Index creation
235
+
236
+ This gem by default automatically creates indexes for all configured models on application startup.
237
+
238
+ Set ```Mongoid::Elasticsearch.autocreate_indexes = false``` in an initalizer to prevent automatic creation for all indexes.
239
+
240
+ You can always use ```rake es:create``` to create all indexes or call ```Mongoid::Elasticsearch.create_all_indexes!```.
241
+
242
+ Indexes defined with option ```skip_create: true``` are not created with all other indexes and must be created manually with ```Model.es.index.create```
243
+
244
+
238
245
  #### Util
239
246
 
240
247
  # Escape string so it can be safely passed to ES (removes all special characters)
@@ -14,6 +14,9 @@ require 'mongoid/elasticsearch/monkeypatches'
14
14
 
15
15
  module Mongoid
16
16
  module Elasticsearch
17
+ mattr_accessor :autocreate_indexes
18
+ self.autocreate_indexes = true
19
+
17
20
  mattr_accessor :prefix
18
21
  self.prefix = ''
19
22
 
@@ -51,23 +54,28 @@ module Mongoid
51
54
  index_options: {},
52
55
  index_mappings: nil,
53
56
  wrapper: :model,
54
- callbacks: true
57
+ callbacks: true,
58
+ skip_create: false
55
59
  }.merge(options)
56
60
 
57
61
  if options[:wrapper] == :model
58
62
  attr_accessor :_type, :_score, :_source
59
63
  end
60
64
 
61
- cattr_accessor :es_client_options, :es_index_name, :es_index_options, :es_wrapper
65
+ cattr_accessor :es_client_options, :es_index_name, :es_index_options, :es_wrapper, :es_skip_create
62
66
 
63
67
  self.es_client_options = Mongoid::Elasticsearch.client_options.dup.merge(options[:client_options])
64
68
  self.es_index_name = (options[:prefix_name] ? Mongoid::Elasticsearch.prefix : '') + (options[:index_name] || model_name.plural)
65
69
  self.es_index_options = options[:index_options]
66
70
  self.es_wrapper = options[:wrapper]
71
+ self.es_skip_create = options[:skip_create]
67
72
 
68
73
  Mongoid::Elasticsearch.registered_indexes.push self.es_index_name
74
+ Mongoid::Elasticsearch.registered_indexes.uniq!
75
+
69
76
  Mongoid::Elasticsearch.registered_models.push self.name
70
-
77
+ Mongoid::Elasticsearch.registered_models.uniq!
78
+
71
79
  unless options[:index_mappings].nil?
72
80
  self.es_index_options = self.es_index_options.deep_merge({
73
81
  :mappings => {
@@ -84,9 +92,10 @@ module Mongoid
84
92
  end
85
93
 
86
94
  def self.create_all_indexes!
87
- puts "creating ES indexes"
95
+ # puts "creating ES indexes"
88
96
  Mongoid::Elasticsearch.registered_models.each do |model_name|
89
- model_name.constantize.es.index.create
97
+ model = model_name.constantize
98
+ model.es.index.create unless model.es_skip_create
90
99
  end
91
100
  end
92
101
 
@@ -4,13 +4,26 @@ module Mongoid
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- after_save do
7
+ after_save :update_es_index
8
+ after_destroy :update_es_index
9
+
10
+ def update_es_index
8
11
  es_update
9
12
  end
13
+ end
10
14
 
11
- after_destroy do
12
- es_update
13
- end
15
+ module ClassMethods
16
+ def without_es_update!( &block )
17
+ skip_callback( :save, :after, :update_es_index )
18
+ skip_callback( :destroy, :after, :update_es_index )
19
+
20
+ result = yield
21
+
22
+ set_callback( :save, :after, :update_es_index )
23
+ set_callback( :destroy, :after, :update_es_index )
24
+
25
+ result
26
+ end
14
27
  end
15
28
  end
16
29
  end
@@ -4,8 +4,8 @@ module Mongoid::Elasticsearch
4
4
  require File.expand_path('../tasks', __FILE__)
5
5
  end
6
6
 
7
- initializer 'elasticsearch.load_app' do
8
- Mongoid::Elasticsearch.create_all_indexes!
7
+ config.after_initialize do
8
+ Mongoid::Elasticsearch.create_all_indexes! if Mongoid::Elasticsearch.autocreate_indexes
9
9
  end
10
10
  end
11
11
  end
@@ -55,7 +55,12 @@ module Mongoid
55
55
  if @multi
56
56
  multi_with_load
57
57
  else
58
- @model.find(hits.map { |h| h['_id'] })
58
+ records = @model.find(hits.map { |h| h['_id'] })
59
+ hits.map do |item|
60
+ records.detect do |record|
61
+ record.id.to_s == item['_id'].to_s
62
+ end
63
+ end
59
64
  end
60
65
  when :mash
61
66
  hits.map do |h|
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Elasticsearch
3
- VERSION = "0.7.0"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
  end
@@ -0,0 +1,9 @@
1
+ class NoAutocreate
2
+ include Mongoid::Document
3
+
4
+ field :name, type: String
5
+
6
+ include Mongoid::Elasticsearch
7
+ elasticsearch! skip_create: true
8
+ end
9
+
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # encoding: utf-8
2
+
2
3
  require "spec_helper"
3
4
 
4
5
  describe Article do
@@ -39,6 +40,7 @@ describe Article do
39
40
  end
40
41
  end
41
42
 
43
+
42
44
  context 'searching' do
43
45
  before :each do
44
46
  @article_1 = Article.create!(name: 'test article name likes', tags: 'likely')
@@ -71,6 +73,11 @@ describe Article do
71
73
  results.first.to_param.should eq @article_2.name.to_url
72
74
  end
73
75
 
76
+ it 'mongoid_slug with sort and wrapper: :load' do
77
+ results = Article.es.search body: { query: { match_all: {} }, sort: { 'name.raw' => 'desc' } }
78
+ results.map( &:id ).should eq Article.all.desc( :name ).map( &:id )
79
+ end
80
+
74
81
  it 'mongoid_slug with wrapper: :model' do
75
82
  sleep 3
76
83
  results = Article.es.search 'likely', wrapper: :model
@@ -103,6 +110,7 @@ describe Article do
103
110
  else
104
111
  pending "completion suggester not supported in ES version #{Article.es.version}"
105
112
  end
113
+
106
114
  end
107
115
 
108
116
  context 'pagination' do
@@ -401,3 +409,19 @@ describe 'utils' do
401
409
  Mongoid::Elasticsearch::Utils.clean(' test test ').should eq 'test test'
402
410
  end
403
411
  end
412
+
413
+
414
+ describe 'no autocreate' do
415
+ it "desn't create index automatically" do
416
+ NoAutocreate.es.index.delete
417
+ NoAutocreate.es.index.exists?.should be_false
418
+
419
+ Mongoid::Elasticsearch.create_all_indexes!
420
+ NoAutocreate.es.index.exists?.should be_false
421
+
422
+ NoAutocreate.es.index.force_create
423
+ NoAutocreate.es.index.exists?.should be_true
424
+
425
+ NoAutocreate.es.index.delete
426
+ end
427
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-elasticsearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-18 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -197,6 +197,7 @@ files:
197
197
  - ".ruby-gemset"
198
198
  - ".ruby-version"
199
199
  - ".travis.yml"
200
+ - CHANGELOG.md
200
201
  - Gemfile
201
202
  - MIT-LICENSE.txt
202
203
  - README.md
@@ -219,6 +220,7 @@ files:
219
220
  - mongoid-elasticsearch.gemspec
220
221
  - spec/models/article.rb
221
222
  - spec/models/namespaced.rb
223
+ - spec/models/no_autocreate.rb
222
224
  - spec/models/nowrapper.rb
223
225
  - spec/models/post.rb
224
226
  - spec/mongoid_elasticsearch_spec.rb
@@ -250,6 +252,7 @@ summary: Simple and easy integration of mongoid with the new elasticsearch gem
250
252
  test_files:
251
253
  - spec/models/article.rb
252
254
  - spec/models/namespaced.rb
255
+ - spec/models/no_autocreate.rb
253
256
  - spec/models/nowrapper.rb
254
257
  - spec/models/post.rb
255
258
  - spec/mongoid_elasticsearch_spec.rb