elastic_searchable 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/elastic_searchable.gemspec +1 -1
- data/lib/elastic_searchable/active_record.rb +7 -12
- data/lib/elastic_searchable/callbacks.rb +2 -2
- data/lib/elastic_searchable/index.rb +13 -5
- data/lib/elastic_searchable/queries.rb +3 -7
- data/test/test_elastic_searchable.rb +50 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/elastic_searchable.gemspec
CHANGED
@@ -12,8 +12,6 @@ module ElasticSearchable
|
|
12
12
|
end
|
13
13
|
|
14
14
|
module ClassMethods
|
15
|
-
attr_accessor :index_name
|
16
|
-
attr_accessor :elastic_search_type
|
17
15
|
attr_accessor :elastic_options
|
18
16
|
|
19
17
|
# Valid options:
|
@@ -22,13 +20,11 @@ module ElasticSearchable
|
|
22
20
|
# :unless
|
23
21
|
def elastic_searchable(options = {})
|
24
22
|
options.symbolize_keys!
|
25
|
-
|
23
|
+
options[:index] ||= self.table_name
|
24
|
+
options[:type] ||= self.table_name
|
25
|
+
options[:index_options] ||= {}
|
26
|
+
options[:mapping] ||= false
|
26
27
|
self.elastic_options = options
|
27
|
-
@index_name = options[:index_name] || self.name.underscore.gsub(/\//,'-')
|
28
|
-
@elastic_search_type = options[:elastic_search_type] || self.name.underscore.singularize.gsub(/\//,'-')
|
29
|
-
|
30
|
-
@index_options = options[:index_options] || {}
|
31
|
-
@mapping = options[:mapping] || false
|
32
28
|
|
33
29
|
extend ElasticSearchable::ActiveRecord::Index
|
34
30
|
extend ElasticSearchable::Queries
|
@@ -47,12 +43,11 @@ module ElasticSearchable
|
|
47
43
|
def indexed_json_document
|
48
44
|
self.to_json
|
49
45
|
end
|
50
|
-
def index_in_elastic_search(
|
51
|
-
options[:id] ||= self.id.to_s
|
46
|
+
def index_in_elastic_search(lifecycle = nil)
|
52
47
|
document = self.indexed_json_document
|
53
|
-
ElasticSearchable.searcher.index document, self.class.
|
48
|
+
ElasticSearchable.searcher.index document, self.class.index_options.merge(:id => self.id.to_s)
|
54
49
|
|
55
|
-
self.run_callbacks("after_index_on_#{
|
50
|
+
self.run_callbacks("after_index_on_#{lifecycle}".to_sym) if lifecycle
|
56
51
|
self.run_callbacks(:after_index)
|
57
52
|
end
|
58
53
|
end
|
@@ -20,10 +20,10 @@ module ElasticSearchable
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def update_index_on_create
|
23
|
-
index_in_elastic_search :
|
23
|
+
index_in_elastic_search :create
|
24
24
|
end
|
25
25
|
def update_index_on_update
|
26
|
-
index_in_elastic_search :
|
26
|
+
index_in_elastic_search :update
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -3,15 +3,16 @@ module ElasticSearchable
|
|
3
3
|
module Index
|
4
4
|
def create_index
|
5
5
|
self.delete_index
|
6
|
-
ElasticSearchable.searcher.create_index
|
7
|
-
|
6
|
+
ElasticSearchable.searcher.create_index index_name, self.elastic_options[:index_options]
|
7
|
+
if mapping = self.elastic_options[:mapping]
|
8
|
+
ElasticSearchable.searcher.update_mapping mapping, self.index_options
|
9
|
+
end
|
8
10
|
|
9
11
|
self.find_each do |record|
|
10
12
|
record.run_callbacks :after_commit_on_update
|
11
13
|
end
|
12
14
|
self.refresh_index
|
13
15
|
end
|
14
|
-
|
15
16
|
# explicitly refresh the index, making all operations performed since the last refresh
|
16
17
|
# available for search
|
17
18
|
#
|
@@ -35,8 +36,15 @@ module ElasticSearchable
|
|
35
36
|
end
|
36
37
|
|
37
38
|
#delete one record from the index
|
38
|
-
def delete_id_from_index(id
|
39
|
-
ElasticSearchable.searcher.delete id.to_s,
|
39
|
+
def delete_id_from_index(id)
|
40
|
+
ElasticSearchable.searcher.delete id.to_s, index_options
|
41
|
+
end
|
42
|
+
|
43
|
+
def index_name
|
44
|
+
self.elastic_options[:index]
|
45
|
+
end
|
46
|
+
def index_options
|
47
|
+
self.elastic_options.slice :index, :type
|
40
48
|
end
|
41
49
|
end
|
42
50
|
end
|
@@ -13,7 +13,7 @@ module ElasticSearchable
|
|
13
13
|
if query.kind_of?(Hash)
|
14
14
|
query = {:query => query}
|
15
15
|
end
|
16
|
-
hits = ElasticSearchable.searcher.search query,
|
16
|
+
hits = ElasticSearchable.searcher.search query, index_options.merge(options)
|
17
17
|
|
18
18
|
results = WillPaginate::Collection.new(hits.current_page, hits.per_page, hits.total_entries)
|
19
19
|
results.replace self.find(hits.collect(&:_id))
|
@@ -25,7 +25,7 @@ module ElasticSearchable
|
|
25
25
|
if query.kind_of?(Hash)
|
26
26
|
query = {:query => query}
|
27
27
|
end
|
28
|
-
ElasticSearchable.searcher.count query,
|
28
|
+
ElasticSearchable.searcher.count query, index_options.merge(options)
|
29
29
|
end
|
30
30
|
|
31
31
|
def facets(fields_list, options = {})
|
@@ -43,7 +43,7 @@ module ElasticSearchable
|
|
43
43
|
options[:facets][field] = {:terms => {:field => field, :size => size}}
|
44
44
|
end
|
45
45
|
|
46
|
-
hits = ElasticSearchable.searcher.search options,
|
46
|
+
hits = ElasticSearchable.searcher.search options, index_options.merge(options)
|
47
47
|
out = {}
|
48
48
|
|
49
49
|
fields_list.each do |field|
|
@@ -55,9 +55,5 @@ module ElasticSearchable
|
|
55
55
|
|
56
56
|
out
|
57
57
|
end
|
58
|
-
|
59
|
-
def elastic_search_options(options = {})
|
60
|
-
options.merge(:index => self.index_name, :type => self.elastic_search_type)
|
61
|
-
end
|
62
58
|
end
|
63
59
|
end
|
@@ -1,5 +1,24 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'helper')
|
2
2
|
|
3
|
+
module ElasticSearch
|
4
|
+
class Client
|
5
|
+
def index_mapping(*args)
|
6
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
7
|
+
indices = args.empty? ? [(default_index || :all)] : args.flatten
|
8
|
+
indices.collect! { |i| [:all].include?(i) ? "_#{i}" : i }
|
9
|
+
execute(:index_mapping, indices, options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
module Transport
|
13
|
+
class HTTP
|
14
|
+
def index_mapping(index_list, options={})
|
15
|
+
standard_request(:get, {:index => index_list, :op => "_mapping"})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
3
22
|
class TestElasticSearchable < Test::Unit::TestCase
|
4
23
|
ActiveRecord::Schema.define(:version => 1) do
|
5
24
|
create_table :posts, :force => true do |t|
|
@@ -11,6 +30,9 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
11
30
|
t.column :title, :string
|
12
31
|
t.column :body, :string
|
13
32
|
end
|
33
|
+
create_table :users, :force => true do |t|
|
34
|
+
t.column :name, :string
|
35
|
+
end
|
14
36
|
end
|
15
37
|
|
16
38
|
class Post < ActiveRecord::Base
|
@@ -24,8 +46,11 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
24
46
|
should 'respond to :search' do
|
25
47
|
assert @clazz.respond_to?(:search)
|
26
48
|
end
|
49
|
+
should 'define elastic_options' do
|
50
|
+
assert @clazz.elastic_options
|
51
|
+
end
|
27
52
|
should 'define index_name' do
|
28
|
-
assert_equal '
|
53
|
+
assert_equal 'posts', @clazz.index_name
|
29
54
|
end
|
30
55
|
end
|
31
56
|
|
@@ -83,4 +108,28 @@ class TestElasticSearchable < Test::Unit::TestCase
|
|
83
108
|
end
|
84
109
|
end
|
85
110
|
end
|
111
|
+
|
112
|
+
class User < ActiveRecord::Base
|
113
|
+
elastic_searchable :mapping => {:properties => {:name => {:type => :string, :index => :not_analyzed}}}
|
114
|
+
end
|
115
|
+
context 'activerecord class with :mapping=>{}' do
|
116
|
+
context 'creating index' do
|
117
|
+
setup do
|
118
|
+
User.create_index
|
119
|
+
@status = ElasticSearchable.searcher.index_mapping User.index_name
|
120
|
+
end
|
121
|
+
should 'have set mapping' do
|
122
|
+
expected = {
|
123
|
+
"users"=> {
|
124
|
+
"users"=> {
|
125
|
+
"properties"=> {
|
126
|
+
"name"=> {"type"=>"string", "index"=>"not_analyzed"}
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
assert_equal expected, @status
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
86
135
|
end
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Sonnek
|