elastic-rails 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ccf947047bcebb5042dddebb851e017da2f9a5b
4
- data.tar.gz: 459d2dd8391984dc5824cd1e7b5465c5d950b615
3
+ metadata.gz: a12b5aa58fd03c89b2da12c996b19994bb3a3c1a
4
+ data.tar.gz: d1e4d3dee93fd0d9fd92c700b4367b415225a142
5
5
  SHA512:
6
- metadata.gz: 9faed007e581dfb072a6f39c789e04911c026e2bcaa77bf3024e57407f6100eed2d9302bf46171cd5bb17d09b6a9d8ab87b224fec7ca77dab5bbc4d30b8d1ea5
7
- data.tar.gz: 9bcd9d07542c303efa0df810924f5629a43ba217326a2fd4600d0a0eb88bf07f3328d3f9cd097093662f99dc54324e354c7c0aa4c828a0d2ec69b1a80e49990b
6
+ metadata.gz: cf802900bc06db2cf8c4457c2839580d255c61fbefe4a616e8323699708ee20231be44220eb5149996501b88d5d0675cbecfccae050f8293e68821a0c5258b60
7
+ data.tar.gz: 3879b379633548e1d7d7d18b1500dd5e8e73feaf31858c892e056a1c38a03aaabcb150c37bba2534df28795f03044d2a987086040228c5f9890369ccb9f96607
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
- # Elastic
1
+ # Elastic Rails
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elastic`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Elasticsearch + Ruby on Rails made easy.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Features
6
+
7
+ * Easy setup
8
+ * Chainable query DSL
9
+ * Easy to use results
10
+ * Seamless rails integration
11
+ * Multiple enviroment support
12
+ * Zero downtime index migrations
6
13
 
7
14
  ## Installation
8
15
 
@@ -20,25 +27,45 @@ Or install it yourself as:
20
27
 
21
28
  $ gem install elastic-rails
22
29
 
23
- Finally execute the initialization script:
30
+ Finally execute the initialization script to generate configuration files:
24
31
 
25
32
  $ rails g es:init
26
33
 
27
- ## Usage
34
+ ## Usage overview
35
+
36
+ [For detailed usage reference check out the GUIDE]
28
37
 
29
- Start by creating an index over a model (You can also index non-activerecord objects)
38
+ Suppose that you already have a model called `Bike`, start by creating an index for it:
30
39
 
31
40
  rails g es:index Bike
32
41
 
33
- This will generate a new index definition file in `app/indices`
42
+ This will generate a new index definition file in `app/indices/bike_index.rb`
34
43
 
35
- Add some fields to the index
44
+ Add some fields to the index:
36
45
 
37
- TODO
46
+ ```ruby
47
+ class BikeIndex < ElasticType
48
+ # field types are extracted from the target model
49
+ fields :brand_id, :model, :year, :price
50
+
51
+ # you can also explicitly set the field type
52
+ field :category, type: :term
53
+ field :description, type: :string
54
+ field :created_at, type: :time
55
+
56
+ # you can also have nested documents, the following will require a nested PartIndex to be defined.
57
+ nested :parts
58
+
59
+ # you can override fields or create new ones
60
+ def year
61
+ object.batch.year
62
+ end
63
+ end
64
+ ```
38
65
 
39
66
  Every time you create or change and index you will neeed to synchronize the Elasticsearch index mappings:
40
67
 
41
- rake es:migrate
68
+ rake es:remap
42
69
 
43
70
  If you already have some data that needs to be indexed then run the reindex task:
44
71
 
@@ -52,35 +79,63 @@ some_bike.index_later # this will queue a reindexing job on the record
52
79
  BikeIndex.import([bike_1, bike_2, bike_3]) # this will perform a bulk insertion
53
80
  ```
54
81
 
55
- After some data has been added you can start searching:
82
+ You can also setup automatic indexation/unindexation for a given model:
56
83
 
57
84
  ```ruby
58
- BikeIndex.must(brand: 'Trek', size: 'M').should(year: { gte: 2015 }).avg(:price)
59
-
60
- BikeIndex.must(origin: 'China').segment(:brand).each { |brand, bikes| }
85
+ class Bike < ActiveRecord::Base
86
+ index on: :save
87
+ end
61
88
  ```
62
89
 
63
- migrate: remaps if necessary
64
-
65
- reindex: attempts to rotate if index already exist
66
-
67
- TODO: Write usage instructions here
68
-
69
- ## Missing Features
70
-
71
- These are some features that will be added in the future:
90
+ After some data has been added you can start quering:
72
91
 
