meilisearch-rails 0.10.2 → 0.11.0

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: cb990c26e799658b655df059ea7a22b4cfc7511a88e7581d72c27841b736f74f
4
- data.tar.gz: c2457a258356732877729a9f110a8f392316a98d725d6d052f2dc64824a3ea2f
3
+ metadata.gz: 215428bdb5e437c3a906e1246444dc7e2afce9d2b52646790de1d55d0490783f
4
+ data.tar.gz: 412f8d56c952a330a9b3473602aca9ac0fee6789a3a3632f65340c7c85524637
5
5
  SHA512:
6
- metadata.gz: 7499509091a149069f1322e3ceec9c789432c3495d8c2754d258bb17c5cc1d70a410ebcd41e1eb76bcbe50b3c9e301044d0a5e6931e94bdd8779eb290e9eaecf
7
- data.tar.gz: 68173b7d7767308144b6987c35c9c2fb75806959a6fcdc3ee3545cab88779d24e013dea0ea0b3b0a3182f8fb9bcd5c7e1634aaf89ecd39f4cb7ad21231b95a0b
6
+ metadata.gz: ad8f9e4489c11dd3b2a522b134921232fbe2b66946f57ebf6be0c18ccd0643490379fb483d0a2ecf3371a427ff442809801130308e8f11fdedc7d6869a8d14c5
7
+ data.tar.gz: 5473ca9c267a3dfada14258db095d86c2e3618d356afc29ecf60c6975b067078cac91d8cc84c3e0b0bf8b5f227496d95830d77ed41923d0deeb313165b0b37df
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021-2022 Meili SAS
3
+ Copyright (c) 2021-2024 Meili SAS
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,19 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ class MSCleanUpJob < ::ActiveJob::Base
4
+ queue_as :meilisearch
5
+
6
+ def perform(documents)
7
+ documents.each do |document|
8
+ index = MeiliSearch::Rails.client.index(document[:index_uid])
9
+
10
+ if document[:synchronous]
11
+ index.delete_document!(document[:primary_key])
12
+ else
13
+ index.delete_document(document[:primary_key])
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -18,7 +18,7 @@ module MeiliSearch
18
18
 
19
19
  def self.log_pagy_error
20
20
  MeiliSearch::Rails.logger
21
- .warning('[meilisearch-rails] Remove `pagination_backend: :pagy` from your initializer, `pagy` it is not required for `pagy`')
21
+ .warn('[meilisearch-rails] Remove `pagination_backend: :pagy` from your initializer, `pagy` it is not required for `pagy`')
22
22
  end
23
23
 
24
24
  def self.load_pagination!(pagination_backend, results, total_hits, options)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module MeiliSearch
4
4
  module Rails
5
- VERSION = '0.10.2'
5
+ VERSION = '0.11.0'
6
6
 
7
7
  def self.qualified_version
8
8
  "Meilisearch Rails (v#{VERSION})"
@@ -249,6 +249,7 @@ module MeiliSearch
249
249
  # lazy load the ActiveJob class to ensure the
250
250
  # queue is initialized before using it
251
251
  autoload :MSJob, 'meilisearch/rails/ms_job'
252
+ autoload :MSCleanUpJob, 'meilisearch/rails/ms_clean_up_job'
252
253
  end
253
254
 
254
255
  # this class wraps an MeiliSearch::Index document ensuring all raised exceptions
@@ -260,7 +261,7 @@ module MeiliSearch
260
261
  @raise_on_failure = raise_on_failure.nil? || raise_on_failure
261
262
 
262
263
  SafeIndex.log_or_throw(nil, @raise_on_failure) do
263
- client.create_index(index_uid, { primary_key: primary_key })
264
+ client.create_index!(index_uid, { primary_key: primary_key })
264
265
  end
265
266
 
266
267
  @index = client.index(index_uid)
@@ -282,6 +283,15 @@ module MeiliSearch
282
283
  end
283
284
  end
284
285
 
