diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7e33d7b1..44a59326 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -15,7 +15,7 @@ Metrics/BlockLength: # Offense count: 4 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 441 + Max: 442 # Offense count: 1 # Configuration parameters: Max, CountKeywordArgs. diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 2032787f..2de94164 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -185,6 +185,18 @@ def update_documents_in_batches!(documents, batch_size = 1000, primary_key = nil update_documents_in_batches(documents, batch_size, primary_key).each(&:await) end + # Update documents by function + # + # options - A Hash containing the function string and related options + # context: + # filter: + # function: + def update_documents_by_function(options) + response = http_post "/indexes/#{@uid}/documents/edit", options + + Models::Task.new(response, task_endpoint) + end + # Public: Delete documents from an index # # options: A Hash or an Array containing documents_ids or a hash with filter:. diff --git a/spec/meilisearch/index/documents_spec.rb b/spec/meilisearch/index/documents_spec.rb index d54d5a5f..962d833c 100644 --- a/spec/meilisearch/index/documents_spec.rb +++ b/spec/meilisearch/index/documents_spec.rb @@ -390,6 +390,30 @@ end end + describe '#update_documents_by_function' do + before do + index.add_documents(documents).await + index.update_filterable_attributes(['objectId']).await + end + + it 'updates documents by function' do + enable_edit_documents_by_function(true) + expect(index.document(1344)).to include('title' => 'The Hobbit') + expect(index.document(456)).to include('title' => 'Le Petit Prince') + + index.update_documents_by_function( + { + filter: 'objectId = 1344', + context: { extra: 'extended' }, + function: 'doc.title = `${doc.title.to_upper()} - ${context.extra}`' + } + ).await + + expect(index.document(1344)).to include('title' => 'THE HOBBIT - extended') + expect(index.document(456)).to include('title' => 'Le Petit Prince') + end + end + describe '#delete_document' do before { index.add_documents(documents).await } diff --git a/spec/support/experimental_feature_helpers.rb b/spec/support/experimental_feature_helpers.rb index c937cd83..15ab1f2c 100644 --- a/spec/support/experimental_feature_helpers.rb +++ b/spec/support/experimental_feature_helpers.rb @@ -8,6 +8,10 @@ def enable_vector_store(toggle) configure_feature('vectorStore', toggle) end + def enable_edit_documents_by_function(toggle) + configure_feature('editDocumentsByFunction', toggle) + end + private # @param [String] attribute_to_toggle