Skip to content

Commit 7b641c5

Browse files
committed
Support updating documents with JSON, NDJSON, CSV
Previously, you could only *replace* documents with the add endpoints.
1 parent 80d8b6c commit 7b641c5

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

lib/meilisearch/index.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ def update_documents(documents, primary_key = nil)
145145
end
146146
alias add_or_update_documents update_documents
147147

148+
def update_documents_json(documents, primary_key = nil)
149+
options = { convert_body?: false }
150+
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
151+
152+
Models::Task.new(response, task_endpoint)
153+
end
154+
alias add_or_update_documents_json update_documents_json
155+
156+
def update_documents_ndjson(documents, primary_key = nil)
157+
options = { headers: { 'Content-Type' => 'application/x-ndjson' }, convert_body?: false }
158+
response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options
159+
160+
Models::Task.new(response, task_endpoint)
161+
end
162+
alias add_or_update_documents_ndjson update_documents_ndjson
163+
164+
def update_documents_csv(documents, primary_key = nil, delimiter = nil)
165+
options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }
166+
167+
response = http_put "/indexes/#{@uid}/documents", documents, {
168+
primaryKey: primary_key,
169+
csvDelimiter: delimiter
170+
}.compact, options
171+
172+
Models::Task.new(response, task_endpoint)
173+
end
174+
alias add_or_update_documents_csv add_documents_csv
175+
148176
def update_documents!(documents, primary_key = nil)
149177
Utils.soft_deprecate(
150178
'Index#update_documents!',

spec/meilisearch/index/documents_spec.rb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
let(:csv_docs) do
123123
<<~CSV
124124
"objectRef:number","title:string","comment:string"
125-
"1239","Pride and Prejudice","A great book"
125+
"123","Pride and Prejudice","A great book"
126126
"456","Le Petit Prince","A french book"
127127
"49","Harry Potter and the Half-Blood Prince","The best book"
128128
"55","The Three Body Problem","An interesting book"
@@ -133,7 +133,7 @@
133133
let(:csv_docs_custom_delim) do
134134
<<~CSV
135135
"objectRef:number"|"title:string"|"comment:string"
136-
"1239"|"Pride and Prejudice"|"A great book"
136+
"123"|"Pride and Prejudice"|"A great book"
137137
"456"|"Le Petit Prince"|"A french book"
138138
"49"|"Harry Potter and the Half-Blood Prince"|"The best book"
139139
"55"|"The Three Body Problem"|"An interesting book"
@@ -199,6 +199,66 @@
199199

200200
expect(index.documents['results']).to include(batch1_doc, batch2_doc)
201201
end
202+
203+
it '#update_documents_json' do
204+
index.add_documents_json(json_docs, 'objectRef').await
205+
edit = '[{ "objectRef": 123, "comment": "AN OLD BOOK" }]'
206+
index.update_documents_json(edit).await
207+
expect(index.documents['results']).to include(
208+
{
209+
'objectRef' => 123,
210+
'title' => 'Pride and Prejudice',
211+
'comment' => 'AN OLD BOOK'
212+
}
213+
)
214+
end
215+
216+
it '#update_documents_ndjson' do
217+
index.add_documents_ndjson(ndjson_docs, 'objectRef').await
218+
edit = '{ "objectRef": 123, "comment": "AN OLD BOOK" }'
219+
index.update_documents_ndjson(edit).await
220+
expect(index.documents['results']).to include(
221+
{
222+
'objectRef' => 123,
223+
'title' => 'Pride and Prejudice',
224+
'comment' => 'AN OLD BOOK'
225+
}
226+
)
227+
end
228+
229+
it '#update_documents_csv' do
230+
index.add_documents_csv(csv_docs, 'objectRef').await
231+
edit = <<~CSV
232+
"objectRef:number","comment:string"
233+
"123","AN OLD BOOK"
234+
CSV
235+
236+
index.update_documents_csv(edit).await
237+
expect(index.documents['results']).to include(
238+
{
239+
'objectRef' => 123,
240+
'title' => 'Pride and Prejudice',
241+
'comment' => 'AN OLD BOOK'
242+
}
243+
)
244+
end
245+
246+
it '#update_documents_csv with custom delimiter' do
247+
index.add_documents_csv(csv_docs, 'objectRef').await
248+
edit = <<~CSV
249+
"objectRef:number"|"comment:string"
250+
"123"|"AN OLD BOOK"
251+
CSV
252+
253+
index.update_documents_csv(edit, 'objectRef', '|').await
254+
expect(index.documents['results']).to include(
255+
{
256+
'objectRef' => 123,
257+
'title' => 'Pride and Prejudice',
258+
'comment' => 'AN OLD BOOK'
259+
}
260+
)
261+
end
202262
end
203263

204264
describe '#add_documents!' do

0 commit comments

Comments
 (0)