arelastic 2.7.0 → 3.2.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/aggregations.rb +3 -0
- data/lib/arelastic/aggregations/bucket_selector.rb +9 -0
- data/lib/arelastic/aggregations/bucket_sort.rb +18 -0
- data/lib/arelastic/aggregations/cardinality.rb +9 -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/lib/arelastic/queries/nested.rb +8 -7
- data/test/arelastic/aggregations/bucket_selector_test.rb +25 -0
- data/test/arelastic/aggregations/bucket_sort_test.rb +45 -0
- data/test/arelastic/aggregations/cardinality_test.rb +16 -0
- data/test/arelastic/builders/aggregations_test.rb +10 -0
- data/test/arelastic/builders/queries_test.rb +20 -4
- data/test/arelastic/queries/geo_polygon_test.rb +1 -1
- data/test/arelastic/queries/nested_test.rb +20 -0
- data/test/arelastic_test.rb +5 -0
- metadata +15 -13
- 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: '0984b722326d3ec3a60b11114a249eba54399ba4a28a5f05635ec3c6570b16dc'
|
4
|
+
data.tar.gz: 7f4451aa328ab073bc7aa66818bcb98b28ac6d2354c8d086213fdad1a441532d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01d68d3d7669ab860e232fdb6723b70460ddb103e2c8a4b3d4597a49d090b24da4ef25f3a6a847ee76e459e00b8b6f983499216aa9aa672bef80fe0141913af1
|
7
|
+
data.tar.gz: 1a866d2a4b7129a7b59c44ed7c030748937f70bd0539afe9ed45fb6e10ca6a672d1fa4795672b5b9569b34c327764a8a1c798572e49090ebd017a631370430e9
|
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
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'arelastic/aggregations/aggregation'
|
2
2
|
require 'arelastic/aggregations/bucket'
|
3
3
|
|
4
|
+
require 'arelastic/aggregations/bucket_sort'
|
5
|
+
require 'arelastic/aggregations/bucket_selector'
|
6
|
+
require 'arelastic/aggregations/cardinality'
|
4
7
|
require 'arelastic/aggregations/date_histogram'
|
5
8
|
require 'arelastic/aggregations/filter'
|
6
9
|
require 'arelastic/aggregations/filters'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Arelastic
|
2
|
+
module Aggregations
|
3
|
+
class BucketSort < Arelastic::Aggregations::Aggregation
|
4
|
+
attr_accessor :sort
|
5
|
+
|
6
|
+
def initialize(name, options = {})
|
7
|
+
super
|
8
|
+
@sort = read_option! options, 'sort'
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_elastic_aggregation
|
12
|
+
params = options
|
13
|
+
params = params.merge('sort' => convert_to_elastic(sort)) if sort
|
14
|
+
{ 'bucket_sort' => params }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
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
|
@@ -1,21 +1,22 @@
|
|
1
1
|
module Arelastic
|
2
2
|
module Queries
|
3
3
|
class Nested < Arelastic::Queries::Query
|
4
|
-
attr_accessor :path, :
|
4
|
+
attr_accessor :path, :query, :options
|
5
5
|
|
6
|
-
def initialize(path,
|
7
|
-
@path
|
8
|
-
@
|
6
|
+
def initialize(path, query, options = {})
|
7
|
+
@path = path
|
8
|
+
@query = query
|
9
|
+
@options = options
|
9
10
|
end
|
10
11
|
|
11
12
|
def as_elastic
|
12
13
|
params = {
|
13
14
|
'path' => path,
|
14
|
-
'query' => convert_to_elastic(
|
15
|
-
}
|
15
|
+
'query' => convert_to_elastic(query)
|
16
|
+
}.update(options)
|
16
17
|
|
17
18
|
{ 'nested' => params }
|
18
19
|
end
|
19
20
|
end
|
20
21
|
end
|
21
|
-
end
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class Arelastic::Aggregations::BucketSelectorTest < Minitest::Test
|
4
|
+
def test_as_elastic
|
5
|
+
aggregation = Arelastic::Aggregations::BucketSelector.new("foo_agg",
|
6
|
+
"buckets_path" => {
|
7
|
+
"foo_bucket" => "foo_bucket_path",
|
8
|
+
"bar_bucket" => "bar_bucket_path"
|
9
|
+
},
|
10
|
+
"script" => "params.foo_bucket > 10 && params.bar_bucket < 100"
|
11
|
+
)
|
12
|
+
expected = {
|
13
|
+
"foo_agg" => {
|
14
|
+
"bucket_selector" => {
|
15
|
+
"buckets_path" => {
|
16
|
+
"foo_bucket" => "foo_bucket_path",
|
17
|
+
"bar_bucket" => "bar_bucket_path"
|
18
|
+
},
|
19
|
+
"script" => "params.foo_bucket > 10 && params.bar_bucket < 100"
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
assert_equal expected, aggregation.as_elastic
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class Arelastic::Aggregations::BucketSortTest < Minitest::Test
|
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
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_arelastic_sort
|
20
|
+
sort = Arelastic::Sorts::Field.new("price" => "desc")
|
21
|
+
aggregation = Arelastic::Aggregations::BucketSort.new("foo_agg", "sort" => sort)
|
22
|
+
|
23
|
+
expected = {
|
24
|
+
"foo_agg" => {
|
25
|
+
"bucket_sort" => {
|
26
|
+
"sort" => {"price" => "desc"}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
assert_equal expected, aggregation.as_elastic
|
31
|
+
end
|
32
|
+
|
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
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Arelastic::Aggregations::CardinalityTest < Minitest::Test
|
4
|
+
def test_as_elastic
|
5
|
+
aggregation = Arelastic::Aggregations::Cardinality.new('foo', 'field' => 'name')
|
6
|
+
|
7
|
+
expected = {
|
8
|
+
"foo" => {
|
9
|
+
"cardinality" => {
|
10
|
+
"field" => "name"
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
assert_equal expected, aggregation.as_elastic
|
15
|
+
end
|
16
|
+
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
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Arelastic::Queries::NestedTest < Minitest::Test
|
4
|
+
def test_as_elastic
|
5
|
+
expected = {
|
6
|
+
'nested' => {
|
7
|
+
'path' => 'comments',
|
8
|
+
'query' => {
|
9
|
+
'match' => {
|
10
|
+
'author'=>'matt'
|
11
|
+
}
|
12
|
+
},
|
13
|
+
'inner_hits' => {}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
query = Arelastic::Queries::Match.new('author', 'matt')
|
18
|
+
assert_equal expected, Arelastic::Queries::Nested.new('comments', query, 'inner_hits' => {}).as_elastic
|
19
|
+
end
|
20
|
+
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: 2.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Higgins
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Build Elastic Search queries with objects
|
14
14
|
email: developer@matthewhiggins.com
|
@@ -22,6 +22,9 @@ files:
|
|
22
22
|
- lib/arelastic/aggregations.rb
|
23
23
|
- lib/arelastic/aggregations/aggregation.rb
|
24
24
|
- lib/arelastic/aggregations/bucket.rb
|
25
|
+
- lib/arelastic/aggregations/bucket_selector.rb
|
26
|
+
- lib/arelastic/aggregations/bucket_sort.rb
|
27
|
+
- lib/arelastic/aggregations/cardinality.rb
|
25
28
|
- lib/arelastic/aggregations/date_histogram.rb
|
26
29
|
- lib/arelastic/aggregations/filter.rb
|
27
30
|
- lib/arelastic/aggregations/filters.rb
|
@@ -39,11 +42,9 @@ files:
|
|
39
42
|
- lib/arelastic/arities/polyadic.rb
|
40
43
|
- lib/arelastic/arities/unary.rb
|
41
44
|
- lib/arelastic/builders.rb
|
42
|
-
- lib/arelastic/builders/
|
43
|
-
- lib/arelastic/builders/filter.rb
|
45
|
+
- lib/arelastic/builders/aggregations.rb
|
44
46
|
- lib/arelastic/builders/mapping.rb
|
45
47
|
- lib/arelastic/builders/queries.rb
|
46
|
-
- lib/arelastic/builders/search.rb
|
47
48
|
- lib/arelastic/builders/sort.rb
|
48
49
|
- lib/arelastic/mappings.rb
|
49
50
|
- lib/arelastic/mappings/type.rb
|
@@ -102,7 +103,10 @@ files:
|
|
102
103
|
- lib/arelastic/sorts/geo_distance.rb
|
103
104
|
- lib/arelastic/sorts/sort.rb
|
104
105
|
- test/arelastic/aggregations/aggregation_test.rb
|
106
|
+
- test/arelastic/aggregations/bucket_selector_test.rb
|
107
|
+
- test/arelastic/aggregations/bucket_sort_test.rb
|
105
108
|
- test/arelastic/aggregations/bucket_test.rb
|
109
|
+
- test/arelastic/aggregations/cardinality_test.rb
|
106
110
|
- test/arelastic/aggregations/date_histogram_test.rb
|
107
111
|
- test/arelastic/aggregations/filter_test.rb
|
108
112
|
- test/arelastic/aggregations/filters_test.rb
|
@@ -115,11 +119,9 @@ files:
|
|
115
119
|
- test/arelastic/arities/binary_test.rb
|
116
120
|
- test/arelastic/arities/polyadic_test.rb
|
117
121
|
- test/arelastic/arities/unary_test.rb
|
118
|
-
- test/arelastic/builders/
|
119
|
-
- test/arelastic/builders/filter_test.rb
|
122
|
+
- test/arelastic/builders/aggregations_test.rb
|
120
123
|
- test/arelastic/builders/mapping_test.rb
|
121
124
|
- test/arelastic/builders/queries_test.rb
|
122
|
-
- test/arelastic/builders/search_test.rb
|
123
125
|
- test/arelastic/builders/sort_test.rb
|
124
126
|
- test/arelastic/mappings/types/binary_test.rb
|
125
127
|
- test/arelastic/mappings/types/multi_field_test.rb
|
@@ -140,6 +142,7 @@ files:
|
|
140
142
|
- test/arelastic/queries/match_phrase_test.rb
|
141
143
|
- test/arelastic/queries/match_test.rb
|
142
144
|
- test/arelastic/queries/multi_match_test.rb
|
145
|
+
- test/arelastic/queries/nested_test.rb
|
143
146
|
- test/arelastic/queries/percolate_test.rb
|
144
147
|
- test/arelastic/queries/query_string_test.rb
|
145
148
|
- test/arelastic/queries/query_test.rb
|
@@ -157,7 +160,7 @@ homepage: http://github.com/matthuhiggins/arelastic
|
|
157
160
|
licenses:
|
158
161
|
- MIT
|
159
162
|
metadata: {}
|
160
|
-
post_install_message:
|
163
|
+
post_install_message:
|
161
164
|
rdoc_options: []
|
162
165
|
require_paths:
|
163
166
|
- lib
|
@@ -172,9 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
175
|
- !ruby/object:Gem::Version
|
173
176
|
version: '0'
|
174
177
|
requirements: []
|
175
|
-
|
176
|
-
|
177
|
-
signing_key:
|
178
|
+
rubygems_version: 3.2.15
|
179
|
+
signing_key:
|
178
180
|
specification_version: 4
|
179
181
|
summary: Elastic Search query builder
|
180
182
|
test_files: []
|
@@ -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
|