Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support batching and updating with JSON, NDJSON, and CSV #599

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-01-15 12:21:42 UTC using RuboCop version 1.69.2.
# on 2025-01-22 14:03:07 UTC using RuboCop version 1.69.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 67
# Offense count: 70
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Expand All @@ -15,9 +15,9 @@ Metrics/BlockLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 492
Max: 537

# Offense count: 1
# Offense count: 3
# Configuration parameters: Max, CountKeywordArgs.
Metrics/ParameterLists:
MaxOptionalParameters: 4
Expand Down
24 changes: 12 additions & 12 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def initialize(url, api_key = nil, options = {})
@headers = build_default_options_headers
end

def http_get(relative_path = '', query_params = {})
def http_get(relative_path = '', query_params = {}, options = {})
send_request(
proc { |path, config| self.class.get(path, config) },
relative_path,
config: {
query_params: query_params,
headers: remove_headers(@headers.dup, 'Content-Type'),
options: @options
headers: remove_headers(@headers.dup.merge(options[:headers] || {}), 'Content-Type'),
options: @options.merge(options)
}
)
end
Expand All @@ -47,40 +47,40 @@ def http_post(relative_path = '', body = nil, query_params = nil, options = {})
)
end

def http_put(relative_path = '', body = nil, query_params = nil)
def http_put(relative_path = '', body = nil, query_params = nil, options = {})
send_request(
proc { |path, config| self.class.put(path, config) },
relative_path,
config: {
query_params: query_params,
body: body,
headers: @headers,
options: @options
headers: @headers.dup.merge(options[:headers] || {}),
options: @options.merge(options)
}
)
end

def http_patch(relative_path = '', body = nil, query_params = nil)
def http_patch(relative_path = '', body = nil, query_params = nil, options = {})
send_request(
proc { |path, config| self.class.patch(path, config) },
relative_path,
config: {
query_params: query_params,
body: body,
headers: @headers,
options: @options
headers: @headers.dup.merge(options[:headers] || {}),
options: @options.merge(options)
}
)
end

def http_delete(relative_path = '', query_params = nil)
def http_delete(relative_path = '', query_params = nil, options = {})
send_request(
proc { |path, config| self.class.delete(path, config) },
relative_path,
config: {
query_params: query_params,
headers: remove_headers(@headers.dup, 'Content-Type'),
options: @options
headers: remove_headers(@headers.dup.merge(options[:headers] || {}), 'Content-Type'),
options: @options.merge(options)
}
)
end
Expand Down
56 changes: 56 additions & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,48 @@ def update_documents(documents, primary_key = nil)
end
alias add_or_update_documents update_documents

def update_documents_json(documents, primary_key = nil)
options = { convert_body?: false }
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

Models::Task.new(response, task_endpoint)
end
alias add_or_update_documents_json update_documents_json

def update_documents_ndjson(documents, primary_key = nil)
options = { headers: { 'Content-Type' => 'application/x-ndjson' }, convert_body?: false }
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

Models::Task.new(response, task_endpoint)
end
alias add_or_update_documents_ndjson update_documents_ndjson

def update_documents_csv(documents, primary_key = nil, delimiter = nil)
options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }

response = http_put "/indexes/#{@uid}/documents", documents, {
primaryKey: primary_key,
csvDelimiter: delimiter
}.compact, options

Models::Task.new(response, task_endpoint)
end
alias add_or_update_documents_csv add_documents_csv

def update_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
documents.lines.each_slice(batch_size).map do |batch|
update_documents_ndjson(batch.join, primary_key)
end
end

def update_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
lines = documents.lines
heading = lines.first
lines.drop(1).each_slice(batch_size).map do |batch|
update_documents_csv(heading + batch.join, primary_key, delimiter)
end
end

def update_documents!(documents, primary_key = nil)
Utils.soft_deprecate(
'Index#update_documents!',
Expand All @@ -161,6 +203,20 @@ def add_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
end
end

def add_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
documents.lines.each_slice(batch_size).map do |batch|
add_documents_ndjson(batch.join, primary_key)
end
end

def add_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
lines = documents.lines
heading = lines.first
lines.drop(1).each_slice(batch_size).map do |batch|
add_documents_csv(heading + batch.join, primary_key, delimiter)
end
end

def add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
Utils.soft_deprecate(
'Index#add_documents_in_batches!',
Expand Down
Loading
Loading