elasticsearch-dsl-builder 0.0.16 → 0.0.17

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 418d5192343837465ab9d7a60484aacc9eb9f501
4
- data.tar.gz: e0627ac86a4f19429291920b72002b9d282ecb8f
3
+ metadata.gz: 4e9f6de3a22231c91619745c3e9b547bb4719aaa
4
+ data.tar.gz: b95ba53e9bc8e28c4ad78b1103be51163bc87216
5
5
  SHA512:
6
- metadata.gz: d44a4bb6bb06cc966024627bf37715a96f01b706732d8ed00a5601f8e1ac13b5a5a91b0a0daf0680dd087e0792febbec00a6a7e0a6d8ce11b6afbd6dbf9a87cb
7
- data.tar.gz: b2b3566b4d78722c80029f2da00a28510f1e2eee02c702180eb7cc82a693fa07215aa1102071d5604a89f9dd47ebe6a939f117007822b32202e69a7dc83dd059
6
+ metadata.gz: 8ca5470e4df4234de616674ed79f28f07334c777663a8e059e77240d8a88169b8c6fbd6af848aadc77b0a4a24fe89e87c6c193d23b3c2dce3da4bc718e8dbda2
7
+ data.tar.gz: c05e3cc6dd26eb397ea0750c87109b93367901633e2883f473d68134bee7b092c1d20c6d70f03b2d172ee9d97299148512cdd7d690f890a67fe6225a3d5ea2ef
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elasticsearch-dsl-builder (0.0.16)
4
+ elasticsearch-dsl-builder (0.0.17)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'elasticsearch-dsl-builder'
3
- s.version = '0.0.16'
3
+ s.version = '0.0.17'
4
4
  s.date = '2017-10-06'
5
5
  s.summary = 'Library utilizing builder pattern providing Ruby API for the Elasticsearch Query DSL'
6
6
  s.description = 'TBD'
@@ -2,6 +2,7 @@ require 'elasticsearch_dsl_builder/dsl/version'
2
2
 
3
3
  require 'elasticsearch_dsl_builder/dsl/search/aggregation'
4
4
  require 'elasticsearch_dsl_builder/dsl/search/query'
5
+ require 'elasticsearch_dsl_builder/dsl/search/inner_hits'
5
6
  require 'elasticsearch_dsl_builder/dsl/search/script'
6
7
  require 'elasticsearch_dsl_builder/dsl/search/sort'
7
8
  require 'elasticsearch_dsl_builder/exceptions'
@@ -0,0 +1,57 @@
1
+ module ElasticsearchDslBuilder
2
+ module DSL
3
+ module Search
4
+ class InnerHits
5
+ def size(value)
6
+ raise ArgumentError, 'value must be Numeric' unless value.is_a? Numeric
7
+ @size = value
8
+ self
9
+ end
10
+
11
+ def from(value)
12
+ raise ArgumentError, 'value must be Numeric' unless value.is_a? Numeric
13
+ @from = value
14
+ self
15
+ end
16
+
17
+ def sort_by(name, direction = nil)
18
+ @sort ||= Sort.new
19
+ @sort.by(name, direction)
20
+ self
21
+ end
22
+
23
+ def include_fields(*fields)
24
+ fields = fields.flatten
25
+ fields_valid = !fields.empty? && fields.all? { |f| f.instance_of?(String) }
26
+ raise ArgumentError, 'must pass at least 1 String field' unless fields_valid
27
+ @included_fields ||= []
28
+ @included_fields.concat fields
29
+ self
30
+ end
31
+
32
+ def exclude_fields(*fields)
33
+ fields = fields.flatten
34
+ fields_valid = !fields.empty? && fields.all? { |f| f.instance_of?(String) }
35
+ raise ArgumentError, 'must pass at least 1 String field' unless fields_valid
36
+ @excluded_fields ||= []
37
+ @excluded_fields.concat fields
38
+ self
39
+ end
40
+
41
+ def to_hash
42
+ hash = {}
43
+ hash.update(sort: @sort.to_hash) if @sort
44
+ hash.update(size: @size) if @size
45
+ hash.update(from: @from) if @from
46
+ unless @included_fields.to_a.empty? && @excluded_fields.to_a.empty?
47
+ source = {}
48
+ source.update(includes: @included_fields) unless @included_fields.to_a.empty?
49
+ source.update(excludes: @excluded_fields) unless @excluded_fields.to_a.empty?
50
+ hash.update(_source: source)
51
+ end
52
+ hash
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -23,9 +23,16 @@ module ElasticsearchDslBuilder
23
23
  self