286
+ # Maually define facet_search due to complications with **opts in ruby 2.*
287
+ def facet_search(*args, **opts)
288
+ SafeIndex.log_or_throw(:facet_search, @raise_on_failure) do
289
+ return MeiliSearch::Rails.black_hole unless MeiliSearch::Rails.active?
290
+
291
+ @index.facet_search(*args, **opts)
292
+ end
293
+ end
294
+
285
295
  # special handling of wait_for_task to handle null task_id
286
296
  def wait_for_task(task_uid)
287
297
  return if task_uid.nil? && !@raise_on_failure # ok
@@ -373,7 +383,11 @@ module MeiliSearch
373
383
 
374
384
  proc = if options[:enqueue] == true
375
385
  proc do |record, remove|
376
- MSJob.perform_later(record, remove ? 'ms_remove_from_index!' : 'ms_index!')
386
+ if remove
387
+ MSCleanUpJob.perform_later(record.ms_entries)
388
+ else
389
+ MSJob.perform_later(record, 'ms_index!')
390
+ end
377
391
  end
378
392
  elsif options[:enqueue].respond_to?(:call)
379
393
  options[:enqueue]
@@ -445,7 +459,7 @@ module MeiliSearch
445
459
  end
446
460
  end
447
461
  elsif respond_to?(:after_destroy)
