diff --git a/Gemfile b/Gemfile index d079214f..c0fe23b4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'http://rubygems.org' gem 'json', '~> 2.5', '>= 2.5.1' -gem 'meilisearch', '~> 0.17.0' +gem 'meilisearch', '~> 0.18.0' gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx' diff --git a/README.md b/README.md index 0ee17214..db1eaac9 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ To learn more about MeiliSearch, check out our [Documentation](https://docs.meil ## 🤖 Compatibility with MeiliSearch -This package only guarantees the compatibility with the [version v0.24.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.24.0). +This package only guarantees the compatibility with the [version v0.25.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.25.0). ## 🔧 Installation @@ -659,7 +659,7 @@ class Book < ActiveRecord::Base end end ``` -🚨 This is only recommended for testing purposes, the gem will call the `wait_for_pending_update` method that will stop your code execution until the asynchronous task has been processed by MeilSearch. +🚨 This is only recommended for testing purposes, the gem will call the `wait_for_task` method that will stop your code execution until the asynchronous task has been processed by MeilSearch. ##### Disable auto-indexing & auto-removal diff --git a/lib/meilisearch-rails.rb b/lib/meilisearch-rails.rb index 0b9ffbaf..fe5d83fe 100644 --- a/lib/meilisearch-rails.rb +++ b/lib/meilisearch-rails.rb @@ -237,7 +237,8 @@ class SafeIndex def initialize(index_uid, raise_on_failure, options) client = MeiliSearch.client primary_key = options[:primary_key] || MeiliSearch::IndexSettings::DEFAULT_PRIMARY_KEY - @index = client.get_or_create_index(index_uid, { primaryKey: primary_key }) + client.create_index(index_uid, { primaryKey: primary_key }) + @index = client.index(index_uid) @raise_on_failure = raise_on_failure.nil? || raise_on_failure end @@ -254,12 +255,12 @@ def initialize(index_uid, raise_on_failure, options) end end - # special handling of wait_for_pending_update to handle null task_id - def wait_for_pending_update(update_id) - return if update_id.nil? && !@raise_on_failure # ok + # special handling of wait_for_task to handle null task_id + def wait_for_task(task_uid) + return if task_uid.nil? && !@raise_on_failure # ok - SafeIndex.log_or_throw(:wait_for_pending_update, @raise_on_failure) do - @index.wait_for_pending_update(update_id) + SafeIndex.log_or_throw(:wait_for_task, @raise_on_failure) do + @index.wait_for_task(task_uid) end end @@ -442,7 +443,7 @@ def ms_reindex!(batch_size = MeiliSearch::IndexSettings::DEFAULT_BATCH_SIZE, syn next if ms_indexing_disabled?(options) index = ms_ensure_init(options, settings) - last_update = nil + last_task = nil ms_find_in_batches(batch_size) do |group| if ms_conditional_index?(options) @@ -457,9 +458,9 @@ def ms_reindex!(batch_size = MeiliSearch::IndexSettings::DEFAULT_BATCH_SIZE, syn attributes = attributes.to_hash unless attributes.instance_of?(Hash) attributes.merge ms_pk(options) => ms_primary_key_of(d, options) end - last_update= index.add_documents(documents) + last_task = index.add_documents(documents) end - index.wait_for_pending_update(last_update['updateId']) if last_update && (synchronous || options[:synchronous]) + index.wait_for_task(last_task['uid']) if last_task && (synchronous || options[:synchronous]) end nil end @@ -474,8 +475,8 @@ def ms_set_settings(synchronous = false) end index = SafeIndex.new(ms_index_uid(options), true, options) - update = index.update_settings(final_settings) - index.wait_for_pending_update(update['updateId']) if synchronous + task = index.update_settings(final_settings) + index.wait_for_task(task['uid']) if synchronous end end @@ -484,8 +485,8 @@ def ms_index_documents(documents, synchronous = false) next if ms_indexing_disabled?(options) index = ms_ensure_init(options, settings) - update = index.add_documents(documents.map { |d| settings.get_attributes(d).merge ms_pk(options) => ms_primary_key_of(d, options) }) - index.wait_for_pending_update(update['updateId']) if synchronous || options[:synchronous] + task = index.add_documents(documents.map { |d| settings.get_attributes(d).merge ms_pk(options) => ms_primary_key_of(d, options) }) + index.wait_for_task(task['uid']) if synchronous || options[:synchronous] end end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 406f3a12..23be6066 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -1248,6 +1248,12 @@ class SerializedDocument < ActiveRecord::Base end describe 'People' do + it 'adds custom complex attribute' do + People.create(first_name: 'Jane', last_name: 'Doe', card_number: 75_801_887) + result = People.raw_search('Jane') + expect(result['hits'][0]['full_name']).to eq('Jane Doe') + end + it 'has as uid the custom name specified' do expect(People.index.uid).to eq(safe_index_uid('MyCustomPeople')) end @@ -1257,26 +1263,12 @@ class SerializedDocument < ActiveRecord::Base expect(index.primary_key).to eq('card_number') end - it 'adds custom complex attribute' do - People.create(first_name: 'Jane', last_name: 'Doe', card_number: 75_801_887) - result = People.raw_search('Jane') - expect(result['hits'][0]['full_name']).to eq('Jane Doe') - end - it 'does not call the API if there has been no attribute change' do - person = People.search('Jane')[0] - before_save_statuses = People.index.get_all_update_status - before_save_status = before_save_statuses.last - person.first_name = 'Jane' - person.save - after_save_statuses = People.index.get_all_update_status - after_save_status = after_save_statuses.last - expect(before_save_status['updateId']).to eq(after_save_status['updateId']) - person.first_name = 'Alice' - person.save - after_change_statuses = People.index.get_all_update_status - after_change_status = after_change_statuses.last - expect(before_save_status['updateId']).not_to eq(after_change_status['updateId']) + person = People.search('Jane').first + + expect do + person.update(first_name: 'Jane') + end.not_to change(People.index.tasks['results'], :size) end it 'does not auto-remove' do @@ -1310,7 +1302,7 @@ class SerializedDocument < ActiveRecord::Base Dog.create!(name: 'Toby') Cat.create!(name: 'Felix') index = MeiliSearch.client.index(safe_index_uid('animals')) - index.wait_for_pending_update(index.get_all_update_status.last['updateId']) + index.wait_for_task(index.tasks['results'].first['uid']) docs = index.search('') expect(docs['hits'].size).to eq(2) end @@ -1321,8 +1313,8 @@ class SerializedDocument < ActiveRecord::Base Song.create!(name: 'Coconut nut', artist: 'Smokey Mountain', premium: false, released: true) # Only song supposed to be added to Songs index Song.create!(name: 'Smoking hot', artist: 'Cigarettes before lunch', premium: true, released: true) Song.create!(name: 'Floor is lava', artist: 'Volcano', premium: true, released: false) - Song.index.wait_for_pending_update(Song.index.get_all_update_status.last['updateId']) - MeiliSearch.client.index(safe_index_uid('PrivateSongs')).wait_for_pending_update(MeiliSearch.client.index(safe_index_uid('PrivateSongs')).get_all_update_status.last['updateId']) + Song.index.wait_for_task(Song.index.tasks['results'].first['uid']) + MeiliSearch.client.index(safe_index_uid('PrivateSongs')).wait_for_task(MeiliSearch.client.index(safe_index_uid('PrivateSongs')).tasks['results'].first['uid']) results = Song.search('', index: safe_index_uid('Songs')) expect(results.size).to eq(1) raw_results = Song.raw_search('', index: safe_index_uid('Songs'))