24
24
  end
25
25
 
26
+ def inner_hits(inner_hits)
27
+ raise ArgumentError, 'inner_hits must be an InnerHits object' unless inner_hits.instance_of?(InnerHits)
28
+ @inner_hits = inner_hits.to_hash
29
+ self
30
+ end
31
+
26
32
  def to_hash
27
33
  @query = { type: @child_type }
28
34
  @query.update(query: @nested_query) if @nested_query
35
+ @query.update(inner_hits: @inner_hits) if @inner_hits
29
36
  super
30
37
  end
31
38
  end
@@ -30,11 +30,18 @@ module ElasticsearchDslBuilder
30
30
  self
31
31
  end
32
32
 
33
+ def inner_hits(inner_hits)
34
+ raise ArgumentError, 'inner_hits must be an InnerHits object' unless inner_hits.instance_of?(InnerHits)
35
+ @inner_hits = inner_hits.to_hash
36
+ self
37
+ end
38
+
33
39
  def to_hash
34
40
  @query = {}
35
41
  @query.update(parent_type: @parent_type) if @parent_type
36
42
  @query.update(score: @score) if @score
37
43
  @query.update(query: @nested_query) if @nested_query
44
+ @query.update(inner_hits: @inner_hits) if @inner_hits
38
45
  super
39
46
  end
40
47
  end
@@ -22,9 +22,16 @@ module ElasticsearchDslBuilder
22
22
  self
23
23
  end
24
24
 
25
+ def inner_hits(inner_hits)
26
+ raise ArgumentError, 'inner_hits must be an InnerHits object' unless inner_hits.instance_of?(InnerHits)
27
+ @inner_hits = inner_hits.to_hash
28
+ self
29
+ end
30
+
25
31
  def to_hash
26
32
  @query = { path: @path }
27
33
  @query.update(query: @nested_query) if @nested_query
34
+ @query.update(inner_hits: @inner_hits) if @inner_hits
28
35
  super
29
36
  end
30
37
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElasticsearchDslBuilder::DSL::Search::InnerHits do
4
+ it 'should raise error if sort_by not valid' do
5
+ expect { InnerHits.new.sort_by(123, 'asc') }.to raise_error(ArgumentError)
6
+ expect { InnerHits.new.sort_by('valid', 'not_asc_or_desc') }.to raise_error(ArgumentError)
7
+ expect { InnerHits.new.sort_by('valid', 123) }.to raise_error(ArgumentError)
8
+ end
9
+
10
+ it 'should raise error if size not Numeric' do
11
+ expect { InnerHits.new.size('invalid') }.to raise_error(ArgumentError)
12
+ end
13
+
14
+ it 'should raise error if from not Numeric' do
15
+ expect { InnerHits.new.from('invalid') }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ it 'should raise error if include_fields invalid' do
19
+ expect { InnerHits.new.include_fields }.to raise_error(ArgumentError)
20
+ expect { InnerHits.new.include_fields(123) }.to raise_error(ArgumentError)
21
+ expect { InnerHits.new.include_fields('valid', 123) }.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it 'should raise error if exclude_fields invalid' do
25
+ expect { InnerHits.new.exclude_fields }.to raise_error(ArgumentError)
26
+ expect { InnerHits.new.exclude_fields(123) }.to raise_error(ArgumentError)
27
+ expect { InnerHits.new.exclude_fields('valid', 123) }.to raise_error(ArgumentError)
28
+ end
29
+
30
+ it 'should chain build an ES search' do
31
+ search = InnerHits.new.
32
+ sort_by('posted_at', 'desc').
33
+ size(5).from(2).
34
+ include_fields('id', 'content').
35
+ exclude_fields('no_show')
36
+
37
+ expect(search.to_hash).to eq(
38
+ sort: [
39
+ { 'posted_at' => 'desc' }
40
+ ],
41
+ size: 5,
42
+ from: 2,
43
+ _source: {
44
+ includes: ['id', 'content'],
45
+ excludes: ['no_show']
46
+ }
47
+ )
48
+ end
49
+ end
@@ -11,8 +11,31 @@ describe ElasticsearchDslBuilder::DSL::Search::Queries::HasChild do
11
11
  expect { Queries::HasChild.new('child').query({}) }.to raise_error(ArgumentError)
