Skip to content

Commit

Permalink
Merge pull request #429 from meilisearch/add-csv-delimiter
Browse files Browse the repository at this point in the history
Add csv delimiter to `Index#add_documents_csv` method
  • Loading branch information
brunoocasali committed Mar 30, 2023
2 parents a486380 + ee293e0 commit 216ace2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-07-27 15:08:15 UTC using RuboCop version 1.32.0.
# on 2023-03-30 12:31:09 UTC using RuboCop version 1.48.1.
# 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: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Include.
# Configuration parameters: Severity, Include.
# Include: **/*.gemspec
Gemspec/RequireMFA:
Exclude:
- 'meilisearch.gemspec'

# Offense count: 46
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
# Offense count: 45
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 626
Max: 624

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 317
Max: 318

# Offense count: 1
# Configuration parameters: Max, CountKeywordArgs.
Expand Down
8 changes: 6 additions & 2 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ def add_documents_ndjson(documents, primary_key = nil)
alias replace_documents_ndjson add_documents_ndjson
alias add_or_replace_documents_ndjson add_documents_ndjson

def add_documents_csv(documents, primary_key = nil)
def add_documents_csv(documents, primary_key = nil, delimiter = nil)
options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

http_post "/indexes/#{@uid}/documents", documents, {
primaryKey: primary_key,
csvDelimiter: delimiter
}.compact, options
end
alias replace_documents_csv add_documents_csv
alias add_or_replace_documents_csv add_documents_csv
Expand Down
17 changes: 17 additions & 0 deletions spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@
expect(index.documents['results'].count).to eq(3)
end

it 'adds CSV documents (as an array of documents with a different separator)' do
documents = <<~CSV
"objectRef:number"|"title:string"|"comment:string"
"1239"|"Pride and Prejudice"|"A great book"
"4569"|"Le Petit Prince"|"A french book"
"49"|"Harry Potter and the Half-Blood Prince"|"The best book"
CSV

response = index.add_documents_csv(documents, 'objectRef', '|')
index.wait_for_task(response['taskUid'])

expect(index.documents['results'].count).to eq(3)
expect(index.documents['results'][1]['objectRef']).to eq(4569)
expect(index.documents['results'][1]['title']).to eq('Le Petit Prince')
expect(index.documents['results'][1]['comment']).to eq('A french book')
end

it 'adds documents in a batch (as a array of documents)' do
task = index.add_documents_in_batches(documents, 5)
expect(task).to be_a(Array)
Expand Down

0 comments on commit 216ace2

Please sign in to comment.