elasticsearch_query 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -5
- data/lib/elasticsearch_query/sort.rb +3 -4
- data/lib/elasticsearch_query/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1abd0e1f72b653412d2ec2ef4f290369791e5a2d
|
4
|
+
data.tar.gz: 35edb681d3dd0f024b5ea6c5d6c2a14432365a1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4965a348cc695976ba36d96c36c19344037071933fc87b04177198a2465b06e21e2444b47094a282da2fda670d5ecb1074c7bf7274a6527b94d0624f6e3e49a2
|
7
|
+
data.tar.gz: 0ec0f9e80067bda0b510cdec72ca8b8b954d84189f06375a8e4df66f6f67b206925314dcab54666ebdf199e1ba9c18e9cf2a9ede4ccb3f77ad57a04cd1293be5
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# ElasticsearchQuery
|
2
2
|
|
3
|
-
|
3
|
+
`ElasticsearchQuery` is a tranformer from [`JSONAPI`](http://jsonapi.org) style params hash into an [`Elasticseatch-ruby`](https://github.com/elastic/elasticsearch-ruby)-compatible object that can easily be fed into `client.search`
|
4
4
|
|
5
|
-
|
5
|
+
*Note*: This gem was written for use with a [modified](https://github.com/tiagopog/jsonapi-utils/pull/90) [`JSONAPI::Utils`](https://github.com/tiagopog/jsonapi-utils/) and uses several concepts from it (Paginator interface, param structure), but has no hard dependencies.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,79 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
Using the gem is pretty strightforward. You have to set up 2 things: the paginator, and the range parser (if your API accepts range filters)
|
26
|
+
|
27
|
+
*NOTE*: This gem assumes that params come in the form of
|
28
|
+
```ruby
|
29
|
+
{
|
30
|
+
filter: { key: :value },
|
31
|
+
sort: "sort,-other_sort"
|
32
|
+
}.merge( your_pagination_format_here )
|
33
|
+
```
|
34
|
+
|
35
|
+
### Example
|
36
|
+
|
37
|
+
If you are using the above mentioned [modified `JSONAPI::Utils`](https://github.com/tiagopog/jsonapi-utils/pull/90), Your controller action would look like:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class MyApiController < ApplicationController
|
41
|
+
def index
|
42
|
+
# to_unsafe_h is here in order to support any version of rails/other frameworks that just get a hash for params
|
43
|
+
@results = MyModel.search_from_params( params.to_unsafe_h )
|
44
|
+
jsonapi_render json: @results
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Configuration
|
50
|
+
|
51
|
+
As mentioned earlier the config is assignment of a paginator class (required) and a range formatter class (optional)
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# in your config/initializers/elasticsearch_query.rb
|
55
|
+
ElasticsearchQuery::Query.paginator_class = MyPaginator
|
56
|
+
ElasticsearchQuery::FilterFormatter::Range.parser = MyRangeParser
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Paginator
|
60
|
+
|
61
|
+
Most APIs want paged results. But since your interface is your own and not super relevant to how the query is contructed, it needs to duck the following type:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class MyPaginator
|
65
|
+
def initialize( params )
|
66
|
+
@params = params
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_hash
|
70
|
+
{ size: size,
|
71
|
+
from: from }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
What the params look like and how you extract the page size and offset(`from`) are up to you.
|
77
|
+
|
78
|
+
#### RangeFormatter
|
79
|
+
|
80
|
+
If your API has values that are filterable by range (e.g. `created_at`) `ElasticsearchQuery` can supprt those values; all you have to do is set up how to parse the parameter.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class MyRangeParser
|
84
|
+
def initialize( value )
|
85
|
+
@value = value
|
86
|
+
end
|
87
|
+
|
88
|
+
# if your values come in as beginning-to-end for some reason
|
89
|
+
def parse
|
90
|
+
value.split "-to-"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
The result of `MyRangeParser#parse` method **MUST** respond to `#first` and `#last`, so things like `Array` and `Range` are great things to return, but if yoiu want to roll your own, have at it.
|
96
|
+
|
97
|
+
*Note*: This gem assumes that infinity and negative infinity are represented as `inf` and `neginf`.
|
26
98
|
|
27
99
|
## Development
|
28
100
|
|
@@ -32,7 +104,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
104
|
|
33
105
|
## Contributing
|
34
106
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gaorlov/elasticsearch_query. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
108
|
|
37
109
|
## License
|
38
110
|
|
@@ -40,4 +112,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
112
|
|
41
113
|
## Code of Conduct
|
42
114
|
|
43
|
-
Everyone interacting in the ElasticsearchQuery project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
115
|
+
Everyone interacting in the ElasticsearchQuery project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gaorlov/elasticsearch_query/blob/master/CODE_OF_CONDUCT.md).
|
@@ -1,8 +1,7 @@
|
|
1
1
|
module ElasticsearchQuery
|
2
2
|
class Sort
|
3
|
-
def initialize( params = {}
|
3
|
+
def initialize( params = {} )
|
4
4
|
@params = params
|
5
|
-
@default = default
|
6
5
|
end
|
7
6
|
|
8
7
|
def to_hash
|
@@ -18,11 +17,11 @@ module ElasticsearchQuery
|
|
18
17
|
|
19
18
|
def sorts
|
20
19
|
@sorts ||= begin
|
21
|
-
sorts = @params.fetch( :sort,
|
20
|
+
sorts = @params.fetch( :sort, "" ).split(',')
|
22
21
|
|
23
22
|
sorts.each_with_object([]) do |field, arr|
|
24
23
|
desc, field = field.match(/^([-_])?(\w+)$/i)[1..2]
|
25
|
-
arr << { field => desc
|
24
|
+
arr << { field => desc ? :desc : :asc }
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|