12
12
  end
13
13
 
14
+ it 'should fail if inner_hits argument not a InnerHits' do
15
+ expect { Queries::HasChild.new('child').inner_hits('invalid') }.to raise_error(ArgumentError)
16
+ expect { Queries::HasChild.new('child').inner_hits({}) }.to raise_error(ArgumentError)
17
+ end
18
+
14
19
  it 'should chain create valid ES query hash' do
15
- has_child = Queries::HasChild.new('child').query(Queries::Term.new(:field_a, 'value'))
16
- expect(has_child.to_hash).to eq(has_child: { type: 'child', query: { term: { field_a: 'value' } } })
20
+ has_child = Queries::HasChild.new('child').
21
+ query(Queries::Term.new(:field_a, 'value')).
22
+ inner_hits(
23
+ InnerHits.new.size(1).from(0).sort_by('posted_at', 'asc').
24
+ include_fields('id', 'content').
25
+ exclude_fields('no_show')
26
+ )
27
+
28
+ expect(has_child.to_hash).to eq(
29
+ has_child: {
30
+ type: 'child', query: { term: { field_a: 'value' } },
31
+ inner_hits: {
32
+ sort: [{ 'posted_at' => 'asc' }], size: 1, from: 0,
33
+ _source: {
34
+ includes: ['id', 'content'],
35
+ excludes: ['no_show']
36
+ }
37
+ }
38
+ }
39
+ )
17
40
  end
18
41
  end
@@ -16,8 +16,33 @@ describe ElasticsearchDslBuilder::DSL::Search::Queries::HasParent do
16
16
  expect { Queries::HasParent.new('parent').query({}) }.to raise_error(ArgumentError)
17
17
  end
18
18
 
19
+ it 'should fail if inner_hits argument not a InnerHits' do
20
+ expect { Queries::HasParent.new('parent').inner_hits('invalid') }.to raise_error(ArgumentError)
21
+ expect { Queries::HasParent.new('parent').inner_hits({}) }.to raise_error(ArgumentError)
22
+ end
23
+
19
24
  it 'should chain create valid ES query hash' do
20
- has_parent = Queries::HasParent.new('parent').score(true).query(Queries::Term.new(:field_a, 'value'))
21
- expect(has_parent.to_hash).to eq(has_parent: { parent_type: 'parent', score: true, query: { term: { field_a: 'value' } } })
25
+ has_parent = Queries::HasParent.new('parent').
26
+ score(true).
27
+ query(Queries::Term.new(:field_a, 'value')).
28
+ inner_hits(
29
+ InnerHits.new.size(1).from(0).sort_by('posted_at', 'asc').
30
+ include_fields('id', 'content').
31
+ exclude_fields('no_show')
32
+ )
33
+ expect(has_parent.to_hash).to eq(
34
+ has_parent: {
35
+ parent_type: 'parent',
36
+ score: true,
37
+ query: { term: { field_a: 'value' } },
38
+ inner_hits: {
39
+ sort: [{ 'posted_at' => 'asc' }], size: 1, from: 0,
40
+ _source: {
41
+ includes: ['id', 'content'],
42
+ excludes: ['no_show']
43
+ }
44
+ }
45
+ }
46
+ )
22
47
  end
