elastic_record 0.6.8 → 0.6.9
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/README.rdoc +91 -14
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record/index/manage.rb +3 -3
- data/lib/elastic_record/tasks/index.rake +7 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -19,25 +19,102 @@ Include ElasticRecord into your model:
|
|
19
19
|
include ElasticRecord::Model
|
20
20
|
end
|
21
21
|
|
22
|
-
==
|
22
|
+
== Searching
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
ElasticRecord adds the method 'elastic_search' to your models. It works similar to active_record scoping:
|
25
|
+
|
26
|
+
search = Product.elastic_search
|
27
|
+
|
28
|
+
=== Filtering
|
29
|
+
|
30
|
+
If a simple hash is passed into filter, a term or terms query is created:
|
31
|
+
|
32
|
+
search.filter(color: 'red') # Creates a term filter
|
33
|
+
search.filter(color: %w(red blue)) # Creates a terms filter
|
34
|
+
|
35
|
+
If a hash containing hashes is passed into filter, it is used directly as a filter DSL expression:
|
36
|
+
|
37
|
+
search.filter(prefix: { name: "Sca" }) # Creates a prefix filter
|
38
|
+
|
39
|
+
An Arelastic object can also be passed in, working similarily to Arel:
|
40
|
+
|
41
|
+
# Name starts with 'Sca'
|
42
|
+
search.filter(Product.arelastic[:name].prefix("Sca"))
|
43
|
+
|
44
|
+
# Name does not start with 'Sca'
|
45
|
+
search.filter(Product.arelastic[:name].prefix("Sca").negated)
|
46
|
+
|
47
|
+
# Size is greater than 5
|
48
|
+
search.filter(Product.arelastic[:size].gt(5))
|
49
|
+
|
50
|
+
# Product has no name
|
51
|
+
search.filter(Product.arelastic[:name].blank)
|
52
|
+
|
53
|
+
# Product has name
|
54
|
+
search.filter(Product.arelastic[:name].present)
|
55
|
+
|
56
|
+
# Name is 'hola' or name is blank
|
57
|
+
search.filter(Product.arelastic[:name].eq("hola").or(Product.arelastic[:name].blank))
|
58
|
+
|
59
|
+
Helpful Arel builders can be found at https://github.com/matthuhiggins/arelastic/blob/master/lib/arelastic/builders/filter.rb.
|
60
|
+
|
61
|
+
=== Querying
|
62
|
+
|
63
|
+
To create a query string, pass a string to search.query:
|
64
|
+
|
65
|
+
search.query("red AND fun*") # Creates {query_string: {"red AND fun*"}}
|
66
|
+
|
67
|
+
Complex queries are done using either a hash or an arelastic object:
|
68
|
+
|
69
|
+
search.query(match: {description: "amazing"})
|
70
|
+
|
71
|
+
=== Ordering
|
72
|
+
|
73
|
+
search.order(:price) # sort by price
|
74
|
+
search.order(:color, :price) # sort by color, then price
|
75
|
+
search.order(price: :desc) # sort by price in descending order
|
76
|
+
|
77
|
+
=== Offsets and Limits
|
78
|
+
|
79
|
+
To change the 'size' and 'from' values of a query, use offset and limit:
|
80
|
+
|
81
|
+
search.limit(10).offset(20) # Creates a query with {size: 10, from: 20}
|
82
|
+
|
83
|
+
|
84
|
+
=== Facets
|
85
|
+
|
86
|
+
Since term facets are the most common, they are the easiest to add to a query:
|
87
|
+
|
88
|
+
search.facet('colors')
|
89
|
+
|
90
|
+
It is important to note that adding facets to a query is different than retrieving the results of the query:
|
91
|
+
|
92
|
+
search = search.facet('colors').facet('size')
|
93
|
+
search.facets
|
94
|
+
#=> {"colors" => ..., "size" => ...}
|
95
|
+
|
96
|
+
=== Getting Results
|
97
|
+
|
98
|
+
A search object behaves similar to an active_record scope, implementing a few methods of its own and delegating the rest to Array, and your class.
|
99
|
+
|
100
|
+
search.count # Return the number of search results
|
101
|
+
search.first # Limit results to 1 and return the first result or nil
|
102
|
+
search.find(id) # Add an ids filter to the existing query
|
103
|
+
search.as_elastic # Return the json hash that will be sent to elastic search.
|
104
|
+
|
105
|
+
The search object behaves like an array when necessary:
|
106
|
+
|
107
|
+
search.each do |product|
|
32
108
|
...
|
33
109
|
end
|
34
110
|
|
35
|
-
Class methods
|
111
|
+
Class methods can be executed within scopes:
|
36
112
|
|
37
|
-
class
|
38
|
-
def self.
|
39
|
-
all.each do { |
|
113
|
+
class Product
|
114
|
+
def self.increase_prices
|
115
|
+
all.each do { |product| product.increment(:price, 10) }
|
40
116
|
end
|
41
117
|
end
|
42
118
|
|
43
|
-
|
119
|
+
# Increase the price of all red products by $10.
|
120
|
+
Product.filter(color: 'red').increase_prices
|
data/elastic_record.gemspec
CHANGED
@@ -49,12 +49,12 @@ module ElasticRecord
|
|
49
49
|
connection.json_post '/_aliases', actions: actions
|
50
50
|
end
|
51
51
|
|
52
|
-
def update_mapping(index_name)
|
52
|
+
def update_mapping(index_name = alias_name)
|
53
53
|
connection.json_put "/#{index_name}/#{type}/_mapping", type => mapping
|
54
54
|
end
|
55
55
|
|
56
|
-
def refresh
|
57
|
-
connection.json_post "/#{
|
56
|
+
def refresh(index_name = alias_name)
|
57
|
+
connection.json_post "/#{index_name}/_refresh"
|
58
58
|
end
|
59
59
|
|
60
60
|
def reset
|
@@ -46,6 +46,13 @@ namespace :index do
|
|
46
46
|
desc "Recreate index for CLASS or all models."
|
47
47
|
task reset: ['index:drop', 'index:create']
|
48
48
|
|
49
|
+
task update_mapping: :environment do
|
50
|
+
ElasticRecord::Task.get_models.each do |model|
|
51
|
+
model.elastic_index.create_and_deploy
|
52
|
+
logger.info "Updated mapping for #{model.name}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
49
56
|
desc "Add records to index. Deploys a new index by default, or specify INDEX"
|
50
57
|
task build: :environment do
|
51
58
|
ElasticRecord::Task.get_models.each do |model|
|
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.6.
|
4
|
+
version: 0.6.9
|
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-
|
12
|
+
date: 2012-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: arelastic
|