Skip to content

Commit 116f548

Browse files
committed
Introduce an option to update instead of replacing documents
1 parent 283d25b commit 116f548

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/main.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{fs, thread};
55

66
use anyhow::Context;
77
use byte_unit::Byte;
8-
use clap::Parser;
8+
use clap::{Parser, ValueEnum};
99
use exponential_backoff::Backoff;
1010
use flate2::write::GzEncoder;
1111
use flate2::Compression;
@@ -44,7 +44,7 @@ struct Opt {
4444
api_key: Option<String>,
4545

4646
/// A list of file paths that are streamed and sent to Meilisearch in batches.
47-
#[structopt(long)]
47+
#[structopt(long, num_args(1..))]
4848
files: Vec<PathBuf>,
4949

5050
/// The size of the batches sent to Meilisearch.
@@ -54,11 +54,28 @@ struct Opt {
5454
/// The number of batches to skip. Useful when the upload stopped for some reason.
5555
#[structopt(long)]
5656
skip_batches: Option<u64>,
57+
58+
/// The operation to perform when uploading a document.
59+
#[arg(
60+
long,
61+
value_name = "OPERATION",
62+
num_args = 0..=1,
63+
default_value_t = DocumentOperation::AddOrReplace,
64+
value_enum
65+
)]
66+
upload_operation: DocumentOperation,
67+
}
68+
69+
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
70+
enum DocumentOperation {
71+
AddOrReplace,
72+
AddOrUpdate,
5773
}
5874

5975
fn send_data(
6076
opt: &Opt,
6177
agent: &Agent,
78+
upload_operation: DocumentOperation,
6279
pb: &ProgressBar,
6380
mime: &Mime,
6481
data: &[u8],
@@ -79,7 +96,10 @@ fn send_data(
7996
let backoff = Backoff::new(retries, min, max);
8097

8198
for (attempt, duration) in backoff.into_iter().enumerate() {
82-
let mut request = agent.post(&url);
99+
let mut request = match upload_operation {
100+
DocumentOperation::AddOrReplace => agent.post(&url),
101+
DocumentOperation::AddOrUpdate => agent.put(&url),
102+
};
83103
request = request.set("Content-Type", mime.as_str());
84104
request = request.set("Content-Encoding", "gzip");
85105
request = request.set("X-Meilisearch-Client", "Meilisearch Importer");
@@ -128,22 +148,22 @@ fn main() -> anyhow::Result<()> {
128148
Mime::Json => {
129149
if opt.skip_batches.zip(pb.length()).map_or(true, |(s, l)| s > l) {
130150
let data = fs::read_to_string(file)?;
131-
send_data(&opt, &agent, &pb, &mime, data.as_bytes())?;
151+
send_data(&opt, &agent, opt.upload_operation, &pb, &mime, data.as_bytes())?;
132152
}
133153
pb.inc(1);
134154
}
135155
Mime::NdJson => {
136156
for chunk in nd_json::NdJsonChunker::new(file, size) {
137157
if opt.skip_batches.zip(pb.length()).map_or(true, |(s, l)| s > l) {
138-
send_data(&opt, &agent, &pb, &mime, &chunk)?;
158+
send_data(&opt, &agent, opt.upload_operation, &pb, &mime, &chunk)?;
139159
}
140160
pb.inc(1);
141161
}
142162
}
143163
Mime::Csv => {
144164
for chunk in csv::CsvChunker::new(file, size) {
145165
if opt.skip_batches.zip(pb.length()).map_or(true, |(s, l)| s > l) {
146-
send_data(&opt, &agent, &pb, &mime, &chunk)?;
166+
send_data(&opt, &agent, opt.upload_operation, &pb, &mime, &chunk)?;
147167
}
148168
pb.inc(1);
149169
}

0 commit comments

Comments
 (0)