elastic_record 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+ # gem 'arelastic', path: '~/code/arelastic'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Matthew Higgins
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task default: :test
6
+
7
+ desc 'Test.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.libs << 'test'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'elastic_record'
5
+ s.version = '0.3.1'
6
+ s.summary = 'Use Elastic Search with your objects'
7
+ s.description = 'Find your records with elastic search'
8
+
9
+ s.required_ruby_version = '>= 1.9.3'
10
+ s.required_rubygems_version = ">= 1.8.11"
11
+ s.license = 'MIT'
12
+
13
+ s.author = 'Matthew Higgins'
14
+ s.email = 'developer@matthewhiggins.com'
15
+ s.homepage = 'http://github.com/matthuhiggins/elastic_record'
16
+
17
+ s.extra_rdoc_files = ['README.rdoc']
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
20
+
21
+ s.add_dependency 'arelastic'
22
+ s.add_dependency 'rubberband', '>= 0.1.1'
23
+ s.add_dependency 'activemodel'
24
+ end
@@ -0,0 +1,15 @@
1
+ module ElasticRecord
2
+ module Callbacks
3
+ def self.included(base)
4
+ base.class_eval do
5
+ after_save do
6
+ elastic_index.index_record self
7
+ end
8
+
9
+ after_destroy do
10
+ elastic_index.delete_record self
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -37,7 +37,7 @@ module ElasticRecord
37
37
  }
38
38
  ]
39
39
 
