Skip to content

Commit

Permalink
Merge #568
Browse files Browse the repository at this point in the history
568: Added update documents by function r=brunoocasali a=Nymuxyzo

# Pull Request

## Related issue
Fixes #559

## What does this PR do?
Implementation of "Update documents by function"

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?


Co-authored-by: Andre <>
Co-authored-by: Clémentine <[email protected]>
  • Loading branch information
meili-bors[bot] and curquiza authored Dec 11, 2024
2 parents 61aa49a + e20c789 commit 9106f5d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:.
Expand Down
24 changes: 24 additions & 0 deletions spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
4 changes: 4 additions & 0 deletions spec/support/experimental_feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9106f5d

Please sign in to comment.