elastic_record 0.6.8 → 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -19,25 +19,102 @@ Include ElasticRecord into your model:
19
19
  include ElasticRecord::Model
20
20
  end
21
21
 
22
- == Querying:
22
+ == Searching
23
23
 
24
- scope = Product.elastic_search
25
-
26
- scope.filter(color: 'red')
27
- scope.order(:price)
28
- scope.count
29
- scope.first
30
- scope.all
31
- scope.each do |product|
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 are executed within scopes:
111
+ Class methods can be executed within scopes:
36
112
 
37
- class Pirate
38
- def self.ouput_to_screen
39
- all.each do { |widget| puts widget }
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
- scope.output_to_screen
119
+ # Increase the price of all red products by $10.
120
+ Product.filter(color: 'red').increase_prices
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '0.6.8'
5
+ s.version = '0.6.9'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -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 "/#{alias_name}/_refresh"
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.8
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-24 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic