Skip to content

Commit 216ace2

Browse files
authored
Merge pull request #429 from meilisearch/add-csv-delimiter
Add csv delimiter to `Index#add_documents_csv` method
2 parents a486380 + ee293e0 commit 216ace2

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

.rubocop_todo.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2022-07-27 15:08:15 UTC using RuboCop version 1.32.0.
3+
# on 2023-03-30 12:31:09 UTC using RuboCop version 1.48.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

99
# Offense count: 1
1010
# This cop supports safe autocorrection (--autocorrect).
11-
# Configuration parameters: Include.
11+
# Configuration parameters: Severity, Include.
1212
# Include: **/*.gemspec
1313
Gemspec/RequireMFA:
1414
Exclude:
1515
- 'meilisearch.gemspec'
1616

17-
# Offense count: 46
18-
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
19-
# IgnoredMethods: refine
17+
# Offense count: 45
18+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
19+
# AllowedMethods: refine
2020
Metrics/BlockLength:
21-
Max: 626
21+
Max: 624
2222

2323
# Offense count: 2
2424
# Configuration parameters: CountComments, CountAsOne.
2525
Metrics/ClassLength:
26-
Max: 317
26+
Max: 318
2727

2828
# Offense count: 1
2929
# Configuration parameters: Max, CountKeywordArgs.

lib/meilisearch/index.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ def add_documents_ndjson(documents, primary_key = nil)
9696
alias replace_documents_ndjson add_documents_ndjson
9797
alias add_or_replace_documents_ndjson add_documents_ndjson
9898

99-
def add_documents_csv(documents, primary_key = nil)
99+
def add_documents_csv(documents, primary_key = nil, delimiter = nil)
100100
options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }
101-
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
101+
102+
http_post "/indexes/#{@uid}/documents", documents, {
103+
primaryKey: primary_key,
104+
csvDelimiter: delimiter
105+
}.compact, options
102106
end
103107
alias replace_documents_csv add_documents_csv
104108
alias add_or_replace_documents_csv add_documents_csv

spec/meilisearch/index/documents_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@
7878
expect(index.documents['results'].count).to eq(3)
7979
end
8080

81+
it 'adds CSV documents (as an array of documents with a different separator)' do
82+
documents = <<~CSV
83+
"objectRef:number"|"title:string"|"comment:string"
84+
"1239"|"Pride and Prejudice"|"A great book"
85+
"4569"|"Le Petit Prince"|"A french book"
86+
"49"|"Harry Potter and the Half-Blood Prince"|"The best book"
87+
CSV
88+
89+
response = index.add_documents_csv(documents, 'objectRef', '|')
90+
index.wait_for_task(response['taskUid'])
91+
92+
expect(index.documents['results'].count).to eq(3)
93+
expect(index.documents['results'][1]['objectRef']).to eq(4569)
94+
expect(index.documents['results'][1]['title']).to eq('Le Petit Prince')
95+
expect(index.documents['results'][1]['comment']).to eq('A french book')
96+
end
97+
8198
it 'adds documents in a batch (as a array of documents)' do
8299
task = index.add_documents_in_batches(documents, 5)
83100
expect(task).to be_a(Array)

0 commit comments

Comments
 (0)