arelastic 2.9.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -0
- data/lib/arelastic.rb +8 -0
- data/lib/arelastic/builders.rb +1 -3
- data/lib/arelastic/builders/aggregations.rb +28 -0
- data/lib/arelastic/builders/queries.rb +33 -0
- data/test/arelastic/builders/aggregations_test.rb +10 -0
- data/test/arelastic/builders/queries_test.rb +20 -4
- data/test/arelastic_test.rb +5 -0
- metadata +4 -8
- data/lib/arelastic/builders/aggregation.rb +0 -31
- data/lib/arelastic/builders/filter.rb +0 -81
- data/lib/arelastic/builders/search.rb +0 -27
- data/test/arelastic/builders/aggregation_test.rb +0 -10
- data/test/arelastic/builders/filter_test.rb +0 -76
- data/test/arelastic/builders/search_test.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41849e76c34d20f19af86422794c1c7c5245d8355cb6a453decc6c87934dbb66
|
4
|
+
data.tar.gz: a32b6ef72a9a28bccba585a0f93e04586a42b18f2ec23bb79134c59a618ea2d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c109a5bd0a2a6e10aa9e9da405574d86b9d0e74795fd61101b0966aab00c57c41043043b268c94f004127f78b5bc6e6f45116fa1f8ca8f962e41ebeb3ff31d43
|
7
|
+
data.tar.gz: 1f5da97d3217067aac0963b059dff48083da71c02d116b8d3e7a3e4ead1c9786833c5218b84a69fb96b4858447417e5c252aa965ad3bd08925f821493931a8c8
|
data/README.md
CHANGED
@@ -1 +1,59 @@
|
|
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
|
+
# Arelastic
|
3
|
+
|
4
|
+
Arelastic is a Elasticsearch AST manager for Ruby. It simplifies the generation complex of Elasticsearch queries
|
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)
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
### Search
|
13
|
+
```ruby
|
14
|
+
search = Arelastic::Builders::Search['name']
|
15
|
+
|
16
|
+
# Name equals red
|
17
|
+
search.eq('red').as_elastic
|
18
|
+
# => {"term"=>{"name"=>"red"}}
|
19
|
+
|
20
|
+
# Negation
|
21
|
+
search.eq("red").negate.as_elastic
|
22
|
+
# => {"bool"=>{"must_not"=>{"term"=>{:name=>"red"}}}}
|
23
|
+
```
|
24
|
+
|
25
|
+
### Limit & Offset
|
26
|
+
```ruby
|
27
|
+
# Limit
|
28
|
+
Arelastic::Searches::Size.new(20).as_elastic
|
29
|
+
# => {"size"=>20}
|
30
|
+
|
31
|
+
# Offset
|
32
|
+
Arelastic::Searches::From.new(20).as_elastic
|
33
|
+
# => {"from"=>20}
|
34
|
+
```
|
35
|
+
|
36
|
+
### Ordering
|
37
|
+
```ruby
|
38
|
+
sort_field = Arelastic::Sorts::Field.new('price' => 'asc')
|
39
|
+
sort_field.as_elastic
|
40
|
+
# => {'price' => 'asc'}
|
41
|
+
|
42
|
+
sort = Arelastic::Searches::Sort.new([sort_field])
|
43
|
+
sort.as_elastic
|
44
|
+
# => {"sort"=>[{"price"=>"asc"}]}
|
45
|
+
```
|
46
|
+
|
47
|
+
### Putting It All Together
|
48
|
+
```ruby
|
49
|
+
search = [
|
50
|
+
Arelastic::Searches::Query.new(Arelastic::Builders::Search['name'].eq('Fun')),
|
51
|
+
Arelastic::Searches::Size.new(20),
|
52
|
+
Arelastic::Searches::From.new(20),
|
53
|
+
Arelastic::Searches::Sort.new([Arelastic::Sorts::Field.new('price' => 'asc')])
|
54
|
+
]
|
55
|
+
Arelastic::Nodes::HashGroup.new(search).as_elastic
|
56
|
+
# => {"query"=>{"term"=>{"name"=>"Fun"}}, "size"=>20, "from"=>20, "sort"=>[{"price"=>"asc"}]}
|
57
|
+
```
|
58
|
+
|
59
|
+
Some helpful Arel builders can be found [here](/lib/arelastic/builders/filter.rb).
|
data/lib/arelastic.rb
CHANGED
data/lib/arelastic/builders.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
require 'arelastic/builders/
|
2
|
-
require 'arelastic/builders/filter'
|
1
|
+
require 'arelastic/builders/aggregations'
|
3
2
|
require 'arelastic/builders/mapping'
|
4
3
|
require 'arelastic/builders/queries'
|
5
|
-
require 'arelastic/builders/search'
|
6
4
|
require 'arelastic/builders/sort'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Arelastic
|
2
|
+
module Builders
|
3
|
+
class Aggregations
|
4
|
+
class << self
|
5
|
+
MACROS_TO_ARELASTIC = {
|
6
|
+
cardinality: Arelastic::Aggregations::Cardinality,
|
7
|
+
date_histogram: Arelastic::Aggregations::DateHistogram,
|
8
|
+
filter: Arelastic::Aggregations::Filter,
|
9
|
+
filters: Arelastic::Aggregations::Filters,
|
10
|
+
histogram: Arelastic::Aggregations::Histogram,
|
11
|
+
missing: Arelastic::Aggregations::Missing,
|
12
|
+
nested: Arelastic::Aggregations::Nested,
|
13
|
+
range: Arelastic::Aggregations::Range,
|
14
|
+
reverse_nested: Arelastic::Aggregations::ReverseNested,
|
15
|
+
sampler: Arelastic::Aggregations::Sampler,
|
16
|
+
terms: Arelastic::Aggregations::Terms,
|
17
|
+
value_count: Arelastic::Aggregations::ValueCount
|
18
|
+
}
|
19
|
+
|
20
|
+
MACROS_TO_ARELASTIC.each do |macro, klass|
|
21
|
+
define_method macro do |*args|
|
22
|
+
klass.new(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -52,6 +52,39 @@ module Arelastic
|
|
52
52
|
klass.new(name, *args)
|
53
53
|
end
|
54
54
|
end
|
55
|
+
|
56
|
+
def in other, options = {}
|
57
|
+
case other
|
58
|
+
when Range
|
59
|
+
if other.exclude_end?
|
60
|
+
range 'gte' => other.begin, 'lt' => other.end
|
61
|
+
else
|
62
|
+
range 'gte' => other.begin, 'lte' => other.end
|
63
|
+
end
|
64
|
+
else
|
65
|
+
terms other, options
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def missing(options = {})
|
70
|
+
exists(options).negate
|
71
|
+
end
|
72
|
+
|
73
|
+
def gte other
|
74
|
+
range 'gte' => other
|
75
|
+
end
|
76
|
+
|
77
|
+
def gt other
|
78
|
+
range 'gt' => other
|
79
|
+
end
|
80
|
+
|
81
|
+
def lte other
|
82
|
+
range 'lte' => other
|
83
|
+
end
|
84
|
+
|
85
|
+
def lt other
|
86
|
+
range 'lt' => other
|
87
|
+
end
|
55
88
|
end
|
56
89
|
end
|
57
90
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Arelastic::Builders::AggregationsTest < Minitest::Test
|
4
|
+
def test_terms
|
5
|
+
expected = {"name" => {"terms" => {"field" => "color"}}}
|
6
|
+
actual = Arelastic::Builders::Aggregations.terms('name', 'field' => 'color').as_elastic
|
7
|
+
|
8
|
+
assert_equal expected, actual
|
9
|
+
end
|
10
|
+
end
|
@@ -50,10 +50,26 @@ class Arelastic::Builders::QueriesTest < Minitest::Test
|
|
50
50
|
assert_equal expected, query.as_elastic
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
|
55
|
-
expected
|
53
|
+
def test_missing
|
54
|
+
expected = {"bool" => {"must_not" => {"exists" => {"field" => "color"}}}}
|
55
|
+
assert_equal expected, Arelastic::Builders::Queries['color'].missing.as_elastic
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
+
def test_in
|
59
|
+
expected = {"terms" => {"color" => ["blue"]}}
|
60
|
+
assert_equal expected, Arelastic::Builders::Queries['color'].in(['blue']).as_elastic
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_in_with_options
|
64
|
+
expected = {"terms" => {"color" => ["blue"], "execution" => "bool"}}
|
65
|
+
assert_equal expected, Arelastic::Builders::Queries['color'].in(['blue'], "execution" => "bool").as_elastic
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_in_with_range
|
69
|
+
expected = {"range" => {"color" => {"gte" => 1, "lte" => 3}}}
|
70
|
+
assert_equal expected, Arelastic::Builders::Queries['color'].in(1..3).as_elastic
|
71
|
+
|
72
|
+
expected = {"range" => {"color" => {"gte" => 1, "lt" => 3}}}
|
73
|
+
assert_equal expected, Arelastic::Builders::Queries['color'].in(1...3).as_elastic
|
58
74
|
end
|
59
75
|
end
|
data/test/arelastic_test.rb
CHANGED
@@ -4,4 +4,9 @@ class ArelasticTest < Minitest::Test
|
|
4
4
|
def test_queries
|
5
5
|
assert_equal Arelastic::Builders::Queries, Arelastic.queries
|
6
6
|
end
|
7
|
+
|
8
|
+
def test_lookup
|
9
|
+
expected = { "term" => {"color" => "blue" }}
|
10
|
+
assert_equal expected, Arelastic.queries['color'].term('blue').as_elastic
|
11
|
+
end
|
7
12
|
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:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Higgins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Build Elastic Search queries with objects
|
14
14
|
email: developer@matthewhiggins.com
|
@@ -40,11 +40,9 @@ files:
|
|
40
40
|
- lib/arelastic/arities/polyadic.rb
|
41
41
|
- lib/arelastic/arities/unary.rb
|
42
42
|
- lib/arelastic/builders.rb
|
43
|
-
- lib/arelastic/builders/
|
44
|
-
- lib/arelastic/builders/filter.rb
|
43
|
+
- lib/arelastic/builders/aggregations.rb
|
45
44
|
- lib/arelastic/builders/mapping.rb
|
46
45
|
- lib/arelastic/builders/queries.rb
|
47
|
-
- lib/arelastic/builders/search.rb
|
48
46
|
- lib/arelastic/builders/sort.rb
|
49
47
|
- lib/arelastic/mappings.rb
|
50
48
|
- lib/arelastic/mappings/type.rb
|
@@ -117,11 +115,9 @@ files:
|
|
117
115
|
- test/arelastic/arities/binary_test.rb
|
118
116
|
- test/arelastic/arities/polyadic_test.rb
|
119
117
|
- test/arelastic/arities/unary_test.rb
|
120
|
-
- test/arelastic/builders/
|
121
|
-
- test/arelastic/builders/filter_test.rb
|
118
|
+
- test/arelastic/builders/aggregations_test.rb
|
122
119
|
- test/arelastic/builders/mapping_test.rb
|
123
120
|
- test/arelastic/builders/queries_test.rb
|
124
|
-
- test/arelastic/builders/search_test.rb
|
125
121
|
- test/arelastic/builders/sort_test.rb
|
126
122
|
- test/arelastic/mappings/types/binary_test.rb
|
127
123
|
- test/arelastic/mappings/types/multi_field_test.rb
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module Arelastic
|
2
|
-
module Builders
|
3
|
-
class Aggregation < Struct.new :name
|
4
|
-
class << self
|
5
|
-
def [](name)
|
6
|
-
new(name)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def date_histogram options
|
11
|
-
Arelastic::Aggregations::DateHistogram.new name, options
|
12
|
-
end
|
13
|
-
|
14
|
-
def filter filter, options
|
15
|
-
Arelastic::Aggregations::Filter.new name, filter, options
|
16
|
-
end
|
17
|
-
|
18
|
-
def histogram options
|
19
|
-
Arelastic::Aggregations::Histogram.new name, options
|
20
|
-
end
|
21
|
-
|
22
|
-
def sample shard_size, aggs
|
23
|
-
Arelastic::Aggregations::Sampler.new(name, sample_size, aggs)
|
24
|
-
end
|
25
|
-
|
26
|
-
def terms options = {}
|
27
|
-
Arelastic::Aggregations::Terms.new(name, options)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
module Arelastic
|
2
|
-
module Builders
|
3
|
-
class Filter < Struct.new :field
|
4
|
-
class << self
|
5
|
-
def [](field)
|
6
|
-
new(field)
|
7
|
-
end
|
8
|
-
|
9
|
-
def ids *ids
|
10
|
-
Arelastic::Queries::Ids.new ids.flatten
|
11
|
-
end
|
12
|
-
|
13
|
-
def not expr
|
14
|
-
Arelastic::Queries::Bool.new must_not: expr
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def eq other
|
19
|
-
Arelastic::Queries::Term.new field, other
|
20
|
-
end
|
21
|
-
|
22
|
-
def not_eq other
|
23
|
-
self.class.not eq(other)
|
24
|
-
end
|
25
|
-
|
26
|
-
def in other, options = {}
|
27
|
-
case other
|
28
|
-
when Range
|
29
|
-
if other.exclude_end?
|
30
|
-
range 'gte' => other.begin, 'lt' => other.end
|
31
|
-
else
|
32
|
-
range 'gte' => other.begin, 'lte' => other.end
|
33
|
-
end
|
34
|
-
else
|
35
|
-
Arelastic::Queries::Terms.new field, other, options
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def not_in other, options = {}
|
40
|
-
self.class.not self.in(other, options)
|
41
|
-
end
|
42
|
-
|
43
|
-
def prefix other
|
44
|
-
Arelastic::Queries::Prefix.new field, other
|
45
|
-
end
|
46
|
-
|
47
|
-
def exists(options = {})
|
48
|
-
Arelastic::Queries::Exists.new field, options
|
49
|
-
end
|
50
|
-
|
51
|
-
def missing(options = {})
|
52
|
-
exists(options).negate
|
53
|
-
end
|
54
|
-
|
55
|
-
def regexp other
|
56
|
-
Arelastic::Queries::Regexp.new field, other
|
57
|
-
end
|
58
|
-
|
59
|
-
def gte other
|
60
|
-
range 'gte' => other
|
61
|
-
end
|
62
|
-
|
63
|
-
def gt other
|
64
|
-
range 'gt' => other
|
65
|
-
end
|
66
|
-
|
67
|
-
def lte other
|
68
|
-
range 'lte' => other
|
69
|
-
end
|
70
|
-
|
71
|
-
def lt other
|
72
|
-
range 'lt' => other
|
73
|
-
end
|
74
|
-
|
75
|
-
private
|
76
|
-
def range options
|
77
|
-
Arelastic::Queries::Range.new field, options
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Arelastic
|
2
|
-
module Builders
|
3
|
-
class Search
|
4
|
-
class << self
|
5
|
-
def filter
|
6
|
-
Arelastic::Builders::Filter
|
7
|
-
end
|
8
|
-
|
9
|
-
def [](field)
|
10
|
-
filter[field]
|
11
|
-
end
|
12
|
-
|
13
|
-
def aggregation
|
14
|
-
Arelastic::Builders::Aggregation
|
15
|
-
end
|
16
|
-
|
17
|
-
def queries
|
18
|
-
Arelastic::Builders::Queries
|
19
|
-
end
|
20
|
-
|
21
|
-
def sort
|
22
|
-
Arelastic::Builders::Sort
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,10 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class Arelastic::Builders::AggregationTest < Minitest::Test
|
4
|
-
def test_terms
|
5
|
-
expected = {"name" => {"terms" => {"field" => "color"}}}
|
6
|
-
actual = Arelastic::Builders::Aggregation['name'].terms('field' => 'color').as_elastic
|
7
|
-
|
8
|
-
assert_equal expected, actual
|
9
|
-
end
|
10
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class Arelastic::Builders::FilterTest < Minitest::Test
|
4
|
-
def test_ids
|
5
|
-
expected = {"ids" => {"values"=>["5", "6"]}}
|
6
|
-
assert_equal expected, Arelastic::Builders::Filter.ids('5', '6').as_elastic
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_not
|
10
|
-
expected = {"bool" => {"must_not" => {"terms" => {"color" => "blue"}}}}
|
11
|
-
assert_equal expected, Arelastic::Builders::Filter.not({"terms" => {"color" => "blue"}}).as_elastic
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_eq
|
15
|
-
expected = {"term"=>{"color"=>"blue"}}
|
16
|
-
assert_equal expected, builder.eq('blue').as_elastic
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_not_eq
|
20
|
-
expected = {"bool" => {"must_not" => {"term" => {"color" => "blue"}}}}
|
21
|
-
assert_equal expected, builder.not_eq('blue').as_elastic
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_in
|
25
|
-
expected = {"terms" => {"color" => ["blue"]}}
|
26
|
-
assert_equal expected, builder.in(['blue']).as_elastic
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_in_with_options
|
30
|
-
expected = {"terms" => {"color" => ["blue"], "execution" => "bool"}}
|
31
|
-
assert_equal expected, builder.in(['blue'], "execution" => "bool").as_elastic
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_in_with_range
|
35
|
-
expected = {"range" => {"color" => {"gte"=>1, "lte"=>3}}}
|
36
|
-
assert_equal expected, builder.in(1..3).as_elastic
|
37
|
-
|
38
|
-
expected = {"range" => {"color" => {"gte"=>1, "lt"=>3}}}
|
39
|
-
assert_equal expected, builder.in(1...3).as_elastic
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_not_in
|
43
|
-
expected = {"bool" => {"must_not" => {"terms" => {"color"=>["blue"]}}}}
|
44
|
-
assert_equal expected, builder.not_in(['blue']).as_elastic
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_prefix
|
48
|
-
expected = {"prefix" => {"color" => "blu"}}
|
49
|
-
assert_equal expected, builder.prefix('blu').as_elastic
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_exists
|
53
|
-
expected = {"exists" => {"field" => "color"}}
|
54
|
-
assert_equal expected, builder.exists.as_elastic
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_missing
|
58
|
-
expected = {"bool" => {"must_not" => {"exists" => {"field" => "color"}}}}
|
59
|
-
assert_equal expected, builder.missing.as_elastic
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_regexp
|
63
|
-
expected = {"regexp" => {"color" => "green(-blue)?"}}
|
64
|
-
assert_equal expected, builder.regexp("green(-blue)?").as_elastic
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_range
|
68
|
-
expected = {"range" => {"color" => {"lt" => 5}}}
|
69
|
-
assert_equal expected, builder.lt(5).as_elastic
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
def builder
|
74
|
-
@builder ||= Arelastic::Builders::Filter['color']
|
75
|
-
end
|
76
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class Arelastic::Builders::SearchTest < Minitest::Test
|
4
|
-
def test_filter
|
5
|
-
assert_equal Arelastic::Builders::Filter, Arelastic::Builders::Search.filter
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_field
|
9
|
-
assert_kind_of Arelastic::Builders::Filter, Arelastic::Builders::Search['poop']
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_aggregation
|
13
|
-
assert_equal Arelastic::Builders::Aggregation, Arelastic::Builders::Search.aggregation
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_query
|
17
|
-
assert_equal Arelastic::Builders::Queries, Arelastic::Builders::Search.queries
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_sort
|
21
|
-
assert_equal Arelastic::Builders::Sort, Arelastic::Builders::Search.sort
|
22
|
-
end
|
23
|
-
end
|