arelastic 3.1.0 → 3.3.2

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
  SHA256:
3
- metadata.gz: b92131dd2729b5be9ad302a31d33db3482ecc47dee0b97681b3cca050cf627ba
4
- data.tar.gz: d2c3fa9dc8ffe7c8f1c268c030964f23281b15dcba1655bc845d6c5ce8a91862
3
+ metadata.gz: 2795d56fffdc0a61f805e08103f757e3d9d6733e494cb6f6483439b105fc23e4
4
+ data.tar.gz: 7812934cd5319e2404c80fd55fbfdccf5fed51c951c3f4a2659624772b668514
5
5
  SHA512:
6
- metadata.gz: 3e1e3bbdb544e0ee9b39b8c48100339e4340a0adf5985c5a524187ca7758798f596fdf2f7fb3ed221a5334f9f882e037d740267fa29a5cd434c3c13c015fdc3c
7
- data.tar.gz: 7e3578aa3af4ca0efb60fe148a0cd611144d76c9e18b5a78623f43778b3b792e5987a8745dde2e175002e50a4781227bf86500f72716efab76b26387392a4337
6
+ metadata.gz: c5d90ef13123ff1a5eb7e03b7abd2839767697b6cf6905932eafc08fed5e92423da48f7b2111b82633514fea062563d658a60a06e110a7673474aa9145357f20
7
+ data.tar.gz: 7ae6737a2b74e4081d8a4202a7c01e7e10d8b037cb3b4b238dbf4602faa8a2b55e0e49b143ceb0e2df80de02cf831be15f69bdb183c1ea95dfd71b25c6bb780f
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  [![Build Status](https://travis-ci.org/matthuhiggins/arelastic.svg?branch=master)](https://travis-ci.org/matthuhiggins/arelastic) [![Code Climate](https://codeclimate.com/github/matthuhiggins/arelastic/badges/gpa.svg)](https://codeclimate.com/github/matthuhiggins/arelastic)
2
2
  # Arelastic
3
3
 
4
- Arelastic is a Elasticsearch AST manager for Ruby. It simplifies the generation complex of Elasticsearch queries
4
+ Arelastic is a ElasticSearch AST manager for Ruby. It simplifies the generation complex of Elasticsearch queries.
5
5
 
6
- It is intended to be a framework framework; that is, you can build your own ORM with it.
7
-
8
- One example is [Elastic Record](https://github.com/data-axle/elastic_record)
6
+ It was extracted from my [Elastic Record](https://github.com/data-axle/elastic_record) project.
9
7
 
10
8
  ## Usage
11
9
 
@@ -1,22 +1,8 @@
1
1
  module Arelastic
2
2
  module Aggregations
3
3
  class BucketSelector < Arelastic::Aggregations::Aggregation
4
- attr_accessor :script_params, :script
5
-
6
- def initialize(name, options = {})
7
- options = options.dup
8
- @script_params = read_option! options, 'script_params'
9
- @script = read_option! options, 'script'
10
- super(name, options)
11
- end
12
-
13
4
  def as_elastic_aggregation
14
- {
15
- 'bucket_selector' => {
16
- 'buckets_path' => script_params,
17
- 'script' => script
18
- }
19
- }
5
+ { 'bucket_selector' => options }
20
6
  end
21
7
  end
22
8
  end
@@ -1,37 +1,18 @@
1
1
  module Arelastic
2
2
  module Aggregations
3
3
  class BucketSort < Arelastic::Aggregations::Aggregation
4
- attr_accessor :from, :size, :sort
4
+ attr_accessor :sort
5
5
 
6
6
  def initialize(name, options = {})
7
- options = options.dup
8
- @from = read_option! options, 'from'
9
- @size = read_option! options, 'size'
7
+ super
10
8
  @sort = read_option! options, 'sort'
11
- super(name, options)
12
9
  end
13
10
 
14
11
  def as_elastic_aggregation
15
- bucket_sort = {}
16
- bucket_sort['sort'] = parse_sort if sort
17
- bucket_sort['from'] = from if from
18
- bucket_sort['size'] = size if size
19
- { 'bucket_sort' => bucket_sort }
12
+ params = options
13
+ params = params.merge('sort' => convert_to_elastic(sort)) if sort
14
+ { 'bucket_sort' => params }
20
15
  end
21
-
22
- private
23
-
24
- def parse_sort
25
- if sort.is_a?(Hash)
26
- sort.map { |bucket_path, order| { bucket_path => order } }
27
- elsif sort.is_a?(String)
28
- [sort]
29
- elsif sort.is_a?(Array)
30
- sort
31
- else
32
- raise TypeError.new('Expecting a hash, string, or array as the "sort" argument')
33
- end
34
- end
35
16
  end
36
17
  end
37
18
  end
@@ -0,0 +1,9 @@
1
+ module Arelastic
2
+ module Aggregations
3
+ class Sum < Arelastic::Aggregations::Aggregation
4
+ def as_elastic_aggregation
5
+ {'sum' => options}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -14,5 +14,6 @@ require 'arelastic/aggregations/nested'
14
14
  require 'arelastic/aggregations/range'
15
15
  require 'arelastic/aggregations/reverse_nested'
16
16
  require 'arelastic/aggregations/sampler'
17
+ require 'arelastic/aggregations/sum'
17
18
  require 'arelastic/aggregations/terms'
18
19
  require 'arelastic/aggregations/value_count'
@@ -14,6 +14,7 @@ module Arelastic
14
14
  geo_distance: Arelastic::Queries::GeoDistance,
15
15
  geo_polygon: Arelastic::Queries::GeoPolygon,
16
16
  has_child: Arelastic::Queries::HasChild,
17
+ has_parent: Arelastic::Queries::HasParent,
17
18
  ids: Arelastic::Queries::Ids,
18
19
  limit: Arelastic::Queries::Limit,
19
20
  match: Arelastic::Queries::Match,
@@ -0,0 +1,21 @@
1
+ module Arelastic
2
+ module Queries
3
+ class HasParent < Arelastic::Queries::Query
4
+ attr_reader :parent_type, :query
5
+
6
+ def initialize parent_type, query
7
+ @parent_type = parent_type
8
+ @query = query
9
+ end
10
+
11
+ def as_elastic
12
+ {
13
+ "has_parent" => {
14
+ "parent_type" => parent_type,
15
+ "query" => convert_to_elastic(query)
16
+ }
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,6 +5,14 @@ module Arelastic
5
5
  Arelastic::Queries::Nested.new path, self
6
6
  end
7
7
 
8
+ def has_child path
9
+ Arelastic::Queries::HasChild.new path, self
10
+ end
11
+
12
+ def has_parent path
13
+ Arelastic::Queries::HasParent.new path, self
14
+ end
15
+
8
16
  def negate
9
17
  Arelastic::Queries::Bool.new must_not: self
10
18
  end
@@ -12,6 +12,7 @@ require 'arelastic/queries/geo_bounding_box'
12
12
  require 'arelastic/queries/geo_distance'
13
13
  require 'arelastic/queries/geo_polygon'
14
14
  require 'arelastic/queries/has_child'
15
+ require 'arelastic/queries/has_parent'
15
16
  require 'arelastic/queries/ids'
16
17
  require 'arelastic/queries/limit'
17
18
  require 'arelastic/queries/match'
@@ -1,22 +1,22 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  class Arelastic::Aggregations::BucketSelectorTest < Minitest::Test
4
4
  def test_as_elastic
5
- aggregation = Arelastic::Aggregations::BucketSelector.new('foo_agg',
6
- script_params: {
7
- 'foo_bucket' => 'foo_bucket_path',
8
- 'bar_bucket' => 'bar_bucket_path'
5
+ aggregation = Arelastic::Aggregations::BucketSelector.new("foo_agg",
6
+ "buckets_path" => {
7
+ "foo_bucket" => "foo_bucket_path",
8
+ "bar_bucket" => "bar_bucket_path"
9
9
  },
10
- script: "params.foo_bucket > 10 && params.bar_bucket < 100"
10
+ "script" => "params.foo_bucket > 10 && params.bar_bucket < 100"
11
11
  )
12
12
  expected = {
13
- 'foo_agg' => {
14
- 'bucket_selector' => {
15
- 'buckets_path' => {
16
- 'foo_bucket' => 'foo_bucket_path',
17
- 'bar_bucket' => 'bar_bucket_path'
13
+ "foo_agg" => {
14
+ "bucket_selector" => {
15
+ "buckets_path" => {
16
+ "foo_bucket" => "foo_bucket_path",
17
+ "bar_bucket" => "bar_bucket_path"
18
18
  },
19
- 'script' => 'params.foo_bucket > 10 && params.bar_bucket < 100'
19
+ "script" => "params.foo_bucket > 10 && params.bar_bucket < 100"
20
20
  }
21
21
  }
22
22
  }
@@ -1,25 +1,45 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  class Arelastic::Aggregations::BucketSortTest < Minitest::Test
4
- def test_parse_sort
5
- assert_equal ['foo'], parse_sort('foo')
6
- assert_equal [{'foo' => 'desc'}, {'bar' => 'asc'}], parse_sort({'foo' => 'desc', 'bar' => 'asc'})
4
+ def test_as_elastic
5
+ aggregation = Arelastic::Aggregations::BucketSort.new("foo_agg", "size" => 3, "from" => 5, "sort" => "price")
6
+
7
+ expected = {
8
+ "foo_agg" => {
9
+ "bucket_sort" => {
10
+ "sort" => "price",
11
+ "from" => 5,
12
+ "size" => 3
13
+ }
14
+ }
15
+ }
16
+ assert_equal expected, aggregation.as_elastic
7
17
  end
8
18
 
9
- def test_size_and_from
10
- aggregation = Arelastic::Aggregations::BucketSort.new('foo_agg', 'size' => 3, 'from' => 5)
19
+ def test_arelastic_sort
20
+ sort = Arelastic::Sorts::Field.new("price" => "desc")
21
+ aggregation = Arelastic::Aggregations::BucketSort.new("foo_agg", "sort" => sort)
22
+
11
23
  expected = {
12
- 'foo_agg' => {
13
- 'bucket_sort' => {
14
- 'from' => 5,
15
- 'size' => 3
24
+ "foo_agg" => {
25
+ "bucket_sort" => {
26
+ "sort" => {"price" => "desc"}
16
27
  }
17
28
  }
18
29
  }
19
30
  assert_equal expected, aggregation.as_elastic
20
31
  end
21
32
 
22
- def parse_sort(sort)
23
- Arelastic::Aggregations::BucketSort.new('foosort', sort: sort).as_elastic['foosort']['bucket_sort']['sort']
33
+ def test_optional_sort
34
+ aggregation = Arelastic::Aggregations::BucketSort.new("foo_agg", "size" => 3)
35
+
36
+ expected = {
37
+ "foo_agg" => {
38
+ "bucket_sort" => {
39
+ "size" => 3
40
+ }
41
+ }
42
+ }
43
+ assert_equal expected, aggregation.as_elastic
24
44
  end
25
45
  end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class Arelastic::Aggregations::SumTest < Minitest::Test
4
+ def test_as_elastic
5
+ aggregation = Arelastic::Aggregations::Sum.new('foo', 'field' => 'bar_count')
6
+
7
+ expected = {
8
+ "foo" => {
9
+ "sum" => {
10
+ "field" => "bar_count"
11
+ }
12
+ }
13
+ }
14
+ assert_equal expected, aggregation.as_elastic
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ require 'helper'
2
+
3
+ class Arelastic::Queries::HasParentTest < Minitest::Test
4
+ def test_as_elastic
5
+ expected = {"has_parent" => {"parent_type" => "user", "query" => {"query_string" => "foo"}}}
6
+
7
+ assert_equal expected, Arelastic::Queries::HasParent.new("user", {"query_string" => "foo"}).as_elastic
8
+ end
9
+ end
@@ -16,6 +16,36 @@ class Arelastic::Queries::QueryTest < Minitest::Test
16
16
  assert_equal(expected, nested_query.as_elastic)
17
17
  end
18
18
 
19
+ def test_has_child
20
+ query = Arelastic::Queries::Term.new('foo', 'bar')
21
+
22
+ nested_query = query.has_child 'links'
23
+
24
+ assert nested_query.is_a?(Arelastic::Queries::HasChild)
25
+ expected = {
26
+ "has_child" => {
27
+ "type" => "links",
28
+ "query" => query.as_elastic
29
+ }
30
+ }
31
+ assert_equal(expected, nested_query.as_elastic)
32
+ end
33
+
34
+ def test_has_parent
35
+ query = Arelastic::Queries::Term.new('foo', 'bar')
36
+
37
+ nested_query = query.has_parent 'links'
38
+
39
+ assert nested_query.is_a?(Arelastic::Queries::HasParent)
40
+ expected = {
41
+ "has_parent" => {
42
+ "parent_type" => "links",
43
+ "query" => query.as_elastic
44
+ }
45
+ }
46
+ assert_equal(expected, nested_query.as_elastic)
47
+ end
48
+
19
49
  def test_negate
20
50
  filter = Arelastic::Queries::Term.new 'foo', 'bar'
21
51
 
data/test/helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'simplecov'
2
+
1
3
  require 'bundler/setup'
2
4
  Bundler.require
3
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arelastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Higgins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-24 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Build Elastic Search queries with objects
14
14
  email: developer@matthewhiggins.com
@@ -35,6 +35,7 @@ files:
35
35
  - lib/arelastic/aggregations/range.rb
36
36
  - lib/arelastic/aggregations/reverse_nested.rb
37
37
  - lib/arelastic/aggregations/sampler.rb
38
+ - lib/arelastic/aggregations/sum.rb
38
39
  - lib/arelastic/aggregations/terms.rb
39
40
  - lib/arelastic/aggregations/value_count.rb
40
41
  - lib/arelastic/arities.rb
@@ -71,6 +72,7 @@ files:
71
72
  - lib/arelastic/queries/geo_distance.rb
72
73
  - lib/arelastic/queries/geo_polygon.rb
73
74
  - lib/arelastic/queries/has_child.rb
75
+ - lib/arelastic/queries/has_parent.rb
74
76
  - lib/arelastic/queries/ids.rb
75
77
  - lib/arelastic/queries/limit.rb
76
78
  - lib/arelastic/queries/match.rb
@@ -115,6 +117,7 @@ files:
115
117
  - test/arelastic/aggregations/range_test.rb
116
118
  - test/arelastic/aggregations/reverse_nested_test.rb
117
119
  - test/arelastic/aggregations/sampler_test.rb
120
+ - test/arelastic/aggregations/sum_test.rb
118
121
  - test/arelastic/aggregations/terms_test.rb
119
122
  - test/arelastic/arities/binary_test.rb
120
123
  - test/arelastic/arities/polyadic_test.rb
@@ -136,6 +139,7 @@ files:
136
139
  - test/arelastic/queries/geo_distance_test.rb
137
140
  - test/arelastic/queries/geo_polygon_test.rb
138
141
  - test/arelastic/queries/has_child_test.rb
142
+ - test/arelastic/queries/has_parent_test.rb
139
143
  - test/arelastic/queries/ids_test.rb
140
144
  - test/arelastic/queries/match_all_test.rb
141
145
  - test/arelastic/queries/match_none_test.rb
@@ -175,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
179
  - !ruby/object:Gem::Version
176
180
  version: '0'
177
181
  requirements: []
178
- rubygems_version: 3.2.15
182
+ rubygems_version: 3.3.6
179
183
  signing_key:
180
184
  specification_version: 4
181
185
  summary: Elastic Search query builder