elastic_searchable 0.1.6 → 0.2.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.1.6
1
+ 0.2.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.1.6"
8
+ s.version = "0.2.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-01-26}
12
+ s.date = %q{2011-01-28}
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 = [
@@ -7,6 +7,9 @@ module ElasticSearchable
7
7
  def searcher
8
8
  @searcher ||= ElasticSearch.new("localhost:9200")
9
9
  end
10
+ def backgrounded_options
11
+ {:queue => 'elasticsearch'}
12
+ end
10
13
  end
11
14
  end
12
15
 
@@ -15,15 +15,20 @@ module ElasticSearchable
15
15
  attr_accessor :elastic_options
16
16
 
17
17
  # Valid options:
18
- # :index_name (will default class name using method "underscore")
19
- # :if
20
- # :unless
18
+ # :index (optional) configure index to store data in. default to model table name
19
+ # :type (optional) configue type to store data in. default to model table name
20
+ # :index_options (optional) configure index properties (ex: tokenizer)
21
+ # :mapping (optional) configure field properties for this model (ex: skip analyzer for field)
22
+ # :if (optional) reference symbol/proc condition to only index when condition is true
23
+ # :unless (optional) reference symbol/proc condition to skip indexing when condition is true
24
+ # :json (optional) configure the json document to be indexed (see http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json for available options)
21
25
  def elastic_searchable(options = {})
22
26
  options.symbolize_keys!
23
27
  options[:index] ||= self.table_name
24
28
  options[:type] ||= self.table_name
25
29
  options[:index_options] ||= {}
26
30
  options[:mapping] ||= false
31
+ options[:json] ||= {}
27
32
  self.elastic_options = options
28
33
 
29
34
  extend ElasticSearchable::ActiveRecord::Index
@@ -37,15 +42,11 @@ module ElasticSearchable
37
42
  end
38
43
 
39
44
  module InstanceMethods
40
- # build json document to index in elasticsearch
41
- # default implementation simply calls to_json
42
- # implementations can override this method to index custom content
43
45
  def indexed_json_document
44
- self.to_json
46
+ self.as_json self.class.elastic_options[:json]
45
47
  end
46
48
  def index_in_elastic_search(lifecycle = nil)
47
- document = self.indexed_json_document
48
- ElasticSearchable.searcher.index document, self.class.index_options.merge(:id => self.id.to_s)
49
+ ElasticSearchable.searcher.index self.indexed_json_document, self.class.index_options.merge(:id => self.id.to_s)
49
50
 
50
51
  self.run_callbacks("after_index_on_#{lifecycle}".to_sym) if lifecycle
51
52
  self.run_callbacks(:after_index)
@@ -6,9 +6,9 @@ module ElasticSearchable
6
6
 
7
7
  module ClassMethods
8
8
  def add_indexing_callbacks
9
- backgrounded :update_index_on_create => {:queue => 'searchindex'}, :update_index_on_update => {:queue => 'searchindex'}
9
+ backgrounded :update_index_on_create => ElasticSearchable.backgrounded_options, :update_index_on_update => ElasticSearchable.backgrounded_options
10
10
  class << self
11
- backgrounded :delete_id_from_index => {:queue => 'searchindex'}
11
+ backgrounded :delete_id_from_index => ElasticSearchable.backgrounded_options
12
12
  end
13
13
 
14
14
  define_callbacks :after_index_on_create, :after_index_on_update, :after_index
@@ -25,7 +25,6 @@ class TestElasticSearchable < Test::Unit::TestCase
25
25
  t.column :title, :string
26
26
  t.column :body, :string
27
27
  end
28
-
29
28
  create_table :blogs, :force => true do |t|
30
29
  t.column :title, :string
31
30
  t.column :body, :string
@@ -33,10 +32,28 @@ class TestElasticSearchable < Test::Unit::TestCase
33
32
  create_table :users, :force => true do |t|
34
33
  t.column :name, :string
35
34
  end
35
+ create_table :friends, :force => true do |t|
36
+ t.column :name, :string
37
+ t.column :favorite_color, :string
38
+ end
36
39
  end
37
40
 
38
41
  class Post < ActiveRecord::Base
39
42
  elastic_searchable
43
+ after_index :indexed
44
+ after_index_on_create :indexed_on_create
45
+ def indexed
46
+ @indexed = true
47
+ end
48
+ def indexed?
49
+ @indexed
50
+ end
51
+ def indexed_on_create
52
+ @indexed_on_create = true
53
+ end
54
+ def indexed_on_create?
55
+ @indexed_on_create
56
+ end
40
57
  end
41
58
 
42
59
  context 'Post class with default elastic_searchable config' do
@@ -70,6 +87,13 @@ class TestElasticSearchable < Test::Unit::TestCase
70
87
  @post = Post.create :title => 'foo', :body => "bar"
71
88
  Post.create_index
72
89
  end
90
+ should 'have fired after_index callback' do
91
+ assert @post.indexed?
92
+ end
93
+ should 'have fired after_index_on_create callback' do
94
+ assert @post.indexed_on_create?
95
+ end
96
+
73
97
  context 'searching for results' do
74
98
  setup do
75
99
  @results = Post.search 'foo'
@@ -132,4 +156,22 @@ class TestElasticSearchable < Test::Unit::TestCase
132
156
  end
133
157
  end
134
158
  end
159
+
160
+ class Friend < ActiveRecord::Base
161
+ elastic_searchable :json => {:only => [:name]}
162
+ end
163
+ context 'activerecord class with :json=>{}' do
164
+ context 'creating index' do
165
+ setup do
166
+ Friend.delete_all
167
+ @friend = Friend.create! :name => 'bob', :favorite_color => 'red'
168
+ Friend.create_index
169
+ end
170
+ should 'index json with configuration' do
171
+ @response = ElasticSearchable.searcher.get @friend.id, Friend.index_options
172
+ assert_equal 'bob', @response.name
173
+ assert_nil @response.favorite_color
174
+ end
175
+ end
176
+ end
135
177
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
8
+ - 2
9
+ - 0
10
+ version: 0.2.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-01-26 00:00:00 -06:00
18
+ date: 2011-01-28 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency