arelastic 3.4.0 → 3.4.1

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: d8edfdb17e46c6148893fd8ae238a07c4743359a9c5e4955b424649727196ff8
4
- data.tar.gz: 3c5c40a1d08cbf850a096b0d72b675c4d63e692d9b55d0b123792e68904a1aa9
3
+ metadata.gz: 36e11d1e2ddab3f9a341f595e3283d942b1ab84ce56f668edf2fd73f219696e3
4
+ data.tar.gz: 1bd43e54049ea28f58edba742ac2fffbd0d1d0eb80a350fa3b7d58ccca734307
5
5
  SHA512:
6
- metadata.gz: eb01a6abd4f16c130a8759a53d40582698221b11e58178ec49297be9cacf03e87bc2cd666a1076480b65a264968b1ef539d9888aaa3416ad4474617f98de5613
7
- data.tar.gz: 20ae0b0240d1b2d439b08d6dbb38576687f7a5d3c2c5bc4bafb018625f5f35b6662e0fd9f62e45b9264ace1c8164ab4e71f68e696100509ccbff83b14af79e26
6
+ metadata.gz: 5b49b306de42d3b05b0dc06d7a9fbd838aaab75d6b082001bce3650104839c632ecd3cde9e4ffaadf00dbb2f22b1cd4f21ea423f664374cf6736bcfee950bc3f
7
+ data.tar.gz: bf5f7f70d412eeb798df3809de62249ad801a6e614d68913663cc208e3483937c4a6086946f10eef3615e5aacb761ead34eaaf5626a535d4e0417775516e9d84
@@ -0,0 +1,9 @@
1
+ module Arelastic
2
+ module Aggregations
3
+ class Max < Arelastic::Aggregations::Aggregation
4
+ def as_elastic_aggregation
5
+ {'max' => options}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -10,6 +10,7 @@ require 'arelastic/aggregations/date_histogram'
10
10
  require 'arelastic/aggregations/filter'
11
11
  require 'arelastic/aggregations/filters'
12
12
  require 'arelastic/aggregations/histogram'
13
+ require 'arelastic/aggregations/max'
13
14
  require 'arelastic/aggregations/min'
14
15
  require 'arelastic/aggregations/missing'
15
16
  require 'arelastic/aggregations/nested'
@@ -3,16 +3,20 @@ module Arelastic
3
3
  class Aggregations
4
4
  class << self
5
5
  MACROS_TO_ARELASTIC = {
6
- cardinality: Arelastic::Aggregations::Cardinality,
6
+ cardinality: Arelastic::Aggregations::Cardinality,
7
7
  date_histogram: Arelastic::Aggregations::DateHistogram,
8
8
  filter: Arelastic::Aggregations::Filter,
9
9
  filters: Arelastic::Aggregations::Filters,
10
10
  histogram: Arelastic::Aggregations::Histogram,
11
+ max: Arelastic::Aggregations::Max,
12
+ min: Arelastic::Aggregations::Min,
11
13
  missing: Arelastic::Aggregations::Missing,
12
14
  nested: Arelastic::Aggregations::Nested,
15
+ parent: Arelastic::Aggregations::Parent,
13
16
  range: Arelastic::Aggregations::Range,
14
17
  reverse_nested: Arelastic::Aggregations::ReverseNested,
15
18
  sampler: Arelastic::Aggregations::Sampler,
19
+ sum: Arelastic::Aggregations::Sum,
16
20
  terms: Arelastic::Aggregations::Terms,
17
21
  value_count: Arelastic::Aggregations::ValueCount
18
22
  }
@@ -13,6 +13,7 @@ module Arelastic
13
13
  geo_bounding_box: Arelastic::Queries::GeoBoundingBox,
14
14
  geo_distance: Arelastic::Queries::GeoDistance,
15
15
  geo_polygon: Arelastic::Queries::GeoPolygon,
16
+ geo_shape_polygon: Arelastic::Queries::GeoShape::Polygon,
16
17
  has_child: Arelastic::Queries::HasChild,
17
18
  has_parent: Arelastic::Queries::HasParent,
18
19
  ids: Arelastic::Queries::Ids,
@@ -7,7 +7,7 @@ module Arelastic
7
7
 
8
8
  def convert_to_elastic(expr)
9
9
  if expr.is_a?(Array)
10
- expr.map { |e| convert_to_elastic(e) }
10
+ expr.uniq.map { |e| convert_to_elastic(e) }
11
11
  elsif expr.is_a?(Hash)
12
12
  expr.transform_values { |e| convert_to_elastic(e) }
13
13
  else
@@ -22,6 +22,12 @@ module Arelastic
22
22
  def ==(other)
23
23
  other.is_a?(Arelastic::Nodes::Node) && as_elastic == other.as_elastic
24
24
  end
25
+
26
+ def hash
27
+ as_elastic.hash
28
+ end
29
+
30
+ alias eql? ==
25
31
  end
26
32
  end
27
33
  end
@@ -19,11 +19,30 @@ module Arelastic
19
19
  'should' => should,
20
20
  'must_not' => must_not
21
21
  }.each do |k, v|
22
+ if v.is_a?(Array)
23
+ v = merge_expression(k, v, klass: Queries::HasParent, merge_test_method: :parent_type)
24
+ v = merge_expression(k, v, klass: Queries::HasChild, merge_test_method: :type)
25
+ v = merge_expression(k, v, klass: Queries::Nested, merge_test_method: :path_and_options)
26
+ end
27
+
22
28
  searches[k] = convert_to_elastic(v) if v
23
29
  end
24
30
 
25
31
  { 'bool' => searches.update(options) }
26
32
  end
33
+
34
+ def merge_expression(clause, expr, klass:, merge_test_method:)
35
+ mergeable, other = expr.partition { |el| el.is_a?(klass) }
36
+ merged = mergeable.group_by(&merge_test_method).flat_map do |_, group|
37
+ if group.size > 1
38
+ group.first.instance_variable_set("@query", self.class.new(clause => group.map(&:query)))
39
+ group.first
40
+ else
41
+ group
42
+ end
43
+ end
44
+ merged + other
45
+ end
27
46
  end
28
47
  end
29
48
  end
@@ -2,16 +2,19 @@ module Arelastic
2
2
  module Queries
3
3
  class GeoPolygon < Arelastic::Queries::Query
4
4
  attr_accessor :field, :points, :options
5
+
5
6
  def initialize(field, points, options = {})
7
+ warn 'Arelastic::Queries::GeoPolygon is deprecated in Elastic Search 7.12;'
8
+ + 'Use Arelastic::Queries::GeoShape instead.'
6
9
  @field = field
7
10
  @points = points
8
11
  @options = options
9
12
  end
10
13
 
11
14
  def as_elastic
12
- params = {field => {"points" => points}}.update(options)
15
+ params = { field => { 'points' => points } }.update(options)
13
16
 
14
- { "geo_polygon" => params }
17
+ { 'geo_polygon' => params }
15
18
  end
16
19
  end
17
20
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arelastic
4
+ module Queries
5
+ module GeoShape
6
+ class Polygon < Arelastic::Queries::Query
7
+ attr_accessor :field, :points, :options
8
+
9
+ def initialize(field, points, options = {})
10
+ @field = field
11
+ @points = points
12
+ @options = options
13
+ end
14
+
15
+ def as_elastic
16
+ params = {
17
+ 'shape' => { 'type' => 'polygon', 'coordinates' => [points] },
18
+ 'relation' => 'within'
19
+ }.update(options)
20
+
21
+ { 'geo_shape' => { field => params } }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'arelastic/queries/geo_shape/polygon'
@@ -17,6 +17,10 @@ module Arelastic
17
17
 
18
18
  { 'nested' => params }
19
19
  end
20
+
21
+ def path_and_options
22
+ [path, options]
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -11,6 +11,7 @@ require 'arelastic/queries/fuzzy'
11
11
  require 'arelastic/queries/geo_bounding_box'
12
12
  require 'arelastic/queries/geo_distance'
13
13
  require 'arelastic/queries/geo_polygon'
14
+ require 'arelastic/queries/geo_shape'
14
15
  require 'arelastic/queries/has_child'
15
16
  require 'arelastic/queries/has_parent'
16
17
  require 'arelastic/queries/ids'
@@ -23,7 +24,6 @@ require 'arelastic/queries/multi_match'
23
24
  require 'arelastic/queries/nested'
24
25
  require 'arelastic/queries/percolate'
25
26
  require 'arelastic/queries/prefix'
26
- require 'arelastic/queries/query'
27
27
  require 'arelastic/queries/query_string'
28
28
  require 'arelastic/queries/range'
29
29
  require 'arelastic/queries/regexp'
@@ -0,0 +1,7 @@
1
+ module Arelastic
2
+ module Searches
3
+ class RuntimeMappings < Arelastic::Searches::Search
4
+ unary 'runtime_mappings'
5
+ end
6
+ end
7
+ end
@@ -3,5 +3,6 @@ require 'arelastic/searches/search'
3
3
  require 'arelastic/searches/aggregations'
