elasticsearch_query 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: e046e13523ec5f641a624c51a7676c0407e53985
4
- data.tar.gz: 5049e0aea93d2b18077b63c6c7af875eadd7568d
3
+ metadata.gz: 1abd0e1f72b653412d2ec2ef4f290369791e5a2d
4
+ data.tar.gz: 35edb681d3dd0f024b5ea6c5d6c2a14432365a1e
5
5
  SHA512:
6
- metadata.gz: 1c597f1a86af453cd714ccb25c0e67da3db35875162caca2b6a66f0d186815192175c2429f0cf4d6f8528b588b4580ebf0f42183479a4b65847147d3b041d35a
7
- data.tar.gz: 380550d41d26078648f5ad92133d5a662f2ee5158dfc73464846c7f6167a1718be51584c9356b18cf3a138ec14b577323688bd459d557c90bcceb6e36360ed04
6
+ metadata.gz: 4965a348cc695976ba36d96c36c19344037071933fc87b04177198a2465b06e21e2444b47094a282da2fda670d5ecb1074c7bf7274a6527b94d0624f6e3e49a2
7
+ data.tar.gz: 0ec0f9e80067bda0b510cdec72ca8b8b954d84189f06375a8e4df66f6f67b206925314dcab54666ebdf199e1ba9c18e9cf2a9ede4ccb3f77ad57a04cd1293be5
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # ElasticsearchQuery
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elasticsearch_query`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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/[USERNAME]/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.
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/[USERNAME]/elasticsearch_query/blob/master/CODE_OF_CONDUCT.md).
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 = {}, default = "" )
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, @default ).split(',')
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&.empty? ? :asc : :desc }
24
+ arr << { field => desc ? :desc : :asc }
26
25
  end
27
26
  end
28
27
  end
@@ -1,3 +1,3 @@
1
1
  module ElasticsearchQuery
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Orlov