tire 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tire/model/import.rb +7 -0
- data/lib/tire/model/indexing.rb +52 -4
- data/lib/tire/model/naming.rb +20 -0
- data/lib/tire/model/percolate.rb +59 -0
- data/lib/tire/model/persistence.rb +20 -0
- data/lib/tire/model/persistence/attributes.rb +2 -1
- data/lib/tire/model/persistence/finders.rb +2 -0
- data/lib/tire/model/persistence/storage.rb +2 -0
- data/lib/tire/version.rb +1 -1
- metadata +4 -4
data/lib/tire/model/import.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
module Tire
|
2
2
|
module Model
|
3
3
|
|
4
|
+
# Provides support for easy importing of large ActiveRecord- and ActiveModel-bound
|
5
|
+
# recordsets into model index.
|
6
|
+
#
|
7
|
+
# Relies on pagination support in your model, namely the `paginate` class method.
|
8
|
+
#
|
9
|
+
# Please refer to the relevant of the README for more information.
|
10
|
+
#
|
4
11
|
module Import
|
5
12
|
|
6
13
|
module ClassMethods
|
data/lib/tire/model/indexing.rb
CHANGED
@@ -1,16 +1,47 @@
|
|
1
1
|
module Tire
|
2
2
|
module Model
|
3
3
|
|
4
|
+
# Contains logic for definition of index settings and mappings.
|
5
|
+
#
|
4
6
|
module Indexing
|
5
7
|
|
6
8
|
module ClassMethods
|
7
9
|
|
10
|
+
# Define [_settings_](http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html)
|
11
|
+
# for the corresponding index, such as number of shards and replicas, custom analyzers, etc.
|
12
|
+
#
|
13
|
+
# Usage:
|
14
|
+
#
|
15
|
+
# class Article
|
16
|
+
# # ...
|
17
|
+
# settings :number_of_shards => 1 do
|
18
|
+
# mapping do
|
19
|
+
# # ...
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
8
24
|
def settings(*args)
|
9
25
|
@settings ||= {}
|
10
26
|
args.empty? ? (return @settings) : @settings = args.pop
|
11
27
|
yield if block_given?
|
12
28
|
end
|
13
29
|
|
30
|
+
# Define the [_mapping_](http://www.elasticsearch.org/guide/reference/mapping/index.html)
|
31
|
+
# for the corresponding index, telling _ElasticSearch_ how to understand your documents:
|
32
|
+
# what type is which property, whether it is analyzed or no, which analyzer to use, etc.
|
33
|
+
#
|
34
|
+
# Usage:
|
35
|
+
#
|
36
|
+
# class Article
|
37
|
+
# # ...
|
38
|
+
# mapping do
|
39
|
+
# indexes :id, :type => 'string', :index => :not_analyzed
|
40
|
+
# indexes :title, :type => 'string', :analyzer => 'snowball', :boost => 100
|
41
|
+
# # ...
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
14
45
|
def mapping
|
15
46
|
@mapping ||= {}
|
16
47
|
if block_given?
|
@@ -21,6 +52,21 @@ module Tire
|
|
21
52
|
end
|
22
53
|
end
|
23
54
|
|
55
|
+
# Define mapping for the property passed as the first argument (`name`)
|
56
|
+
# using definition from the second argument (`options`).
|
57
|
+
#
|
58
|
+
# `:type` is optional and defaults to `'string'`.
|
59
|
+
#
|
60
|
+
# Usage:
|
61
|
+
#
|
62
|
+
# * Index property but do not analyze it: `indexes :id, :index => :not_analyzed`
|
63
|
+
#
|
64
|
+
# * Use different analyzer for indexing a property: `indexes :title, :analyzer => 'snowball'`
|
65
|
+
#
|
66
|
+
# Please refer to the
|
67
|
+
# [_mapping_ documentation](http://www.elasticsearch.org/guide/reference/mapping/index.html)
|
68
|
+
# for more information.
|
69
|
+
#
|
24
70
|
def indexes(name, options = {}, &block)
|
25
71
|
options[:type] ||= 'string'
|
26
72
|
|
@@ -40,16 +86,18 @@ module Tire
|
|
40
86
|
end
|
41
87
|
end
|
42
88
|
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
|
89
|
+
# Creates the corresponding index with desired settings and mappings, when it does not exists yet.
|
90
|
+
#
|
47
91
|
def create_elasticsearch_index
|
48
92
|
unless index.exists?
|
49
93
|
index.create :mappings => mapping_to_hash, :settings => settings
|
50
94
|
end
|
51
95
|
end
|
52
96
|
|
97
|
+
def store_mapping?
|
98
|
+
@store_mapping || false
|
99
|
+
end
|
100
|
+
|
53
101
|
def mapping_to_hash
|
54
102
|
{ document_type.to_sym => { :properties => mapping } }
|
55
103
|
end
|
data/lib/tire/model/naming.rb
CHANGED
@@ -1,24 +1,44 @@
|
|
1
1
|
module Tire
|
2
2
|
module Model
|
3
3
|
|
4
|
+
# Contains logic for getting and setting the index name and document type for this model.
|
5
|
+
#
|
4
6
|
module Naming
|
5
7
|
|
6
8
|
module ClassMethods
|
9
|
+
|
10
|
+
# Get or set the index name for this model, based on arguments.
|
11
|
+
#
|
12
|
+
# To get the index name:
|
13
|
+
#
|
14
|
+
# Article.index_name
|
15
|
+
#
|
16
|
+
# To set the index name:
|
17
|
+
#
|
18
|
+
# Article.index_name 'my-custom-name'
|
19
|
+
#
|
7
20
|
def index_name name=nil
|
8
21
|
@index_name = name if name
|
9
22
|
@index_name || klass.model_name.plural
|
10
23
|
end
|
11
24
|
|
25
|
+
# Get the document type for this model, based on the class name.
|
26
|
+
#
|
12
27
|
def document_type
|
13
28
|
klass.model_name.singular
|
14
29
|
end
|
15
30
|
end
|
16
31
|
|
17
32
|
module InstanceMethods
|
33
|
+
|
34
|
+
# Proxy to class method `index_name`.
|
35
|
+
#
|
18
36
|
def index_name
|
19
37
|
instance.class.tire.index_name
|
20
38
|
end
|
21
39
|
|
40
|
+
# Proxy to instance method `document_type`.
|
41
|
+
#
|
22
42
|
def document_type
|
23
43
|
instance.class.tire.document_type
|
24
44
|
end
|
data/lib/tire/model/percolate.rb
CHANGED
@@ -1,19 +1,61 @@
|
|
1
1
|
module Tire
|
2
2
|
module Model
|
3
3
|
|
4
|
+
# Contains support for the [percolation](http://www.elasticsearch.org/guide/reference/api/percolate.html)
|
5
|
+
# feature of _ElasticSearch_.
|
6
|
+
#
|
4
7
|
module Percolate
|
5
8
|
|
6
9
|
module ClassMethods
|
10
|
+
|
11
|
+
# Set up the percolation when documents are being added to the index.
|
12
|
+
#
|
13
|
+
# Usage:
|
14
|
+
#
|
15
|
+
# class Article
|
16
|
+
# # ...
|
17
|
+
# percolate!
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# First, you have to register a percolator query:
|
21
|
+
#
|
22
|
+
# Article.index.register_percolator_query('fail') { |query| query.string 'fail' }
|
23
|
+
#
|
24
|
+
# Then, when you update the index, matching queries are returned in the `matches` property:
|
25
|
+
#
|
26
|
+
# p Article.create(:title => 'This is a FAIL!').matches
|
27
|
+
#
|
28
|
+
#
|
29
|
+
# You may pass a pattern to filter which percolator queries will be executed.
|
30
|
+
#
|
31
|
+
# See <http://www.elasticsearch.org/guide/reference/api/index_.html> for more information.
|
32
|
+
#
|
7
33
|
def percolate!(pattern=true)
|
8
34
|
@@_percolator = pattern
|
9
35
|
self
|
10
36
|
end
|
11
37
|
|
38
|
+
# A callback method for intercepting percolator matches.
|
39
|
+
#
|
40
|
+
# Usage:
|
41
|
+
#
|
42
|
+
# class Article
|
43
|
+
# # ...
|
44
|
+
# on_percolate do
|
45
|
+
# puts "Article title “#{title}” matches queries: #{matches.inspect}" unless matches.empty?
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# Based on the response received in `matches`, you may choose to fire notifications,
|
50
|
+
# increment counters, send out e-mail alerts, etc.
|
51
|
+
#
|
12
52
|
def on_percolate(pattern=true,&block)
|
13
53
|
percolate!(pattern)
|
14
54
|
klass.after_update_elasticsearch_index(block)
|
15
55
|
end
|
16
56
|
|
57
|
+
# Returns the status or pattern of percolator for this class.
|
58
|
+
#
|
17
59
|
def percolator
|
18
60
|
defined?(@@_percolator) ? @@_percolator : nil
|
19
61
|
end
|
@@ -21,14 +63,31 @@ module Tire
|
|
21
63
|
|
22
64
|
module InstanceMethods
|
23
65
|
|
66
|
+
# Run this document against registered percolator queries, without indexing it.
|
67
|
+
#
|
68
|
+
# First, register a percolator query:
|
69
|
+
#
|
70
|
+
# Article.index.register_percolator_query('fail') { |query| query.string 'fail' }
|
71
|
+
#
|
72
|
+
# Then, you may query the percolator endpoint with:
|
73
|
+
#
|
74
|
+
# p Article.new(:title => 'This is a FAIL!').percolate
|
75
|
+
#
|
76
|
+
# Optionally, you may pass a block to filter which percolator queries will be executed.
|
77
|
+
#
|
78
|
+
# See <http://www.elasticsearch.org/guide/reference/api/percolate.html> for more information.
|
24
79
|
def percolate(&block)
|
25
80
|
index.percolate instance, block
|
26
81
|
end
|
27
82
|
|
83
|
+
# Mark this instance for percolation when adding it to the index.
|
84
|
+
#
|
28
85
|
def percolate=(pattern)
|
29
86
|
@_percolator = pattern
|
30
87
|
end
|
31
88
|
|
89
|
+
# Returns the status or pattern of percolator for this instance.
|
90
|
+
#
|
32
91
|
def percolator
|
33
92
|
@_percolator || instance.class.tire.percolator || nil
|
34
93
|
end
|
@@ -1,6 +1,26 @@
|
|
1
1
|
module Tire
|
2
2
|
module Model
|
3
3
|
|
4
|
+
# Allows to use _ElasticSearch_ as a primary database (storage).
|
5
|
+
#
|
6
|
+
# Contains all the `Tire::Model::Search` features and provides
|
7
|
+
# an [_ActiveModel_](http://rubygems.org/gems/activemodel)-compatible
|
8
|
+
# interface for persistance.
|
9
|
+
#
|
10
|
+
# Usage:
|
11
|
+
#
|
12
|
+
# class Article
|
13
|
+
# include Tire::Model::Persistence
|
14
|
+
#
|
15
|
+
# property :title
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Article.create :id => 1, :title => 'One'
|
19
|
+
#
|
20
|
+
# article = Article.find
|
21
|
+
#
|
22
|
+
# article.destroy
|
23
|
+
#
|
4
24
|
module Persistence
|
5
25
|
|
6
26
|
def self.included(base)
|
@@ -3,12 +3,13 @@ module Tire
|
|
3
3
|
|
4
4
|
module Persistence
|
5
5
|
|
6
|
+
# Provides infrastructure for declaring the model properties and accessing them.
|
7
|
+
#
|
6
8
|
module Attributes
|
7
9
|
|
8
10
|
module ClassMethods
|
9
11
|
|
10
12
|
def property(name, options = {})
|
11
|
-
# p "#{self}, PERSISTENCE PROPERTY, #{name}"
|
12
13
|
attr_accessor name.to_sym
|
13
14
|
properties << name.to_s unless properties.include?(name.to_s)
|
14
15
|
define_query_method name.to_sym
|
data/lib/tire/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 2
|
9
|
+
version: 0.3.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Karel Minarik
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-09-
|
17
|
+
date: 2011-09-04 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -338,7 +338,7 @@ post_install_message: |
|
|
338
338
|
When there's no conflict with existing methods, Tire methods are added
|
339
339
|
to the class namespace, as well, so the change is 100% backwards-compatible.
|
340
340
|
|
341
|
-
See the full changelog at <http://github.com/karmi/tire/commits/v0.3.
|
341
|
+
See the full changelog at <http://github.com/karmi/tire/commits/v0.3.2>.
|
342
342
|
|
343
343
|
--------------------------------------------------------------------------------
|
344
344
|
|