4
4
  require 'arelastic/searches/from'
5
5
  require 'arelastic/searches/query'
6
+ require 'arelastic/searches/runtime_mappings'
6
7
  require 'arelastic/searches/size'
7
8
  require 'arelastic/searches/sort'
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class Arelastic::Aggregations::MaxTest < Minitest::Test
4
+ def test_as_elastic
5
+ aggregation = Arelastic::Aggregations::Max.new('max_segment_id', 'field' => 'segment_id')
6
+
7
+ expected = {
8
+ "max_segment_id" => {
9
+ "max" => {
10
+ "field" => "segment_id"
11
+ }
12
+ }
13
+ }
14
+ assert_equal expected, aggregation.as_elastic
15
+ end
16
+ end
@@ -8,6 +8,6 @@ class Arelastic::Arities::PolyadicTest < Minitest::Test
8
8
  end
9
9
 
10
10
  expected = {'xor' => [{'a' => 'b'}, {'x' => 'y'}]}
11
- assert_equal expected, node.new([expr.new('a' => 'b'), expr.new('x' => 'y')]).as_elastic
11
+ assert_equal expected, node.new([expr.new({ 'a' => 'b' }), expr.new({'x' => 'y' })]).as_elastic
12
12
  end
13
13
  end
@@ -1,5 +1,17 @@
1
1
  require 'helper'
2
2
 
3
3
  class Arelastic::Builders::SortTest < Minitest::Test
4
-
5
- end
4
+ def test_asc
5
+ query = Arelastic::Builders::Sort['field'].asc
6
+ expected = { 'field' => 'asc' }
7
+
8
+ assert_equal expected, query.as_elastic
9
+ end
10
+
11
+ def test_desc
12
+ query = Arelastic::Builders::Sort['field'].desc
13
+ expected = { 'field' => 'desc' }
14
+
15
+ assert_equal expected, query.as_elastic
16
+ end
17
+ end
@@ -13,7 +13,7 @@ class Arelastic::Queries::BoolTest < Minitest::Test
13
13
  #
14
14
  # def test_as_elastic_with_query_node
15
15
  # filtered = Arelastic::Queries::Filtered.new(query: Arelastic::Queries::QueryString.new("bar"))
16
- # expected = {"filtered" => {"query" => {"query_string" => {"query"=>"bar"}}}}
16
+ # expected = {"filtered" => {"query" => {"query_string" => {"query" => "bar"}}}}
17
17
  #
18
18
  # assert_equal expected, filtered.as_elastic
19
19
  # end
@@ -55,4 +55,67 @@ class Arelastic::Queries::BoolTest < Minitest::Test
55
55
 
56
56
  assert_equal expected, bool.as_elastic
57
57
  end
58
+
59
+ def test_merging_terms
60
+
61
+ expected = {
62
+ "bool" => {
63
+ "must"=> [
64
+ {
65
+ "nested" => {
66
+ "path" => "friends",
67
+ "query"=> { "match" => { "friends.age" => { "query" => "101" } } },
68
+ "inner_hits" => {}
69
+ }
70
+ },
71
+ {
72
+ "nested" => {
73
+ "path" => "friends",
74
+ "query" => {
75
+ "bool" => {
76
+ "must"=> [
77
+ { "match" => { "friends.first_name" => { "query" => "Thomas" } } },
78
+ { "match" => { "friends.last_name" => { "query" => "Jefferson" } } }
79
+ ]
80
+ }
81
+ }
82
+ }
83
+ }
84
+ ],
85
+ "should" => [
86
+ {
87
+ "has_parent" => {
88
+ "parent_type" => "user",
89
+ "query" => {
90
+ "bool" => {
91
+ "should" => [
92
+ { "query_string1" => "Texas" },
93
+ { "query_string2" => "Delaware" }
94
+ ]
95
+ }
96
+ }
97
+ }
98
+ },
99
+ {
100
+ "has_parent" => {
101
+ "parent_type" => "president",
102
+ "query" => { "query_string3" => "Washington" }
103
+ }
104
+ }
105
+ ]
106
+ }
107
+ }
108
+
109
+ should = [
110
+ Arelastic::Queries::HasParent.new("user", { "query_string1" => "Texas" }),
111
+ Arelastic::Queries::HasParent.new("user", { "query_string2" => "Delaware" }),
112
+ Arelastic::Queries::HasParent.new("president", { "query_string3" => "Washington" })
113
+ ]
114
+ must = [
115
+ Arelastic::Queries::Nested.new("friends", {"match" => { "friends.age" => { "query" => "101" }}}, 'inner_hits' => {}),
116
+ Arelastic::Queries::Nested.new("friends", {"match" => { "friends.first_name" => { "query" => "Thomas" }}}),
117
+ Arelastic::Queries::Nested.new("friends", {"match" => { "friends.last_name" => { "query" => "Jefferson" }}})
118
+ ]
119
+ assert_equal expected, Arelastic::Queries::Bool.new(should: should, must: must).as_elastic
120
+ end
58
121
  end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ class Arelastic::Queries::GeoShape::PolygonTest < Minitest::Test
