meilisearch-rails 0.7.3 → 0.8.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: 849d3f11092d279468f2303406fac8ed52849490cd5f044d0f111c2731593f76
4
- data.tar.gz: 41ec2f87a63e74bb6c4237435490b381f80def5dee0f8393c3a40cd583386a03
3
+ metadata.gz: 95c22d57895ceb454d6026064d0e27d4f0abcba6cf6e8d30171bde564174bf83
4
+ data.tar.gz: 45e490dae97786d644cbd0c4bd3f63e84d02b7cffc3b4cb1fc5f393c53a61b80
5
5
  SHA512:
6
- metadata.gz: c6fb870cced8f9e395f5bbc23404331d932b8ebe92921f9867ce53e4b682a114d4a39a0d319a44395fe9d2e4f5907752ca923febee4503b4fa23df437f2237ab
7
- data.tar.gz: d74ded33afca229deb79ed57ab820c0737f1bb2b3f258aafa4155a3d29515e24a0b9cb5680d106d0ebd9e39892f9a9e45aed8a50b934669558b8c9c11d1eddb5
6
+ metadata.gz: 9cf32c4a4a74f4c29be2e89ed97fc592de962402001473ddfb0a9cbb8d8c258aec87f2ccca18efce1db338e403065df08159e6f31d96d83060cf0815d2c0c2e6
7
+ data.tar.gz: cdcbc8944ab5be9ddf5e8b60a56620ab843488a8699b81e5df152127d8ce7dab79ab1bfc1430680d3f15b472f121196b248c69fae30c67d9fc405b421da1c010
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ # gem 'meilisearch', path: '../meilisearch-ruby'
4
+
3
5
  gemspec
4
6
 
5
7
  gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  <h4 align="center">
8
8
  <a href="https://github.com/meilisearch/meilisearch">Meilisearch</a> |
9
9
  <a href="https://docs.meilisearch.com">Documentation</a> |
10
- <a href="https://slack.meilisearch.com">Slack</a> |
10
+ <a href="https://discord.meilisearch.com">Discord</a> |
11
11
  <a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> |
12
12
  <a href="https://www.meilisearch.com">Website</a> |
13
13
  <a href="https://docs.meilisearch.com/faq">FAQ</a>
@@ -23,18 +23,20 @@
23
23
 
24
24
  **Meilisearch Rails** is the Meilisearch integration for Ruby on Rails developers.
25
25
 
26
- **Meilisearch** is an open-source search engine. [Discover what Meilisearch is!](https://github.com/meilisearch/meilisearch)
26
+ **Meilisearch** is an open-source search engine. [Learn more about Meilisearch.](https://github.com/meilisearch/meilisearch)
27
27
 
28
28
  ## Table of Contents <!-- omit in toc -->
29
29
 
30
30
  - [📖 Documentation](#-documentation)
31
31
  - [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
32
- - [🚀 Getting Started](#-getting-started)
32
+ - [🚀 Getting started](#-getting-started)
33
33
  - [Compatibility](#-compatibility)
34
34
  - [⚙️ Settings](#️-settings)
35
35
  - [🔍 Custom search](#-custom-search)
36
36
  - [🪛 Options](#-options)
37
37
  - [Meilisearch configuration & environment](#meilisearch-configuration--environment)
38
+ - [Pagination with `kaminari` or `will_paginate`](#backend-pagination-with-kaminari-or-will_paginate-)
39
+ - [Pagination with `pagy`](#backend-pagination-with-pagy-)
38
40
  - [Index configuration](#index-configuration)
39
41
  - [Custom attribute definition](#custom-attribute-definition)
40
42
  - [Custom primary key](#custom-primary-key)
@@ -60,7 +62,7 @@ To learn more about Meilisearch, check out our [Documentation](https://docs.meil
60
62
 
61
63
  ## 🤖 Compatibility with Meilisearch
62
64
 
63
- This package only guarantees the compatibility with the [version v0.28.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.28.0).
65
+ This package only guarantees the compatibility with the [version v1.0.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v1.0.0).
64
66
 
65
67
  ## 🔧 Installation <!-- omit in toc -->
66
68
 
@@ -91,7 +93,7 @@ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest meilisearch --maste
91
93
 
92
94
  NB: you can also download Meilisearch from **Homebrew** or **APT**.
93
95
 
94
- ## 🚀 Getting Started
96
+ ## 🚀 Getting started
95
97
 
96
98
  #### Configuration <!-- omit in toc -->
97
99
 
@@ -99,7 +101,7 @@ Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARC
99
101
 
100
102
  ```ruby
101
103
  MeiliSearch::Rails.configuration = {
102
- meilisearch_host: 'YourMeilisearchHost', # example: http://localhost:7700
104
+ meilisearch_url: 'YourMeilisearchUrl', # example: http://localhost:7700
103
105
  meilisearch_api_key: 'YourMeilisearchAPIKey',
104
106
  }
105
107
  ```
@@ -138,46 +140,6 @@ hits.each do |hit|
138
140
  end
139
141
  ```
140
142
 
141
- #### Backend Pagination <!-- omit in toc -->
142
-
143
- This gem supports:
144
- - [kaminari](https://github.com/amatsuda/kaminari)
145
- - [pagy](https://github.com/ddnexus/pagy)
146
- - [will_paginate](https://github.com/mislav/will_paginate).
147
-
148
- Specify the `:pagination_backend` in the configuration file:
149
-
150
- ```ruby
151
- MeiliSearch::Rails.configuration = {
152
- meilisearch_host: 'YourMeilisearchHost',
153
- meilisearch_api_key: 'YourMeilisearchAPIKey',
154
- pagination_backend: :kaminari #:will_paginate
155
- }
156
- ```
157
-
158
- Then, as soon as you use the `search` method, the returning results will be paginated:
159
-
160
- ```ruby
161
- # controller
162
- @hits = Book.search('harry potter')
163
-
164
- # views
165
- <% @hits.each do |hit| %>
166
- <%= hit.title %>
167
- <%= hit.author %>
168
- <% end %>
169
-
170
- <%= paginate @hits %> # if using kaminari
171
-
172
- <%= will_paginate @hits %> # if using will_paginate
173
- ```
174
-
175
- The **number of hits per page defaults to 20**, you can customize it by adding the `hits_per_page` parameter to your search:
176
-
177
- ```ruby
178
- Book.search('harry potter', hits_per_page: 10)
179
- ```
180
-
181
143
  #### Extra Configuration <!-- omit in toc -->
182
144
 
183
145
  Requests made to Meilisearch may timeout and retry. To adapt the behavior to
@@ -185,7 +147,7 @@ your needs, you can change the parameters during configuration:
185
147
 
186
148
  ```ruby
187
149
  MeiliSearch::Rails.configuration = {
188
- meilisearch_host: 'YourMeilisearchHost',
150
+ meilisearch_url: 'YourMeilisearchUrl',
189
151
  meilisearch_api_key: 'YourMeilisearchAPIKey',
190
152
  timeout: 2,
191
153
  max_retries: 1,
@@ -223,6 +185,7 @@ class Book < ApplicationRecord
223
185
  attributes_to_highlight ['*']
224
186
  attributes_to_crop [:description]
225
187
  crop_length 10
188
+ pagination max_total_hits: 1000
226
189
  end
227
190
  end
228
191
  ```
@@ -236,6 +199,13 @@ All the supported options are described in the [search parameters](https://docs.
236
199
  ```ruby
237
200
  Book.search('Harry', attributes_to_highlight: ['*'])
238
201
  ```
202
+
203
+ Then it's possible to retrieve the highlighted or cropped value by using the `formatted` method available in the object.
204
+
205
+ ```ruby
206
+ harry_book.formatted # => {"id"=>"1", "name"=>"<em>Harry</em> Potter", "description"=>…
207
+ ```
208
+
239
209
  👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
240
210
  `crop_length` can be set up in the `meilisearch` block of your model.
241
211
 
@@ -254,6 +224,89 @@ Book.search('*', sort: ['title:asc'])
254
224
 
255
225
  ### Meilisearch configuration & environment
256
226
 
227
+ ### Backend Pagination with `kaminari` or `will_paginate` <!-- omit in toc -->
228
+
229
+ This gem supports:
230
+ - [kaminari](https://github.com/amatsuda/kaminari)
231
+ - [will_paginate](https://github.com/mislav/will_paginate)
232
+
233
+ Specify the `:pagination_backend` in the configuration file:
234
+
235
+ ```ruby
236
+ MeiliSearch::Rails.configuration = {
237
+ meilisearch_url: 'YourMeilisearchUrl',
238
+ meilisearch_api_key: 'YourMeilisearchAPIKey',
239
+ pagination_backend: :kaminari # :will_paginate
240
+ }
241
+ ```
242
+
243
+ Then, as soon as you use the `search` method, the returning results will be paginated:
244
+
245
+ ```ruby
246
+ # controller
247
+ @hits = Book.search('harry potter')
248
+
249
+ # views
250
+ <% @hits.each do |hit| %>
251
+ <%= hit.title %>
252
+ <%= hit.author %>
253
+ <% end %>
254
+
255
+ <%= paginate @hits %> # if using kaminari
256
+
257
+ <%= will_paginate @hits %> # if using will_paginate
258
+ ```
259
+
260
+ The **number of hits per page defaults to 20**, you can customize it by adding the `hits_per_page` parameter to your search:
261
+
262
+ ```ruby
263
+ Book.search('harry potter', hits_per_page: 10)
264
+ ```
265
+
266
+ ### Backend Pagination with `pagy` <!-- omit in toc -->
267
+
268
+ This gem supports [pagy](https://github.com/ddnexus/pagy) to paginate your search results.
269
+
270
+ To use `pagy` with your `meilisearch-rails` you need to:
271
+
272
+ Add the `pagy` gem to your Gemfile.
273
+ Create a new initializer `pagy.rb` with this:
274
+
275
+ ```rb
276
+ # config/initializers/pagy.rb
277
+
278
+ require 'pagy/extras/meilisearch'
279
+ ```
280
+
281
+ Then in your model you must extend `Pagy::Meilisearch`:
282
+
283
+ ```rb
284
+ class Book < ApplicationRecord
285
+ include MeiliSearch::Rails
286
+ extend Pagy::Meilisearch
287
+
288
+ meilisearch # ...
289
+ end
290
+ ```
291
+
292
+ And in your controller and view:
293
+
294
+ ```rb
295
+ # controllers/books_controller.rb
296
+ def search
297
+ hits = Book.pagy_search(params[:query])
298
+ @pagy, @hits = pagy_meilisearch(hits, items: 25)
299
+ end
300
+
301
+
302
+ # views/books/search.html.rb
303
+ <%== pagy_nav(@pagy) %>
304
+ ```
305
+
306
+ :warning: There is no need to set `pagination_backend` in the configuration block `MeiliSearch::Rails.configuration` for `pagy`.
307
+
308
+ Check [`ddnexus/pagy`](https://ddnexus.github.io/pagy/extras/meilisearch) for more information.
309
+
257
310
  #### Deactivate Meilisearch in certain moments
258
311
 
259
312
  By default HTTP connections to the Meilisearch URL is always active, but sometimes you want to disable the HTTP requests in a particular moment or environment.<br>
@@ -263,7 +316,7 @@ By adding `active: false` in the configuration initializer:
263
316
 
264
317
  ```ruby
265
318
  MeiliSearch::Rails.configuration = {
266
- meilisearch_host: 'YourMeilisearchHost',
319
+ meilisearch_url: 'YourMeilisearchUrl',
267
320
  meilisearch_api_key: 'YourMeilisearchAPIKey',
268
321
  active: false
269
322
  }
@@ -308,7 +361,7 @@ You can suffix the index UID with the current Rails environment by setting it gl
308
361
 
309
362
  ```ruby
310
363
  MeiliSearch::Rails.configuration = {
311
- meilisearch_host: 'YourMeilisearchHost',
364
+ meilisearch_url: 'YourMeilisearchUrl',
312
365
  meilisearch_api_key: 'YourMeilisearchAPIKey',
313
366
  per_environment: true
314
367
  }
@@ -45,7 +45,7 @@ module MeiliSearch
45
45
  return black_hole unless active?
46
46
 
47
47
  ::MeiliSearch::Client.new(
48
- configuration[:meilisearch_host] || 'http://localhost:7700',
48
+ configuration[:meilisearch_url] || 'http://localhost:7700',
49
49
  configuration[:meilisearch_api_key],
50
50
  configuration.slice(:timeout, :max_retries)
51
51
  .merge(client_agents: MeiliSearch::Rails.qualified_version)
@@ -7,7 +7,7 @@ module MeiliSearch
7
7
  class NotConfigured < StandardError
8
8
  def message
9
9
  'Please configure Meilisearch. Set MeiliSearch::Rails.configuration = ' \
10
- "{meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}"
10
+ "{meilisearch_url: 'YOUR_MEILISEARCH_URL', meilisearch_api_key: 'YOUR_API_KEY'}"
11
11
  end
12
12
  end
13
13
  end
@@ -17,30 +17,18 @@ module MeiliSearch
17
17
  end
18
18
  end
19
19
 
20
- def limit(_num)
21
- # noop
22
- self
23
- end
24
-
25
- def offset(_num)
26
- # noop
27
- self
28
- end
20
+ def self.create(results, total_hits, options = {})
21
+ offset = ((options[:page] - 1) * options[:per_page])
22
+ array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
29
23
 
30
- class << self
31
- def create(results, total_hits, options = {})
32
- offset = ((options[:page] - 1) * options[:per_page])
33
- array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
34
-
35
- if array.empty? && !results.empty?
36
- # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
37
- # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
38
- results = ([nil] * offset) + results
39
- array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
40
- end
41
-
42
- array
24
+ if array.empty? && !results.empty?
25
+ # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
26
+ # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
27
+ results = Array.new(offset) + results
28
+ array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
43
29
  end
30
+
31
+ array
44
32
  end
45
33
  end
46
34
  end
@@ -11,9 +11,7 @@ module MeiliSearch
11
11
  class WillPaginate
12
12
  def self.create(results, total_hits, options = {})
13
13
  ::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
14
- start = (options[:page] - 1) * options[:per_page]
15
- paginated_results = results[start, options[:per_page]]
16
- pager.replace paginated_results
14
+ pager.replace results
17
15
  end
18
16
  end
19
17
  end
@@ -5,15 +5,28 @@ module MeiliSearch
5
5
  autoload :Kaminari, 'meilisearch/rails/pagination/kaminari'
6
6
 
7
7
  def self.create(results, total_hits, options = {})
8
- return results if MeiliSearch::Rails.configuration[:pagination_backend].nil?
8
+ pagination_backend = MeiliSearch::Rails.configuration[:pagination_backend]
9
9
 
10
- begin
11
- backend = MeiliSearch::Rails.configuration[:pagination_backend].to_s.classify
10
+ if pagination_backend.nil? || (is_pagy = pagination_backend.to_s == 'pagy')
11
+ log_pagy_error if is_pagy
12
12
 
13
- ::MeiliSearch::Rails::Pagination.const_get(backend).create(results, total_hits, options)
14
- rescue NameError
15
- raise(BadConfiguration, 'Unknown pagination backend')
13
+ return results
16
14
  end
15
+
16
+ load_pagination!(pagination_backend, results, total_hits, options)
17
+ end
18
+
19
+ def self.log_pagy_error
20
+ (::Rails.logger || Logger.new($stdout))
21
+ .warning('[meilisearch-rails] Remove `pagination_backend: :pagy` from your initializer, `pagy` it is not required for `pagy`')
22
+ end
23
+
24
+ def self.load_pagination!(pagination_backend, results, total_hits, options)
25
+ ::MeiliSearch::Rails::Pagination
26
+ .const_get(pagination_backend.to_s.classify)
27
+ .create(results, total_hits, options)
28
+ rescue NameError
29
+ raise(BadConfiguration, 'Invalid `pagination_backend:` configuration, check your initializer.')
17
30
  end
18
31
  end
19
32
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module MeiliSearch
4
4
  module Rails
5
- VERSION = '0.7.3'
5
+ VERSION = '0.8.1'
6
6
 
7
7
  def self.qualified_version
8
8
  "Meilisearch Rails (v#{VERSION})"
@@ -59,8 +59,11 @@ module MeiliSearch
59
59
  attributesToHighlight
60
60
  attributesToCrop
61
61
  cropLength
62
+ pagination
62
63
  ].freeze
63
64
 
65
+ CAMELIZE_OPTIONS = %i[pagination].freeze
66
+
64
67
  OPTIONS.each do |option|
65
68
  define_method option do |value|
66
69
  instance_variable_set("@#{option}", value)
@@ -199,7 +202,13 @@ module MeiliSearch
199
202
  settings = {}
200
203
  OPTIONS.each do |k|
201
204
  v = get_setting(k)
202
- settings[k] = v unless v.nil?
205
+ next if v.nil?
206
+
207
+ settings[k] = if CAMELIZE_OPTIONS.include?(k) && v.is_a?(Hash)
208
+ v.transform_keys { |key| key.to_s.camelize(:lower) }
209
+ else
210
+ v
211
+ end
203
212
  end
204
213
  settings
205
214
  end
@@ -284,7 +293,7 @@ module MeiliSearch
284
293
  raise e if raise_on_failure
285
294
 
286
295
  # log the error
287
- (::Rails.logger || Logger.new($stdout)).error("[meilisearch-rails] #{e.message}")
296
+ (::Rails.logger || Logger.new($stdout)).info("[meilisearch-rails] #{e.message}")
288
297
  # return something
289
298
  case method.to_s
290
299
  when 'search'
@@ -602,18 +611,15 @@ module MeiliSearch
602
611
 
603
612
  def ms_search(query, params = {})
604
613
  if MeiliSearch::Rails.configuration[:pagination_backend]
605
- page = params[:page].nil? ? params[:page] : params[:page].to_i
606
- hits_per_page = params[:hitsPerPage].nil? ? params[:hitsPerPage] : params[:hitsPerPage].to_i
607
- hits_per_page ||= params[:hits_per_page].nil? ? params[:hits_per_page] : params[:hits_per_page].to_i
608
-
609
- %i[page hitsPerPage hits_per_page].each { |param| params.delete(param) }
614
+ %i[page hitsPerPage hits_per_page].each do |key|
615
+ params[key.to_s.underscore.to_sym] = params[key].to_i if params.key?(key)
616
+ end
610
617
 
611
- params[:limit] = 200
618
+ # It is required to activate the finite pagination in Meilisearch v0.30 (or newer),
619
+ # to have at least `hits_per_page` defined or `page` in the search request.
620
+ params[:page] ||= 1
612
621
  end
613
622
 
614
- # Returns raw json hits as follows:
615
- # {"hits"=>[{"id"=>"13", "href"=>"apple", "name"=>"iphone"}], "offset"=>0, "limit"=>|| 20, "estimatedTotalHits"=>1,
616
- # "processingTimeMs"=>0, "query"=>"iphone"}
617
623
  json = ms_raw_search(query, params)
618
624
 
619
625
  # condition_key gets the primary key of the document; looks for "id" on the options
@@ -656,11 +662,7 @@ module MeiliSearch
656
662
  end
657
663
  end.compact
658
664
 
659
- total_hits = json['hits'].length
660
- hits_per_page ||= 20
661
- page ||= 1
662
-
663
- res = MeiliSearch::Rails::Pagination.create(results, total_hits, meilisearch_options.merge(page: page, per_page: hits_per_page))
665
+ res = Pagination.create(results, json['totalHits'], meilisearch_options.merge(page: json['page'], per_page: json['hitsPerPage']))
664
666
  res.extend(AdditionalMethods)
665
667
  res.send(:ms_init_raw_answer, json)
666
668
  res
@@ -34,5 +34,5 @@ Gem::Specification.new do |s|
34
34
 
35
35
  s.required_ruby_version = '>= 2.6.0'
36
36
 
37
- s.add_dependency 'meilisearch', '~> 0.19.2'
37
+ s.add_dependency 'meilisearch', '~> 0.22.0'
38
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meilisearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-28 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: meilisearch
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.19.2
19
+ version: 0.22.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.19.2
26
+ version: 0.22.0
27
27
  description: Meilisearch integration for Ruby on Rails. See https://github.com/meilisearch/meilisearch
28
28
  email: bonjour@meilisearch.com
29
29
  executables: []