activesearch 0.0.5 → 0.0.6
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/lib/activesearch/base.rb +29 -0
- data/lib/activesearch/elastic_search.rb +46 -23
- data/lib/activesearch/mongoid.rb +2 -19
- data/lib/activesearch/version.rb +1 -1
- data/spec/models/elastic_search.rb +2 -5
- data/spec/models/mongoid.rb +3 -2
- data/spec/spec_helper.rb +11 -0
- metadata +3 -2
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveSearch
|
2
|
+
module Base
|
3
|
+
def search_by(*params)
|
4
|
+
@search_parameters = params
|
5
|
+
self.after_save :reindex, self.search_conditions
|
6
|
+
self.after_destroy :deindex, self.search_conditions
|
7
|
+
end
|
8
|
+
|
9
|
+
def search_options
|
10
|
+
search_parameters.last.is_a?(Hash) ? search_parameters.last : search_parameters
|
11
|
+
end
|
12
|
+
|
13
|
+
def search_conditions
|
14
|
+
{}.tap do |conditions|
|
15
|
+
conditions.merge!(if: search_options[:if]) if search_options.has_key?(:if)
|
16
|
+
conditions.merge!(unless: search_options[:unless]) if search_options.has_key?(:unless)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_fields
|
21
|
+
search_parameters.last.is_a?(Hash) ? search_parameters[0...-1] : search_parameters
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
def search_parameters
|
26
|
+
@search_parameters.first.respond_to?(:call) ? @search_parameters.first.call : @search_parameters
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
|
+
require 'tire'
|
1
2
|
require "active_support/core_ext"
|
3
|
+
require "activesearch/base"
|
2
4
|
require "activesearch/elastic_search/proxy"
|
3
5
|
|
4
6
|
module ActiveSearch
|
@@ -9,34 +11,55 @@ module ActiveSearch
|
|
9
11
|
|
10
12
|
module ElasticSearch
|
11
13
|
def self.included(base)
|
14
|
+
base.extend Base
|
12
15
|
base.extend ClassMethods
|
13
16
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
|
18
|
+
def to_indexable
|
19
|
+
self.attributes.merge(_type: self.elastic_type)
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
def elastic_type
|
24
|
+
@elastic_type ||= self.type.gsub!(/(.)([A-Z])/,'\1_\2').downcase
|
25
|
+
end
|
26
|
+
|
27
|
+
def elastic_index(&block)
|
28
|
+
Tire.index(elastic_type, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def reindex
|
32
|
+
doc = self.to_indexable
|
33
|
+
properties = self.class.elastic_properties
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
35
|
+
elastic_index do
|
36
|
+
unless exists?
|
37
|
+
create({ mappings: { doc[:_type] => {properties: properties}}})
|
38
|
+
end
|
39
|
+
store doc
|
28
40
|
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
def deindex
|
44
|
+
doc = self.to_indexable
|
45
|
+
elastic_index do
|
46
|
+
remove doc
|
35
47
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
48
|
+
end
|
49
|
+
|
50
|
+
module ClassMethods
|
51
|
+
def elastic_properties
|
52
|
+
props = {}
|
53
|
+
|
54
|
+
search_fields.each_with_object(props) do |field,hash|
|
55
|
+
hash[field] = {type: 'string'}
|
56
|
+
end
|
57
|
+
|
58
|
+
(Array(search_options[:store]) - search_fields).each_with_object(props) do |field,hash|
|
59
|
+
hash[field] = {type: 'string', :index => :no}
|
60
|
+
end
|
61
|
+
|
62
|
+
props
|
40
63
|
end
|
41
64
|
end
|
42
65
|
end
|
data/lib/activesearch/mongoid.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'activesearch/base'
|
1
2
|
require 'activesearch/mongoid/model'
|
2
3
|
|
3
4
|
module ActiveSearch
|
@@ -10,7 +11,7 @@ module ActiveSearch
|
|
10
11
|
|
11
12
|
module Mongoid
|
12
13
|
def self.included(base)
|
13
|
-
base.extend
|
14
|
+
base.extend Base
|
14
15
|
end
|
15
16
|
|
16
17
|
protected
|
@@ -21,23 +22,5 @@ module ActiveSearch
|
|
21
22
|
def deindex
|
22
23
|
ActiveSearch::Mongoid::Model.deindex(self)
|
23
24
|
end
|
24
|
-
|
25
|
-
module ClassMethods
|
26
|
-
def search_options
|
27
|
-
@search_options
|
28
|
-
end
|
29
|
-
|
30
|
-
def search_fields
|
31
|
-
@search_fields
|
32
|
-
end
|
33
|
-
|
34
|
-
def search_by(*fields)
|
35
|
-
@search_options = fields.pop if fields.last.is_a?(Hash)
|
36
|
-
conditions = {if: @search_options.delete(:if), unless: @search_options.delete(:unless)}
|
37
|
-
@search_fields = fields
|
38
|
-
self.after_save :reindex, conditions
|
39
|
-
self.after_destroy :deindex, conditions
|
40
|
-
end
|
41
|
-
end
|
42
25
|
end
|
43
26
|
end
|
data/lib/activesearch/version.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
|
-
require 'tire'
|
2
1
|
require 'activesearch/elastic_search'
|
3
2
|
|
4
|
-
Tire.configure { logger File.join(File.dirname(__FILE__), '..', '..', 'log', 'elasticsearch.log'), :level => 'debug' }
|
5
|
-
|
6
3
|
module ElasticSearchRefresh
|
7
4
|
|
8
5
|
def save
|
9
|
-
super.tap {
|
6
|
+
super.tap { Tire.index('_all') { refresh }}
|
10
7
|
end
|
11
8
|
|
12
9
|
def destroy
|
13
|
-
super.tap {
|
10
|
+
super.tap { Tire.index('_all') { refresh }}
|
14
11
|
end
|
15
12
|
end
|
16
13
|
|
data/spec/models/mongoid.rb
CHANGED
@@ -19,7 +19,7 @@ class AnotherMongoidModel
|
|
19
19
|
include ActiveSearch::Mongoid
|
20
20
|
|
21
21
|
field :title, type: String
|
22
|
-
search_by :title, :text, store: [:title]
|
22
|
+
search_by lambda { [:title, :text, store: [:title]] } # Simulating dynamic options
|
23
23
|
end
|
24
24
|
|
25
25
|
|
@@ -28,5 +28,6 @@ class LocalizedMongoidModel
|
|
28
28
|
include ActiveSearch::Mongoid
|
29
29
|
|
30
30
|
field :title, localize: true
|
31
|
-
|
31
|
+
field :special_type
|
32
|
+
search_by :title, store: [:title], type: :special_type
|
32
33
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,6 +9,7 @@ class ActiveMimic
|
|
9
9
|
include ActiveAttr::MassAssignment
|
10
10
|
|
11
11
|
attribute :id
|
12
|
+
attribute :type
|
12
13
|
|
13
14
|
define_model_callbacks :save
|
14
15
|
define_model_callbacks :destroy
|
@@ -17,7 +18,12 @@ class ActiveMimic
|
|
17
18
|
new(attrs).tap(&:save)
|
18
19
|
end
|
19
20
|
|
21
|
+
def type
|
22
|
+
self.class.to_s
|
23
|
+
end
|
24
|
+
|
20
25
|
def save
|
26
|
+
self.id = self.class.next_id
|
21
27
|
run_callbacks :save do
|
22
28
|
true
|
23
29
|
end
|
@@ -28,4 +34,9 @@ class ActiveMimic
|
|
28
34
|
true
|
29
35
|
end
|
30
36
|
end
|
37
|
+
|
38
|
+
def self.next_id
|
39
|
+
@next_id ||= 0
|
40
|
+
@next_id += 1
|
41
|
+
end
|
31
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activesearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
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-11-
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- Rakefile
|
122
122
|
- activesearch.gemspec
|
123
123
|
- lib/activesearch.rb
|
124
|
+
- lib/activesearch/base.rb
|
124
125
|
- lib/activesearch/elastic_search.rb
|
125
126
|
- lib/activesearch/elastic_search/proxy.rb
|
126
127
|
- lib/activesearch/mongoid.rb
|