4
+ def test_as_elastic
5
+ points = [
6
+ [-124.33, 47.15],
7
+ [-124.42, 46.63],
8
+ [-123.84, 46.15]
9
+ ]
10
+ expected = {
11
+ 'geo_shape' => {
12
+ 'person.location' => {
13
+ 'shape' => {
14
+ 'type' => 'polygon',
15
+ 'coordinates' => [points]
16
+ },
17
+ 'relation' => 'within'
18
+ }
19
+ }
20
+ }
21
+
22
+ assert_equal expected, Arelastic::Queries::GeoShape::Polygon.new('person.location', points).as_elastic
23
+ end
24
+ end
@@ -7,4 +7,15 @@ class Arelastic::Queries::MatchAllTest < Minitest::Test
7
7
 
8
8
  assert_equal expected, match_all.as_elastic
9
9
  end
10
- end
10
+
11
+ def test_merging_match_all_terms
12
+ expected = { "bool" => { "should" => [{
13
+ "match_all" => {}
14
+ }]}}
15
+
16
+ term1 = Arelastic::Queries::MatchAll.new
17
+ term2 = Arelastic::Queries::MatchAll.new
18
+ assert_equal expected, Arelastic::Queries::Bool.new(should: [term1, term2]).as_elastic
19
+ end
20
+
21
+ end
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.4.0
4
+ version: 3.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Higgins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-07 00:00:00.000000000 Z
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Build Elastic Search queries with objects
14
14
  email: developer@matthewhiggins.com
@@ -30,6 +30,7 @@ files:
30
30
  - lib/arelastic/aggregations/filter.rb
31
31
  - lib/arelastic/aggregations/filters.rb
32
32
  - lib/arelastic/aggregations/histogram.rb
33
+ - lib/arelastic/aggregations/max.rb
33
34
  - lib/arelastic/aggregations/min.rb
34
35
  - lib/arelastic/aggregations/missing.rb
35
36
  - lib/arelastic/aggregations/nested.rb
@@ -74,6 +75,8 @@ files:
74
75
  - lib/arelastic/queries/geo_bounding_box.rb
75
76
  - lib/arelastic/queries/geo_distance.rb
76
77
  - lib/arelastic/queries/geo_polygon.rb
78
+ - lib/arelastic/queries/geo_shape.rb
79
+ - lib/arelastic/queries/geo_shape/polygon.rb
77
80
  - lib/arelastic/queries/has_child.rb
78
81
  - lib/arelastic/queries/has_parent.rb
79
82
  - lib/arelastic/queries/ids.rb
@@ -99,6 +102,7 @@ files:
99
102
  - lib/arelastic/searches/aggregations.rb
100
103
  - lib/arelastic/searches/from.rb
101
104
  - lib/arelastic/searches/query.rb
105
+ - lib/arelastic/searches/runtime_mappings.rb
102
106
  - lib/arelastic/searches/search.rb
103
107
  - lib/arelastic/searches/size.rb
104
108
  - lib/arelastic/searches/sort.rb
@@ -115,6 +119,7 @@ files:
115
119
  - test/arelastic/aggregations/date_histogram_test.rb
116
120
  - test/arelastic/aggregations/filter_test.rb
117
121
  - test/arelastic/aggregations/filters_test.rb
122
+ - test/arelastic/aggregations/max_test.rb
118
123
  - test/arelastic/aggregations/missing_test.rb
119
124
  - test/arelastic/aggregations/nested_test.rb
120
125
  - test/arelastic/aggregations/range_test.rb
@@ -141,6 +146,7 @@ files:
141
146
  - test/arelastic/queries/geo_bounding_box_test.rb
142
147
  - test/arelastic/queries/geo_distance_test.rb
143
148
  - test/arelastic/queries/geo_polygon_test.rb
149
+ - test/arelastic/queries/geo_shape/polygon_test.rb
144
150
  - test/arelastic/queries/has_child_test.rb
145
151
  - test/arelastic/queries/has_parent_test.rb
146
152
  - test/arelastic/queries/ids_test.rb