448
- after_destroy { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
462
+ after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
449
463
  end
450
464
  end
451
465
 
@@ -526,7 +540,8 @@ module MeiliSearch
526
540
  def ms_index!(document, synchronous = false)
527
541
  return if ms_without_auto_index_scope
528
542
 
529
- ms_configurations.each do |options, settings|
543
+ # MS tasks to be returned
544
+ ms_configurations.map do |options, settings|
530
545
  next if ms_indexing_disabled?(options)
531
546
 
532
547
  primary_key = ms_primary_key_of(document, options)
@@ -550,8 +565,20 @@ module MeiliSearch
550
565
  index.delete_document(primary_key)
551
566
  end
552
567
  end
568
+ end.compact
569
+ end
570
+
571
+ def ms_entries_for(document:, synchronous:)
572
+ primary_key = ms_primary_key_of(document)
573
+ raise ArgumentError, 'Cannot index a record without a primary key' if primary_key.blank?
574
+
575
+ ms_configurations.filter_map do |options, settings|
576
+ {
577
+ synchronous: synchronous || options[:synchronous],
578
+ index_uid: options[:index_uid],
579
+ primary_key: primary_key
580
+ }.with_indifferent_access unless ms_indexing_disabled?(options)
553
581
  end
554
- nil
555
582
  end
556
583
 
557
584
  def ms_remove_from_index!(document, synchronous = false)
@@ -735,33 +762,38 @@ module MeiliSearch
735
762
 
736
763
  protected
737
764
 
738
- def ms_ensure_init(options = nil, settings = nil, index_settings = nil)
765
+ def ms_ensure_init(options = meilisearch_options, settings = meilisearch_settings, user_configuration = settings.to_settings)
739
766
  raise ArgumentError, 'No `meilisearch` block found in your model.' if meilisearch_settings.nil?
740
767
 
741
768
  @ms_indexes ||= { true => {}, false => {} }
742
769
 
743
- options ||= meilisearch_options
744
- settings ||= meilisearch_settings
770
+ @ms_indexes[MeiliSearch::Rails.active?][settings] ||= SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
745
771
 
746
- return @ms_indexes[MeiliSearch::Rails.active?][settings] if @ms_indexes[MeiliSearch::Rails.active?][settings]
772
+ update_settings_if_changed(@ms_indexes[MeiliSearch::Rails.active?][settings], options, user_configuration)
747
773
 
748
- @ms_indexes[MeiliSearch::Rails.active?][settings] = SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
774
+ @ms_indexes[MeiliSearch::Rails.active?][settings]
775
+ end
749
776
 
750
- current_settings = @ms_indexes[MeiliSearch::Rails.active?][settings].settings(getVersion: 1) rescue nil # if the index doesn't exist
777
+ private
751
778
 
752
- index_settings ||= settings.to_settings
753
- index_settings = options[:primary_settings].to_settings.merge(index_settings) if options[:inherit]
779
+ def update_settings_if_changed(index, options, user_configuration)
780
+ server_state = index.settings
781
+ user_configuration = options[:primary_settings].to_settings.merge(user_configuration) if options[:inherit]
754
782
 
755
- options[:check_settings] = true if options[:check_settings].nil?
783
+ config = user_configuration.except(:attributes_to_highlight, :attributes_to_crop, :crop_length)
756
784
 
757
- if !ms_indexing_disabled?(options) && options[:check_settings] && meilisearch_settings_changed?(current_settings, index_settings)
758
- @ms_indexes[MeiliSearch::Rails.active?][settings].update_settings(index_settings)
785
+ if !skip_checking_settings?(options) && meilisearch_settings_changed?(server_state, config)
786
+ index.update_settings(user_configuration)
759
787
  end
788
+ end
760
789
 
761
- @ms_indexes[MeiliSearch::Rails.active?][settings]
790
+ def skip_checking_settings?(options)
791
+ ms_indexing_disabled?(options) || ms_checking_disabled?(options)
762
792
  end
763
793
 
764
- private
794
+ def ms_checking_disabled?(options)
795
+ options[:check_settings] == false
796
+ end
765
797
 
766
798
  def ms_configurations
767
799
  raise ArgumentError, 'No `meilisearch` block found in your model.' if meilisearch_settings.nil?
@@ -800,19 +832,22 @@ module MeiliSearch
800
832
  options[:primary_key] || MeiliSearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
801
833
  end
802
834
 
803
- def meilisearch_settings_changed?(prev, current)
804
- return true if prev.nil?
835
+ def meilisearch_settings_changed?(server_state, user_configuration)
836
+ return true if server_state.nil?
805
837
 
806
- current.each do |k, v|
807
- prev_v = prev[k.to_s]
808
- if v.is_a?(Array) && prev_v.is_a?(Array)
809
- # compare array of strings, avoiding symbols VS strings comparison
810
- return true if v.map(&:to_s) != prev_v.map(&:to_s)
811
- elsif prev_v != v
812
- return true
838
+ user_configuration.transform_keys! { |key| key.to_s.camelize(:lower) }
839
+
840
+ user_configuration.any? do |key, user|
841
+ server = server_state[key]
842
+
843
+ if user.is_a?(Hash) && server.is_a?(Hash)
844
+ meilisearch_settings_changed?(server, user)
845
+ elsif user.is_a?(Array) && server.is_a?(Array)
846
+ user.map(&:to_s) != server.map(&:to_s)
847
+ else
848
+ user.to_s != server.to_s
813
849
  end
814
850
  end
815
- false
816
851
  end
817
852
 
818
853
  def ms_conditional_index?(options = nil)
@@ -923,6 +958,10 @@ module MeiliSearch
923
958
  @ms_synchronous
924
959
  end
925
960
 
961
+ def ms_entries(synchronous = false)
962
+ self.class.ms_entries_for(document: self, synchronous: synchronous || ms_synchronous?)
963
+ end
964
+
926
965
  private
927
966
 
928
967
  def ms_mark_synchronous
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.10.2
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-11 00:00:00.000000000 Z
11
+ date: 2024-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: meilisearch
@@ -40,6 +40,7 @@ files:
40
40
  - lib/meilisearch-rails.rb
41
41
  - lib/meilisearch/rails/configuration.rb
42
42
  - lib/meilisearch/rails/errors.rb
43
+ - lib/meilisearch/rails/ms_clean_up_job.rb
43
44
  - lib/meilisearch/rails/ms_job.rb
44
45
  - lib/meilisearch/rails/null_object.rb
45
46
  - lib/meilisearch/rails/pagination.rb