92
+ ```ruby
93
+ # List bikes of brand Trek or Cannondale, preferably 2015 or later models:
94
+ BikeIndex
95
+ .must(brand: ['Trek', 'Cannondale'])
96
+ .should(year: { gte: 2015 })
97
+ .to_a
98
+
99
+ # List bikes of brand Trek, preferably 2015 or 2016, give higher score to 2016 models:
100
+ BikeIndex
101
+ .must(brand: ['Trek', 'Cannondale'])
102
+ .should(year: 2015)
103
+ .boost(2.0) { should(year: 2016) }
104
+ .to_a
105
+
106
+ # More score manipulation:
107
+ BikeIndex
108
+ .coord_similarity(false) # disable coord similarity (no score normalization)
109
+ .boost(0.0) { must(brand: ['Trek', 'Cannondale']) } # no score
110
+ .boost(fixed: 1.0) { should(year: 2015) } # fixed score
111
+ .boost(fixed: 2.0) { should(year: 2016) }
112
+ .each_with_score { |bike, score| puts "#{bike.name} got score: #{score}" }
113
+
114
+ # Get average bike price by year and category, for bikes newer than 2014
115
+ BikeIndex
116
+ .must(year: { gte: 2014 })
117
+ .segment(:year)
118
+ .segment(:category)
119
+ .average(:price)
120
+ .each { |keys, price| puts "#{keys[:year]}/#{keys[:category]} => #{price}" }
121
+
122
+ # Search bikes ids that have shimano parts:
123
+ BikeIndex.must(parts: { brand: 'shimano' }).ids
124
+ ```
73
125
 
74
- ## Development
126
+ ## Missing features
75
127
 
76
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
128
+ The following are some features that we plan to implement in the future:
77
129
 
78
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
130
+ * Highlighting support
131
+ * Suggesters support
132
+ * Geo fields and queries support
133
+ * Custom analizers support
134
+ * More queries types support (multi-match, common-terms, wildcard, fuzzy, etc)
79
135
 
80
136
  ## Contributing
81
137
 
82
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/elastic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
-
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/platanus/elastic-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
84
139
 
85
140
  ## License
86
141
 
@@ -9,11 +9,12 @@ module Elastic
9
9
  whiny_indices: false,
10
10
  api_client: nil, # set by method
11
11
  logger: nil, # set by method
12
- time_zone: nil # set by method
12
+ time_zone: nil, # set by method
13
+ disable_indexing: false
13
14
  }
14
15
 
15
16
  attr_accessor :host, :port, :api_client, :index, :page_size, :coord_similarity, :logger,
16
- :import_batch_size, :whiny_indices, :time_zone
17
+ :import_batch_size, :whiny_indices, :time_zone, :disable_indexing
17
18
 
18
19
  def initialize
19
20
  assign_attributes DEFAULTS
@@ -12,6 +12,8 @@ module Elastic::Core
12
12
  end
13
13
 
14
14
  def status
15
+ return :ready if Elastic.config.disable_indexing
16
+
15
17
  actual_name = resolve_actual_index_name
16
18
  return :not_available if actual_name.nil?
17
19
  return :not_synchronized unless mapping_synchronized? actual_name
@@ -49,6 +51,8 @@ module Elastic::Core
49
51
  end
50
52
 
51
53
  def index(_document)
54
+ return if Elastic.config.disable_indexing
55
+
52
56
  # TODO: validate document type
53
57
  operations = write_indices.map do |write_index|
54
58
  { 'index' => _document.merge('_index' => write_index) }
@@ -58,6 +62,8 @@ module Elastic::Core
58
62
  end
59
63
 
60
64
  def bulk_index(_documents)
65
+ return if Elastic.config.disable_indexing
66
+
61
67
  # TODO: validate documents type
62
68
  body = _documents.map { |doc| { 'index' => doc } }
63
69
 
@@ -72,6 +78,8 @@ module Elastic::Core
72
78
  raise ArgumentError, 'document must provide an id' unless _document['_id']
73
79
  raise ArgumentError, 'document must provide a type' unless _document['_type']
74
80
 
81
+ return if Elastic.config.disable_indexing
82
+
75
83
  write_index, rolling_index = write_indices
76
84
 
77
85
  operations = [{ 'delete' => _document.merge('_index' => write_index) }]
@@ -1,6 +1,11 @@
1
1
  RSpec.configure do |config|
2
- config.before(:example, elasticsearch: true) do
3
- Elastic.drop
4
- Elastic.migrate
2
+ config.before(:example) do |example|
3
+ if example.metadata[:elasticsearch]
4
+ Elastic.drop
5
+ Elastic.migrate
6
+ Elastic.config.disable_indexing = false
7
+ else
8
+ Elastic.config.disable_indexing = true
9
+ end
5
10
  end
6
11
  end
@@ -1,3 +1,3 @@
1
1
  module Elastic
2
- VERSION = "0.8.1"
2
+ VERSION = "0.8.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-31 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch