Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <!-- omit in toc -->

Expand Down Expand Up @@ -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 <!-- omit in toc -->

Expand Down
27 changes: 14 additions & 13 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
36 changes: 14 additions & 22 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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'))
Expand Down