meilisearch-rails 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b992f0ae920706899ea36534585a605e0ce7dcee087bcb5aa5a1fa7f29fbede
4
- data.tar.gz: 7d4339178919b50b791f129485dc20646fb7122910073f99a469ef15e3da7452
3
+ metadata.gz: 2957f44b71394e8f2c6c3e3fa8aa3907b85ef4405c2cbb5230080383eebbe034
4
+ data.tar.gz: 99ebb927e9194b10746355e5ba3ae44853fa4c1ee2e98f6818e910c073f01b8d
5
5
  SHA512:
6
- metadata.gz: 202e76dd8aa69ee7440698ac6af2cff4d2b80016f50f39da95c24f40a824f2095a8481b1f6db64d95d07428d406739fa04513c8efa0ee29d0bbe95a6d77f7183
7
- data.tar.gz: eba66cf0d9a4a5059c40325e8b848478a0268d3650d8f0aacc41598d68c555f6f793d56c19456f79ed08b08fd748a7a1474b2621cd9b211a7dd1c27502fb685c
6
+ metadata.gz: '08834c370f32354ad47eb1d904212e8535a5b47f89a705ad3aeb42bf7a4768b0dd51f4198b86855f3691c886ccac850f9ef88e6528433a90a9774d6d3bacff0c'
7
+ data.tar.gz: 22ac7b9564df2fef7f23b5227578713d095492f615e9fa652c0d1fb55167d1d9188fda65e4b683db8d413cb38086e27930d1dd6077975b088fd89dfbd1ac8114
data/README.md CHANGED
@@ -30,6 +30,7 @@
30
30
  - [📖 Documentation](#-documentation)
31
31
  - [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
32
32
  - [🚀 Getting Started](#-getting-started)
33
+ - [Compatibility](#-compatibility)
33
34
  - [⚙️ Settings](#️-settings)
34
35
  - [🔍 Custom search](#-custom-search)
35
36
  - [🪛 Options](#-options)
@@ -43,6 +44,7 @@
43
44
  - [Relations](#relations)
44
45
  - [Sanitize attributes](#sanitize-attributes)
45
46
  - [UTF-8 encoding](#utf-8-encoding)
47
+ - [Eager loading](#eager-loading)
46
48
  - [Manual operations](#manual-operations)
47
49
  - [Indexing & deletion](#indexing--deletion)
48
50
  - [Access the underlying index object](#access-the-underlying-index-object)
@@ -190,6 +192,10 @@ MeiliSearch::Rails.configuration = {
190
192
  }
191
193
  ```
192
194
 
195
+ ## Compatibility
196
+
197
+ If your model already has methods that meilisearch-rails defines such as `search` and `index`, they will not be redefined. You can target the meilisearch-rails-defined methods by prefixing with `ms_`, e.g. `Book.ms_search('harry potter')`.
198
+
193
199
  ## ⚙️ Settings
194
200
 
195
201
  You can configure the index settings by adding them inside the `meilisearch` block as shown below:
@@ -312,15 +318,33 @@ end
312
318
 
313
319
  By default, the primary key is based on your record's id. You can change this behavior by specifying the `primary_key:` option.
314
320
 
315
- Note that the primary key must have a **unique value**.
321
+ Note that the primary key must return a **unique value** otherwise your data could be overwritten.
316
322
 
317
323
  ```ruby
318
324
  class Book < ActiveRecord::Base
319
325
  include MeiliSearch::Rails
320
326
 
321
- meilisearch primary_key: 'ISBN'
327
+ meilisearch primary_key: :isbn # isbn is a column in your table definition.
328
+ end
329
+ ```
330
+
331
+ You can also set the `primary_key` as a method, this method will be evaluated in runtime, and its return
332
+ will be used as the reference to the document when Meilisearch needs it.
333
+
334
+ ```rb
335
+ class Book < ActiveRecord::Base
336
+ include MeiliSearch::Rails
337
+
338
+ meilisearch primary_key: :my_custom_ms_id
339
+
340
+ private
341
+
342
+ def my_custom_ms_id
343
+ "isbn_#{primary_key}" # ensure this return is unique, otherwise you'll lose data.
344
+ end
322
345
  end
323
346
  ```
347
+
324
348
  #### Conditional indexing
325
349
 
326
350
  You can control if a record must be indexed by using the `if:` or `unless:` options.<br>
@@ -600,6 +624,20 @@ class Book < ActiveRecord::Base
600
624
  end
601
625
  ```
602
626
 
627
+ #### Eager loading
628
+
629
+ You can eager load associations using `meilisearch_import` scope.
630
+
631
+ ```ruby
632
+ class Author < ActiveRecord::Base
633
+ include MeiliSearch::Rails
634
+
635
+ has_many :books
636
+
637
+ scope :meilisearch_import, -> { includes(:books) }
638
+ end
639
+ ```
640
+
603
641
  ### Manual operations
604
642
 
605
643
  #### Indexing & deletion
@@ -16,6 +16,7 @@ module MeiliSearch
16
16
  configuration[:meilisearch_host] || 'http://localhost:7700',
17
17
  configuration[:meilisearch_api_key],
18
18
  configuration.slice(:timeout, :max_retries)
19
+ .merge(client_agents: MeiliSearch::Rails.qualified_version)
19
20
  )
20
21
  end
21
22
  end
@@ -2,6 +2,10 @@
2
2
 
3
3
  module MeiliSearch
4
4
  module Rails
5
- VERSION = '0.7.0'
5
+ VERSION = '0.7.1'
6
+
7
+ def self.qualified_version
8
+ "Meilisearch Rails (v#{VERSION})"
9
+ end
6
10
  end
7
11
  end
@@ -233,9 +233,13 @@ module MeiliSearch
233
233
  def initialize(index_uid, raise_on_failure, options)
234
234
  client = MeiliSearch::Rails.client
235
235
  primary_key = options[:primary_key] || MeiliSearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
236
- client.create_index(index_uid, { primaryKey: primary_key })
237
- @index = client.index(index_uid)
238
236
  @raise_on_failure = raise_on_failure.nil? || raise_on_failure
237
+
238
+ SafeIndex.log_or_throw(nil, @raise_on_failure) do
239
+ client.create_index(index_uid, { primary_key: primary_key })
240
+ end
241
+
242
+ @index = client.index(index_uid)
239
243
  end
240
244
 
241
245
  ::MeiliSearch::Index.instance_methods(false).each do |m|
@@ -273,7 +277,7 @@ module MeiliSearch
273
277
 
274
278
  def self.log_or_throw(method, raise_on_failure, &block)
275
279
  yield
276
- rescue ::MeiliSearch::ApiError => e
280
+ rescue ::MeiliSearch::TimeoutError, ::MeiliSearch::ApiError => e
277
281
  raise e if raise_on_failure
278
282
 
279
283
  # log the error
@@ -609,9 +613,6 @@ module MeiliSearch
609
613
  # "processingTimeMs"=>0, "query"=>"iphone"}
610
614
  json = ms_raw_search(query, params)
611
615
 
612
- # Returns the ids of the hits: 13
613
- hit_ids = json['hits'].map { |hit| hit[ms_pk(meilisearch_options).to_s] }
614
-
615
616
  # condition_key gets the primary key of the document; looks for "id" on the options
616
617
  condition_key = if defined?(::Mongoid::Document) && include?(::Mongoid::Document)
617
618
  ms_primary_key_method.in
@@ -619,6 +620,23 @@ module MeiliSearch
619
620
  ms_primary_key_method
620
621
  end
621
622
 
623
+ # The condition_key must be a valid column otherwise, the `.where` below will not work
624
+ # Since we provide a way to customize the primary_key value, `ms_pk(meilisearch_options)` may not
625
+ # respond with a valid database column. The blocks below prevent that from happening.
626
+ has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
627
+ meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s)
628
+ else
629
+ meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
630
+ end
631
+
632
+ condition_key = meilisearch_options[:type].primary_key if has_virtual_column_as_pk
633
+
634
+ hit_ids = if has_virtual_column_as_pk
635
+ json['hits'].map { |hit| hit[condition_key] }
636
+ else
637
+ json['hits'].map { |hit| hit[ms_pk(meilisearch_options).to_s] }
638
+ end
639
+
622
640
  # meilisearch_options[:type] refers to the Model name (e.g. Product)
623
641
  # results_by_id creates a hash with the primaryKey of the document (id) as the key and doc itself as the value
624
642
  # {"13"=>#<Product id: 13, name: "iphone", href: "apple", tags: nil, type: nil,
@@ -827,7 +845,8 @@ module MeiliSearch
827
845
 
828
846
  def ms_find_in_batches(batch_size, &block)
829
847
  if (defined?(::ActiveRecord) && ancestors.include?(::ActiveRecord::Base)) || respond_to?(:find_in_batches)
830
- find_in_batches(batch_size: batch_size, &block)
848
+ scope = respond_to?(:meilisearch_import) ? meilisearch_import : all
849
+ scope.find_in_batches(batch_size: batch_size, &block)
831
850
  elsif defined?(::Sequel::Model) && self < Sequel::Model
832
851
  dataset.extension(:pagination).each_page(batch_size, &block)
833
852
  else
@@ -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'
37
+ s.add_dependency 'meilisearch', '~> 0.19.2'
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.0
4
+ version: 0.7.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-07-12 00:00:00.000000000 Z
11
+ date: 2022-08-01 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'
19
+ version: 0.19.2
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'
26
+ version: 0.19.2
27
27
  description: Meilisearch integration for Ruby on Rails. See https://github.com/meilisearch/meilisearch
28
28
  email: bonjour@meilisearch.com
29
29
  executables: []