Skip to content

Commit 128f0ae

Browse files
committed
Add documents as JSON, NDJSON and CSV arrays
- Fix the aux method that merge request options. - Skip to_json transformation for these methods. - Add related tests - Doesn't add in batches. - Doesn't support key transformations from camelCase to snake_case
1 parent c5f33fb commit 128f0ae

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

lib/meilisearch/http_request.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ def build_default_options_headers(api_key = nil)
6666
}.compact
6767
end
6868

69-
def merge_options(defaults, added = {})
70-
merged = defaults.merge(added)
71-
merged[:headers].merge(added[:headers]) if added.key?(:headers)
72-
merged
69+
def merge_options(default_options, added_options = {})
70+
default_cloned_headers = default_options[:headers].clone
71+
merged_options = default_options.merge(added_options)
72+
merged_options[:headers] = default_cloned_headers.merge(added_options[:headers]) if added_options.key?(:headers)
73+
merged_options
7374
end
7475

7576
def remove_options_header(options, key)
76-
new_options = clone_options(options)
77-
new_options[:headers].tap { |headers| headers.delete(key) }
78-
new_options
77+
cloned_options = clone_options(options)
78+
cloned_options[:headers].tap { |headers| headers.delete(key) }
79+
cloned_options
7980
end
8081

8182
def clone_options(options)
@@ -97,7 +98,7 @@ def send_request(http_method, relative_path, query_params: nil, body: nil, optio
9798
end
9899

99100
def http_config(query_params, body, options)
100-
body = transform_attributes(body).to_json
101+
body = transform_attributes(body).to_json if options[:transform_body?] == true
101102
{
102103
headers: options[:headers],
103104
query: query_params,

lib/meilisearch/index.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ def add_documents!(documents, primary_key = nil)
8383
alias replace_documents! add_documents!
8484
alias add_or_replace_documents! add_documents!
8585

86+
def add_documents_json(documents, primary_key = nil)
87+
options = { headers: { 'Content-Type' => 'application/json' }, transform_body?: false }
88+
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
89+
end
90+
alias replace_documents_json add_documents_json
91+
alias add_or_replace_documents_json add_documents_json
92+
93+
def add_documents_ndjson(documents, primary_key = nil)
94+
options = { headers: { 'Content-Type' => 'application/x-ndjson' }, transform_body?: false }
95+
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
96+
end
97+
alias replace_documents_ndjson add_documents_ndjson
98+
alias add_or_replace_documents_ndjson add_documents_ndjson
99+
100+
def add_documents_csv(documents, primary_key = nil)
101+
options = { headers: { 'Content-Type' => 'text/csv' }, transform_body?: false }
102+
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
103+
end
104+
alias replace_documents_csv add_documents_csv
105+
alias add_or_replace_documents_csv add_documents_csv
106+
86107
def update_documents(documents, primary_key = nil)
87108
documents = [documents] if documents.is_a?(Hash)
88109
http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact

spec/meilisearch/index/documents_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,52 @@
2525
expect(index.documents.count).to eq(documents.count)
2626
end
2727

28+
it 'adds JSON documents (as a array of documents)' do
29+
documents = <<JSON
30+
[
31+
{ "objectId": 123, "title": "Pride and Prejudice", "comment": "A great book" },
32+
{ "objectId": 456, "title": "Le Petit Prince", "comment": "A french book" },
33+
{ "objectId": 1, "title": "Alice In Wonderland", "comment": "A weird book" },
34+
{ "objectId": 1344, "title": "The Hobbit", "comment": "An awesome book" },
35+
{ "objectId": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
36+
]
37+
JSON
38+
response = index.add_documents_json(documents)
39+
40+
expect(response).to be_a(Hash)
41+
expect(response).to have_key('updateId')
42+
index.wait_for_pending_update(response['updateId'])
43+
expect(index.documents.count).to eq(5)
44+
end
45+
46+
it 'adds NDJSON documents (as a array of documents)' do
47+
documents = <<NDJSON
48+
{ "objectId": 123, "title": "Pride and Prejudice", "comment": "A great book" }
49+
{ "objectId": 456, "title": "Le Petit Prince", "comment": "A french book" }
50+
{ "objectId": 1, "title": "Alice In Wonderland", "comment": "A weird book" }
51+
{ "objectId": 4, "title": "Harry Potter and the Half-Blood Prince", "comment": "The best book" }
52+
NDJSON
53+
response = index.add_documents_ndjson(documents)
54+
expect(response).to be_a(Hash)
55+
expect(response).to have_key('updateId')
56+
index.wait_for_pending_update(response['updateId'])
57+
expect(index.documents.count).to eq(4)
58+
end
59+
60+
it 'adds CSV documents (as a array of documents)' do
61+
documents = <<CSV
62+
"objectId:number","title:string","comment:string"
63+
"1239","Pride and Prejudice","A great book"
64+
"4569","Le Petit Prince","A french book"
65+
"49","Harry Potter and the Half-Blood Prince","The best book"
66+
CSV
67+
response = index.add_documents_csv(documents)
68+
expect(response).to be_a(Hash)
69+
expect(response).to have_key('updateId')
70+
index.wait_for_pending_update(response['updateId'])
71+
expect(index.documents.count).to eq(3)
72+
end
73+
2874
it 'adds documents in a batch (as a array of documents)' do
2975
response = index.add_documents_in_batches(documents, 5)
3076
expect(response).to be_a(Array)

0 commit comments

Comments
 (0)