diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index e9f430a9..12a28d59 100644 --- a/opensearch/src/root/bulk.rs +++ b/opensearch/src/root/bulk.rs @@ -172,7 +172,7 @@ where B: Serialize, { /// Creates a new instance of a [bulk create operation](BulkCreateOperation) - pub fn create(id: S, source: B) -> BulkCreateOperation + pub fn create(id: Option, source: B) -> BulkCreateOperation where S: Into, { @@ -180,8 +180,11 @@ where } /// Creates a new instance of a [bulk index operation](BulkIndexOperation) - pub fn index(source: B) -> BulkIndexOperation { - BulkIndexOperation::new(source) + pub fn index(id: Option, source: B) -> BulkIndexOperation + where + S: Into, + { + BulkIndexOperation::new(id, source) } /// Creates a new instance of a [bulk delete operation](BulkDeleteOperation) @@ -227,7 +230,7 @@ pub struct BulkCreateOperation { impl BulkCreateOperation { /// Creates a new instance of [BulkCreateOperation] - pub fn new(id: S, source: B) -> Self + pub fn new(id: Option, source: B) -> Self where S: Into, { @@ -236,7 +239,10 @@ impl BulkCreateOperation { header: BulkHeader { action: BulkAction::Create, metadata: BulkMetadata { - _id: Some(id.into()), + _id: match id { + Some(_id) => Some(_id.into()), + None => None, + }, ..Default::default() }, }, @@ -291,12 +297,19 @@ pub struct BulkIndexOperation { impl BulkIndexOperation { /// Creates a new instance of [BulkIndexOperation] - pub fn new(source: B) -> Self { + pub fn new(id: Option, source: B) -> Self + where + S: Into, + { Self { operation: BulkOperation { header: BulkHeader { action: BulkAction::Index, metadata: BulkMetadata { + _id: match id { + Some(_id) => Some(_id.into()), + None => None, + }, ..Default::default() }, }, @@ -696,8 +709,7 @@ mod tests { let mut ops: Vec> = Vec::with_capacity(4); ops.push( - BulkOperation::index(json!({ "foo": "index" })) - .id("1") + BulkOperation::index(Some("1"), json!({ "foo": "index" })) .pipeline("pipeline") .routing("routing") .if_seq_no(1) @@ -707,7 +719,7 @@ mod tests { .into(), ); ops.push( - BulkOperation::create("2", json!({ "bar": "create" })) + BulkOperation::create(Some("2"), json!({ "bar": "create" })) .pipeline("pipeline") .routing("routing") .index("create_index") @@ -782,13 +794,19 @@ mod tests { let mut ops = BulkOperations::new(); ops.push( - BulkOperation::index(IndexDoc { foo: "index" }) - .id("1") + BulkOperation::index(Some("1"), IndexDoc { foo: "index" }) .pipeline("pipeline") .index("index_doc") .routing("routing"), )?; - ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?; + ops.push(BulkOperation::create( + Some("2"), + CreateDoc { bar: "create" }, + ))?; + ops.push(BulkOperation::create( + None::, + CreateDoc { bar: "create" }, + ))?; ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?; ops.push(BulkOperation::<()>::delete("4"))?; @@ -800,6 +818,8 @@ mod tests { expected.put_slice(b"{\"foo\":\"index\"}\n"); expected.put_slice(b"{\"create\":{\"_id\":\"2\"}}\n"); expected.put_slice(b"{\"bar\":\"create\"}\n"); + expected.put_slice(b"{\"create\":{}}\n"); + expected.put_slice(b"{\"bar\":\"create\"}\n"); expected.put_slice(b"{\"update\":{\"_id\":\"3\"}}\n"); expected.put_slice(b"{\"baz\":\"update\"}\n"); expected.put_slice(b"{\"delete\":{\"_id\":\"4\"}}\n"); diff --git a/opensearch/tests/common/client.rs b/opensearch/tests/common/client.rs index c5935cf7..6e438182 100644 --- a/opensearch/tests/common/client.rs +++ b/opensearch/tests/common/client.rs @@ -151,9 +151,8 @@ pub async fn index_documents(client: &OpenSearch) -> Result { if exists_response.status_code() == StatusCode::NOT_FOUND { let mut body: Vec> = vec![]; for i in 1..=10 { - let op = BulkOperation::index(json!({"title":"OpenSearch"})) - .id(i.to_string()) - .into(); + let op = + BulkOperation::index(Some(i.to_string()), json!({"title":"OpenSearch"})).into(); body.push(op); }