rom-elasticsearch 0.1.1 → 0.2.0

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
- SHA1:
3
- metadata.gz: 952c1f408e7aacb129e6cd717870d5cc868d4e9a
4
- data.tar.gz: 5455ddde25816f89151a439864c660c88672da5c
2
+ SHA256:
3
+ metadata.gz: 4e74b8fa3035dc7923a8d9b9ae9366c59ccf412934bc7a4ff11b4fbc76ec7ac3
4
+ data.tar.gz: 3850528b01efb065373bcd31c6d089c11e85872f30dc6c3c02d439dbf81a529f
5
5
  SHA512:
6
- metadata.gz: a6fb4a3af4baf016bec6fa0cbf7d817408f3dbe27a510dc1ec5c3a97e3a207398383d873b9af0a3798acfdd81595ae80035e22a68034088e9ee80f9cd272fbfb
7
- data.tar.gz: 2e830ef47d27a707e85d8340d7f5933b1d18e3ea8911742b9eedd554721b799a63ec4137937d36eec2cdd687e2e6c66beb84e66c5e3f9fa4d3179a00b962dc71
6
+ metadata.gz: fde4c8952f143d7778b377d3b1ac9168ff700b4e0fb184e6a6ee4c391c6918ce6c75efcd3162990ba580bb1ba4bdc86b964b1d4b1f45e5f2d283b8fc6ba73f9a
7
+ data.tar.gz: 23e0871170e5c98b38a4f926c5889094ce89d888e1b87663a122e451870cebd2958d694868ffe79a10fd6eef334fbf5b9915b0f17cf90d11e0a2944da4e88007
data/.codeclimate.yml ADDED
@@ -0,0 +1,6 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+ ratings:
5
+ paths:
6
+ - lib/**
data/.travis.yml CHANGED
@@ -1,19 +1,22 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  cache: bundler
4
+ before_install: gem update --system
4
5
  services:
5
6
  - elasticsearch
6
7
  bundler_args: --without yard guard benchmarks tools
7
8
  before_script:
8
9
  - curl -XPUT http://localhost:9200/rom-test
10
+ after_success:
11
+ - '[ -d coverage ] && bundle exec codeclimate-test-reporter'
9
12
  script: "bundle exec rake ci"
10
13
  rvm:
11
- - 2.3.4
12
- - 2.4.1
14
+ - 2.5.0
15
+ - 2.4.3
16
+ - 2.3.6
13
17
  - jruby-9.1.12.0
14
18
  env:
15
19
  global:
16
- - JRUBY_OPTS='--dev -J-Xmx1024M'
17
20
  - COVERAGE='true'
18
21
  notifications:
19
22
  webhooks:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## v0.2.0 2018-01-23
2
+
3
+ ## Added
4
+
5
+ * `Relation#order` which sets `:sort` (solnic)
6
+ * `Relation#page` which sets `:from` offset (solnic)
7
+ * `Relation#per_page` which sets `:size` (solnic)
8
+ * `Relation#call` returns custom `ROM::Elasticsearch::Relation::Loaded` object, which provides access to `#total_hits` and raw client response (solnic)
9
+
10
+ [Compare v0.1.1...v0.2.0](https://github.com/rom-rb/rom/compare/v0.1.1...v0.2.0)
11
+
1
12
  ## v0.1.1 2017-11-18
2
13
 
3
14
  ## Changed
data/Gemfile CHANGED
@@ -11,8 +11,7 @@ end
11
11
  gem 'codeclimate-test-reporter', require: false
12
12
  gem 'simplecov', require: false
13
13
 
14
- gem 'pry-byebug', platform: :mri
15
- gem 'pry', platform: :jruby
14
+ gem 'byebug', platform: :mri
16
15
  gem 'elasticsearch-dsl'
17
16
 
18
17
  group :tools do
data/README.md CHANGED
@@ -9,8 +9,8 @@
9
9
  [![Gem Version](https://badge.fury.io/rb/rom-elasticsearch.svg)][gem]
10
10
  [![Build Status](https://travis-ci.org/rom-rb/rom-elasticsearch.svg?branch=master)][travis]
11
11
  [![Dependency Status](https://gemnasium.com/rom-rb/rom-elasticsearch.svg)][gemnasium]
12
- [![Code Climate](https://codeclimate.com/github/rom-rb/rom-elasticsearch/badges/gpa.svg)][codeclimate]
13
- [![Test Coverage](https://codeclimate.com/github/rom-rb/rom-elasticsearch/badges/coverage.svg)][codeclimate]
12
+ [![Maintainability](https://api.codeclimate.com/v1/badges/dc6780aac2d99766b141/maintainability)](https://codeclimate.com/github/rom-rb/rom-elasticsearch/maintainability)
13
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/dc6780aac2d99766b141/test_coverage)](https://codeclimate.com/github/rom-rb/rom-elasticsearch/test_coverage)
14
14
  [![Inline docs](http://inch-ci.org/github/rom-rb/rom-elasticsearch.svg?branch=master)][inchpages]
15
15
 
16
16
  ElasticSearch support for [rom-rb](https://github.com/rom-rb/rom).
@@ -7,6 +7,7 @@ module ROM
7
7
  # @api public
8
8
  class Attribute < ROM::Attribute
9
9
  INTERNAL_META_KEYS = %i[name source primary_key].freeze
10
+ DEFAULT_SORT_DIRECTION = 'asc'.freeze
10
11
 
11
12
  # Return ES mapping properties
12
13
  #
@@ -25,6 +26,34 @@ module ROM
25
26
  def properties?
26
27
  properties.size > 0
27
28
  end
29
+
30
+ # Return attribute with direction set to ascending
31
+ #
32
+ # @return [Attribute]
33
+ #
34
+ # @api public
35
+ def asc
36
+ meta(direction: 'asc')
37
+ end
38
+
39
+ # Return attribute with direction set to descending
40
+ #
41
+ # @return [Attribute]
42
+ #
43
+ # @api public
44
+ def desc
45
+ meta(direction: 'desc')
46
+ end
47
+
48
+ # @api private
49
+ memoize def to_sort_expr
50
+ "#{name}:#{direction}"
51
+ end
52
+
53
+ # @api private
54
+ memoize def direction
55
+ meta[:direction] || DEFAULT_SORT_DIRECTION
56
+ end
28
57
  end
29
58
  end
30
59
  end
@@ -20,6 +20,9 @@ module ROM
20
20
 
21
21
  include QueryMethods
22
22
 
23
+ # Sort values separator
24
+ SORT_VALUES_SEPARATOR = ','.freeze
25
+
23
26
  # Default query options
24
27
  ALL = { query: { match_all: EMPTY_HASH } }.freeze
25
28
 
@@ -38,6 +41,10 @@ module ROM
38
41
  # @return [Hash] default body
39
42
  option :body, default: -> { EMPTY_HASH }
40
43
 
44
+ # @!attribute [r] response
45
+ # @return [Hash] memoized response from the client
46
+ option :response, optional: true, reader: false
47
+
41
48
  # Put new data under configured index
42
49
  #
43
50
  # @param [Hash] data
@@ -178,6 +185,37 @@ module ROM
178
185
  self
179
186
  end
180
187
 
188
+ # Return dataset with :sort set
189
+ #
190
+ # @return [Dataset]
191
+ #
192
+ # @api public
193
+ def sort(*fields)
194
+ params(sort: fields.join(SORT_VALUES_SEPARATOR))
195
+ end
196
+
197
+ # Return dataset with :from set
198
+ #
199
+ # @param [Integer] num
200
+ #
201
+ # @return [Dataset]
202
+ #
203
+ # @api public
204
+ def from(num)
205
+ params(from: num)
206
+ end
207
+
208
+ # Return dataset with :size set
209
+ #
210
+ # @param [Integer] num
211
+ #
212
+ # @return [Dataset]
213
+ #
214
+ # @api public
215
+ def size(num)
216
+ params(size: num)
217
+ end
218
+
181
219
  # Create an index
182
220
  #
183
221
  # @param [Hash] opts ES options
@@ -200,6 +238,15 @@ module ROM
200
238
  client.indices.delete(params.merge(opts))
201
239
  end
202
240
 
241
+ # Return a dataset with pre-set client response
242
+ #
243
+ # @return [Dataset]
244
+ #
245
+ # @api public
246
+ def call
247
+ with(response: response)
248
+ end
249
+
203
250
  private
204
251
 
205
252
  # Return results of a query based on configured params and body
@@ -211,9 +258,14 @@ module ROM
211
258
  if params[:id]
212
259
  [client.get(params)]
213
260
  else
214
- client.search(**params, body: body).fetch('hits').fetch('hits')
261
+ response.fetch('hits').fetch('hits')
215
262
  end
216
263
  end
264
+
265
+ # @api private
266
+ def response
267
+ options[:response] || client.search(**params, body: body)
268
+ end
217
269
  end
218
270
  end
219
271
  end
@@ -1,4 +1,5 @@
1
1
  require 'rom/relation'
2
+ require 'rom/elasticsearch/relation/loaded'
2
3
  require 'rom/elasticsearch/types'
3
4
  require 'rom/elasticsearch/schema'
4
5
  require 'rom/elasticsearch/attribute'
@@ -96,6 +97,14 @@ module ROM
96
97
  # whereas in core, it is only used when there's at least one read-type
97
98
  option :output_schema, default: -> { schema.to_output_hash }
98
99
 
100
+ # @attribute [r] current_page
101
+ # @return [Integer] Currently set page
102
+ option :current_page, default: -> { 1 }
103
+
104
+ # @attribute [r] per_page
105
+ # @return [Integer] Number of results per page
106
+ option :per_page, reader: false, optional: true, default: -> { 10 }
107
+
99
108
  # Default index settings that can be overridden
100
109
  index_settings(
101
110
  { number_of_shards: 1,
@@ -111,6 +120,52 @@ module ROM
111
120
  } }.freeze
112
121
  )
113
122
 
123
+ # Load a relation
124
+ #
125
+ # @return [Loaded]
126
+ #
127
+ # @api public
128
+ def call
129
+ Loaded.new(new(dataset.call))
130
+ end
131
+
132
+ # Return a relation with changed sorting logic
133
+ #
134
+ # @param [Array<Symbol,Attribute>] attrs
135
+ #
136
+ # @return [Relation]
137
+ #
138
+ # @api public
139
+ def order(*attrs)
140
+ new(dataset.sort(*schema.project(*attrs).map(&:to_sort_expr)))
141
+ end
142
+
143
+ # Return a relation with page number set
144
+ #
145
+ # @param [Integer] num
146
+ #
147
+ # @return [Relation]
148
+ #
149
+ # @api public
150
+ def page(num)
151
+ new(dataset.from((num - 1) * per_page), current_page: num)
152
+ end
153
+
154
+ # Return a relation with per-page number set
155
+ #
156
+ # @param [Integer] num
157
+ #
158
+ # @return [Relation]
159
+ #
160
+ # @api public
161
+ def per_page(num = Undefined)
162
+ if num.equal?(Undefined)
163
+ options[:per_page]
164
+ else
165
+ new(dataset.size(num), per_page: num)
166
+ end
167
+ end
168
+
114
169
  # Map indexed data
115
170
  #
116
171
  # @yieldparam [Hash,ROM::Struct]
@@ -0,0 +1,28 @@
1
+ module ROM
2
+ module Elasticsearch
3
+ class Relation < ROM::Relation
4
+ # Materialized index data
5
+ #
6
+ # @api public
7
+ class Loaded < ROM::Relation::Loaded
8
+ # Return total number of hits
9
+ #
10
+ # @return [Integer]
11
+ #
12
+ # @api public
13
+ def total_hits
14
+ response['hits']['total']
15
+ end
16
+
17
+ # Return raw response from the ES client
18
+ #
19
+ # @return [Hash]
20
+ #
21
+ # @api public
22
+ def response
23
+ source.dataset.options[:response]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module Elasticsearch
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -15,9 +15,8 @@ if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
15
15
  end
16
16
 
17
17
  begin
18
- require 'pry-byebug'
18
+ require 'byebug'
19
19
  rescue LoadError
20
- require 'pry'
21
20
  end
22
21
 
23
22
  require 'rom-elasticsearch'
@@ -0,0 +1,22 @@
1
+ require 'rom/elasticsearch/relation'
2
+
3
+ RSpec.describe ROM::Elasticsearch::Relation, '#call' do
4
+ subject(:relation) { relations[:users] }
5
+
6
+ include_context 'users'
7
+
8
+ before do
9
+ relation.command(:create).(id: 1, name: 'Jane')
10
+ relation.command(:create).(id: 2, name: 'John')
11
+
12
+ relation.refresh
13
+ end
14
+
15
+ it 'returns loaded relation' do
16
+ result = relation.call
17
+
18
+ expect(result).to match_array([{ id: 1, name: 'Jane' }, { id: 2, name: 'John' }])
19
+
20
+ expect(result.total_hits).to be(2)
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ require 'rom/elasticsearch/relation'
2
+
3
+ RSpec.describe ROM::Elasticsearch::Relation, '#order' do
4
+ subject(:relation) { relations[:users] }
5
+
6
+ include_context 'users'
7
+
8
+ before do
9
+ relation.command(:create).(id: 1, name: 'John')
10
+ relation.command(:create).(id: 2, name: 'Jane')
11
+ relation.command(:create).(id: 3, name: 'Jade')
12
+ relation.command(:create).(id: 4, name: 'Joe')
13
+
14
+ relation.refresh
15
+ end
16
+
17
+ it 'with ascending direction' do
18
+ result = relation.order(:id).to_a
19
+
20
+ expect(result).
21
+ to eql([
22
+ { id: 1, name: 'John' },
23
+ { id: 2, name: 'Jane' },
24
+ { id: 3, name: 'Jade' },
25
+ { id: 4, name: 'Joe' }
26
+ ])
27
+ end
28
+
29
+ it 'with descending direction' do
30
+ result = relation.order(relation[:id].desc).to_a
31
+
32
+ expect(result).
33
+ to eql([
34
+ { id: 4, name: 'Joe' },
35
+ { id: 3, name: 'Jade' },
36
+ { id: 2, name: 'Jane' },
37
+ { id: 1, name: 'John' }
38
+ ])
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ require 'rom/elasticsearch/relation'
2
+
3
+ RSpec.describe ROM::Elasticsearch::Relation, '#page' do
4
+ subject(:relation) { relations[:users].order(:id) }
5
+
6
+ include_context 'users'
7
+
8
+ before do
9
+ relation.command(:create).(id: 1, name: 'Jane')
10
+ relation.command(:create).(id: 2, name: 'John')
11
+
12
+ relation.refresh
13
+ end
14
+
15
+ it 'returns relation with page set' do
16
+ result = relation.per_page(1).page(2).to_a
17
+
18
+ expect(result).to match_array([{ id: 2, name: 'John' }])
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-elasticsearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hannes Nevalainen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-18 00:00:00.000000000 Z
12
+ date: 2018-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rom-core
@@ -89,6 +89,7 @@ executables: []
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
+ - ".codeclimate.yml"
92
93
  - ".gitignore"
93
94
  - ".rspec"
94
95
  - ".travis.yml"
@@ -109,6 +110,7 @@ files:
109
110
  - lib/rom/elasticsearch/plugins/relation/query_dsl.rb
110
111
  - lib/rom/elasticsearch/query_methods.rb
111
112
  - lib/rom/elasticsearch/relation.rb
113
+ - lib/rom/elasticsearch/relation/loaded.rb
112
114
  - lib/rom/elasticsearch/schema.rb
113
115
  - lib/rom/elasticsearch/types.rb
114
116
  - lib/rom/elasticsearch/version.rb
@@ -126,11 +128,14 @@ files:
126
128
  - spec/unit/rom/elasticsearch/dataset/search_spec.rb
127
129
  - spec/unit/rom/elasticsearch/gateway_spec.rb
128
130
  - spec/unit/rom/elasticsearch/plugins/relation/query_dsl_spec.rb
131
+ - spec/unit/rom/elasticsearch/relation/call_spec.rb
129
132
  - spec/unit/rom/elasticsearch/relation/create_index_spec.rb
130
133
  - spec/unit/rom/elasticsearch/relation/dataset_spec.rb
131
134
  - spec/unit/rom/elasticsearch/relation/delete_spec.rb
132
135
  - spec/unit/rom/elasticsearch/relation/get_spec.rb
133
136
  - spec/unit/rom/elasticsearch/relation/map_spec.rb
137
+ - spec/unit/rom/elasticsearch/relation/order_spec.rb
138
+ - spec/unit/rom/elasticsearch/relation/page_spec.rb
134
139
  - spec/unit/rom/elasticsearch/relation/pluck_spec.rb
135
140
  - spec/unit/rom/elasticsearch/relation/query_spec.rb
136
141
  - spec/unit/rom/elasticsearch/relation/query_string_spec.rb
@@ -156,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
161
  version: '0'
157
162
  requirements: []
158
163
  rubyforge_project:
159
- rubygems_version: 2.6.11
164
+ rubygems_version: 2.7.4
160
165
  signing_key:
161
166
  specification_version: 4
162
167
  summary: ROM adapter for Elasticsearch
@@ -174,11 +179,14 @@ test_files:
174
179
  - spec/unit/rom/elasticsearch/dataset/search_spec.rb
175
180
  - spec/unit/rom/elasticsearch/gateway_spec.rb
176
181
  - spec/unit/rom/elasticsearch/plugins/relation/query_dsl_spec.rb
182
+ - spec/unit/rom/elasticsearch/relation/call_spec.rb
177
183
  - spec/unit/rom/elasticsearch/relation/create_index_spec.rb
178
184
  - spec/unit/rom/elasticsearch/relation/dataset_spec.rb
179
185
  - spec/unit/rom/elasticsearch/relation/delete_spec.rb
180
186
  - spec/unit/rom/elasticsearch/relation/get_spec.rb
181
187
  - spec/unit/rom/elasticsearch/relation/map_spec.rb
188
+ - spec/unit/rom/elasticsearch/relation/order_spec.rb
189
+ - spec/unit/rom/elasticsearch/relation/page_spec.rb
182
190
  - spec/unit/rom/elasticsearch/relation/pluck_spec.rb
183
191
  - spec/unit/rom/elasticsearch/relation/query_spec.rb
184
192
  - spec/unit/rom/elasticsearch/relation/query_string_spec.rb