40
- aliased_indexes.each do |index_to_remove|
40
+ aliased_names.each do |index_to_remove|
41
41
  actions << {
42
42
  remove: {
43
43
  "index" => index_to_remove,
@@ -50,11 +50,20 @@ module ElasticRecord
50
50
  end
51
51
 
52
52
  def update_mapping(index_name)
53
- connection.update_mapping(mapping, index: index_name)
54
- # json_put "/#{index_name}/#{type}/_mapping", type => mapping
53
+ # connection.update_mapping(mapping, index: index_name)
54
+ json_put "/#{index_name}/#{type}/_mapping", type => mapping
55
55
  end
56
56
 
57
- def aliased_indexes
57
+ def refresh
58
+ connection.refresh
59
+ end
60
+
61
+ def reset
62
+ delete_all
63
+ create_and_deploy
64
+ end
65
+
66
+ def aliased_names
58
67
  json = json_get '/_cluster/state'
59
68
  json["metadata"]["indices"].select { |name, status| status["aliases"].include?(alias_name) }.map { |name, status| name }
60
69
  end
@@ -0,0 +1,72 @@
1
+ module ElasticRecord
2
+ class Task
3
+ def self.get_models
4
+ if class_name = ENV['CLASS']
5
+ [class_name.camelize.constantize]
6
+ else
7
+ IndexedModels.all
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ namespace :index do
14
+ desc "Create index for CLASS or all models."
15
+ task create: :environment do
16
+ ElasticRecord::Task.get_models.each do |model|
17
+ # begin
18
+ index_name = model.elastic_index.create_and_deploy
19
+ logger.info "Created #{model.name} index (#{index_name})"
20
+ # rescue => e
21
+ # if e.message =~ /IndexAlreadyExistsException/
22
+ # logger.info "#{model.name} index already exists"
23
+ # else
24
+ # raise e
25
+ # end
26
+ # end
27
+ end
28
+ end
29
+
30
+ desc "Drop index for CLASS or all models."
31
+ task drop: :environment do
32
+ ElasticRecord::Task.get_models.each do |model|
33
+ # begin
34
+ model.elastic_index.delete_all
35
+ logger.info "Dropped #{model.name} index"
36
+ # rescue => e
37
+ # if e.message =~ /IndexMissingException/
38
+ # logger.info "#{model.name} index does not exist"
39
+ # else
40
+ # raise e
41
+ # end
42
+ # end
43
+ end
44
+ end
45
+
46
+ desc "Recreate index for CLASS or all models."
47
+ task reset: ['index:drop', 'index:create']
48
+
49
+ desc "Add records to index. Deploys a new index by default, or specify INDEX"
50
+ task build: :environment do
51
+ ElasticRecord::Task.get_models.each do |model|
52
+ logger.info "Building #{model.name} index."
53
+
54
+ if ENV['INDEX']
55
+ index_name = ENV['INDEX']
56
+ else
57
+ logger.info " Creating index..."
58
+ index_name = model.elastic_index.create
59
+ end
60
+
61
+ logger.info " Reindexing into #{index_name}"
62
+ model.find_in_batches do |records|
63
+ model.elastic_index.bulk_add(records, index_name)
64
+ end
65
+
66
+ logger.info " Deploying index..."
67
+ model.elastic_index.deploy(index_name)
68
+
69
+ logger.info " Done."
70
+ end
71
+ end
72
+ end
@@ -4,7 +4,7 @@ class ElasticRecord::Index::DocumentsTest < MiniTest::Spec
4
4
  def setup
5
5
  super
6
6
 
7
- Widget.reset_index!
7
+ index.reset
8
8
  end
9
9
 
10
10
  def test_index_record
@@ -25,10 +25,10 @@ class ElasticRecord::Index::ManageTest < MiniTest::Spec
25
25
  def test_deploy
26
26
  index.create 'widgets_foo'
27
27
 
28
- assert index.aliased_indexes.empty?
28
+ assert index.aliased_names.empty?
29
29
  index.deploy 'widgets_foo'
30
30
 
31
- assert_equal ['widgets_foo'], index.aliased_indexes
31
+ assert_equal ['widgets_foo'], index.aliased_names
32
32
  end
33
33
 
34
34
  private
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::Relation::BatchesTest < MiniTest::Spec
4
4
  def setup
5
- Widget.reset_index!
5
+ Widget.elastic_index.reset
6
6
  create_widgets
7
7
  end
8
8
 
@@ -38,6 +38,6 @@ class ElasticRecord::Relation::BatchesTest < MiniTest::Spec
38
38
  Widget.new(id: 15, color: 'green'),
39
39
  ]
40
40
 
41
- Widget.elastic_connection.refresh
41
+ Widget.elastic_index.refresh
42
42
  end
43
43
  end
@@ -2,12 +2,12 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::Relation::DelegationTest < MiniTest::Spec
4
4
  def setup
5
- Widget.reset_index!
5
+ Widget.elastic_index.reset
6
6
  end
7
7
 
8
8
  def test_delegate_to_array
9
- Widget.elastic_connection.index({'widget' => {'color' => 'red'}}, {index: 'widgets', type: 'widget', id: 5})
10
- Widget.elastic_connection.refresh
9
+ Widget.elastic_index.index_record(Widget.new(id: 5, color: 'red'))
10
+ Widget.elastic_index.refresh
11
11
 
12
12
  records = []
13
13
  Widget.elastic_relation.each do |record|
@@ -26,7 +26,7 @@ class ElasticRecord::Relation::DelegationTest < MiniTest::Spec
26
26
 
27
27
  result = model.elastic_relation.filter('foo' => 'bar').do_it
28
28
 
29
- expected = {"query"=>{"constant_score"=>{"filter"=>{"term"=>{"foo"=>"bar"}}}}}
29
+ expected = {"query" => {"constant_score" => {"filter" => {"term" => {"foo" => "bar"}}}}}
30
30
  assert_equal expected, result
31
31
  end
32
32
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Spec
4
4
  def setup
5
- Widget.reset_index!
5
+ Widget.elastic_index.reset
6
6
  create_widgets
7
7
  end
8
8
 
@@ -34,9 +34,11 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Spec
34
34
  private
35
35
 
36
36
  def create_widgets
37
- Widget.elastic_connection.index({'widget' => {'color' => 'red'}}, {index: 'widgets', type: 'widget', id: '05'})
38
- Widget.elastic_connection.index({'widget' => {'color' => 'blue'}}, {index: 'widgets', type: 'widget', id: '10'})
37
+ Widget.elastic_index.bulk_add [
38
+ Widget.new(color: 'red', id: '05'),
39
+ Widget.new(color: 'blue', id: '10'),
40
+ ]
39
41
 
40
- Widget.elastic_connection.refresh
42
+ Widget.elastic_index.refresh
41
43
  end
42
44
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::RelationTest < MiniTest::Spec
4
4
  def setup
5
- Widget.reset_index!
5
+ Widget.elastic_index.reset
6
6
  create_widgets
7
7
  end
8
8
 
@@ -44,9 +44,11 @@ class ElasticRecord::RelationTest < MiniTest::Spec
44
44
 
45
45
  private
46
46
  def create_widgets
47
- Widget.elastic_connection.index({'widget' => {'color' => 'red'}}, {index: 'widgets', type: 'widget', id: 5})
48
- Widget.elastic_connection.index({'widget' => {'color' => 'blue'}}, {index: 'widgets', type: 'widget', id: 10})
49
-
50
- Widget.elastic_connection.refresh
47
+ Widget.elastic_index.bulk_add [
48
+ Widget.new(id: 5, color: 'red'),
49
+ Widget.new(id: 10, color: 'blue')
50
+ ]
51
+
52
+ Widget.elastic_index.refresh
51
53
  end
52
54
  end
@@ -1,5 +1,8 @@
1
1
  class Widget
2
2
  extend ActiveModel::Naming
3
+ extend ActiveModel::Callbacks
4
+ define_model_callbacks :save, :destroy
5
+
3
6
  include ElasticRecord::Model
4
7
 
5
8
  self.elastic_index.mapping[:properties].update(color: {
@@ -21,11 +24,6 @@ class Widget
21
24
  instance_eval(&block)
22
25
  end
23
26
  end
24
-
25
- def reset_index!
26
- elastic_index.delete_all
27
- elastic_index.create_and_deploy
28
- end
29
27
  end
30
28
 
31
29
  attr_accessor :id, :color
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-04 00:00:00.000000000 Z
12
+ date: 2012-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic
@@ -66,24 +66,32 @@ extensions: []
66
66
  extra_rdoc_files:
67
67
  - README.rdoc
68
68
  files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - MIT-LICENSE
72
+ - README.rdoc
73
+ - Rakefile
74
+ - elastic_record.gemspec
75
+ - lib/elastic_record.rb
76
+ - lib/elastic_record/callbacks.rb
69
77
  - lib/elastic_record/config.rb
70
78
  - lib/elastic_record/connection.rb
79
+ - lib/elastic_record/index.rb
71
80
  - lib/elastic_record/index/documents.rb
72
81
  - lib/elastic_record/index/manage.rb
73
82
  - lib/elastic_record/index/mapping.rb
74
- - lib/elastic_record/index.rb
75
83
  - lib/elastic_record/model.rb
76
84
  - lib/elastic_record/orm/active_record.rb
77
85
  - lib/elastic_record/railtie.rb
86
+ - lib/elastic_record/relation.rb
78
87
  - lib/elastic_record/relation/batches.rb
79
88
  - lib/elastic_record/relation/delegation.rb
80
89
  - lib/elastic_record/relation/finder_methods.rb
81
90
  - lib/elastic_record/relation/merging.rb
82
91
  - lib/elastic_record/relation/search_methods.rb
83
92
  - lib/elastic_record/relation/value_methods.rb
84
- - lib/elastic_record/relation.rb
85
93
  - lib/elastic_record/searching.rb
86
- - lib/elastic_record.rb
94
+ - lib/elastic_record/tasks/index.rake
87
95
  - test/elastic_record/config_test.rb
88
96
  - test/elastic_record/connection_test.rb
89
97
  - test/elastic_record/index/documents_test.rb
@@ -101,7 +109,6 @@ files:
101
109
  - test/helper.rb
102
110
  - test/support/connect.rb
103
111
  - test/support/widget.rb
104
- - README.rdoc
105
112
  homepage: http://github.com/matthuhiggins/elastic_record
106
113
  licenses:
107
114
  - MIT