23
48
  end
@@ -12,8 +12,30 @@ describe ElasticsearchDslBuilder::DSL::Search::Queries::Nested do
12
12
  expect { Queries::Nested.new('path').query({}) }.to raise_error(ArgumentError)
13
13
  end
14
14
 
15
+ it 'should fail if inner_hits argument not a InnerHits' do
16
+ expect { Queries::Nested.new('path').inner_hits('invalid') }.to raise_error(ArgumentError)
17
+ expect { Queries::Nested.new('path').inner_hits({}) }.to raise_error(ArgumentError)
18
+ end
19
+
15
20
  it 'should chain create valid ES query hash' do
16
- match = Queries::Nested.new('path').query(Queries::Exists.new('field_a'))
17
- expect(match.to_hash).to eq(nested: { path: 'path', query: { exists: { field: 'field_a' } } })
21
+ match = Queries::Nested.new('path').
22
+ query(Queries::Exists.new('field_a')).
23
+ inner_hits(
24
+ InnerHits.new.size(1).from(0).sort_by('posted_at', 'asc').
25
+ include_fields('id', 'content').
26
+ exclude_fields('no_show')
27
+ )
28
+ expect(match.to_hash).to eq(
29
+ nested: {
30
+ path: 'path', query: { exists: { field: 'field_a' } },
31
+ inner_hits: {
32
+ sort: [{ 'posted_at' => 'asc' }], size: 1, from: 0,
33
+ _source: {
34
+ includes: ['id', 'content'],
35
+ excludes: ['no_show']
36
+ }
37
+ }
38
+ }
39
+ )
18
40
  end
19
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-dsl-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marvin Guerra
@@ -98,6 +98,7 @@ files:
98
98
  - lib/elasticsearch_dsl_builder/dsl/search/aggregations/sum.rb
99
99
  - lib/elasticsearch_dsl_builder/dsl/search/aggregations/sum_bucket.rb
100
100
  - lib/elasticsearch_dsl_builder/dsl/search/aggregations/terms.rb
101
+ - lib/elasticsearch_dsl_builder/dsl/search/inner_hits.rb
101
102
  - lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb
102
103
  - lib/elasticsearch_dsl_builder/dsl/search/queries/exists.rb
103
104
  - lib/elasticsearch_dsl_builder/dsl/search/queries/function_score.rb
@@ -129,6 +130,7 @@ files:
129
130
  - spec/lib/dsl/search/aggregations/sum_bucket_spec.rb
130
131
  - spec/lib/dsl/search/aggregations/sum_spec.rb
131
132
  - spec/lib/dsl/search/aggregations/terms_spec.rb
133
+ - spec/lib/dsl/search/inner_hits_spec.rb
132
134
  - spec/lib/dsl/search/queries/bool_spec.rb
133
135
  - spec/lib/dsl/search/queries/exists_spec.rb
134
136
  - spec/lib/dsl/search/queries/function_score_spec.rb
@@ -187,6 +189,7 @@ test_files:
187
189
  - spec/lib/dsl/search/aggregations/sum_bucket_spec.rb
188
190
  - spec/lib/dsl/search/aggregations/sum_spec.rb
189
191
  - spec/lib/dsl/search/aggregations/terms_spec.rb
192
+ - spec/lib/dsl/search/inner_hits_spec.rb
190
193
  - spec/lib/dsl/search/queries/bool_spec.rb
191
194
  - spec/lib/dsl/search/queries/exists_spec.rb
192
195
  - spec/lib/dsl/search/queries/function_score_spec.rb