From 16589b5091de23e0f2773df3ac65adf64052b990 Mon Sep 17 00:00:00 2001 From: Edoardo Morandi Date: Sat, 1 Jan 2022 14:58:59 +0100 Subject: [PATCH] feat!: make id optional for BulkCreateOperation --- elasticsearch/src/root/bulk.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/elasticsearch/src/root/bulk.rs b/elasticsearch/src/root/bulk.rs index 0cd80502..8e4d9fb2 100644 --- a/elasticsearch/src/root/bulk.rs +++ b/elasticsearch/src/root/bulk.rs @@ -162,11 +162,8 @@ where B: Serialize, { /// Creates a new instance of a [bulk create operation](BulkCreateOperation) - pub fn create(id: S, source: B) -> BulkCreateOperation - where - S: Into, - { - BulkCreateOperation::new(id, source) + pub fn create(source: B) -> BulkCreateOperation { + BulkCreateOperation::new(source) } /// Creates a new instance of a [bulk index operation](BulkIndexOperation) @@ -217,24 +214,30 @@ pub struct BulkCreateOperation { impl BulkCreateOperation { /// Creates a new instance of [BulkCreateOperation] - pub fn new(id: S, source: B) -> Self - where - S: Into, - { + pub fn new(source: B) -> Self { Self { operation: BulkOperation { header: BulkHeader { action: BulkAction::Create, - metadata: BulkMetadata { - _id: Some(id.into()), - ..Default::default() - }, + metadata: BulkMetadata::default(), }, source: Some(source), }, } } + /// Specify the id for the document + /// + /// If an id is not specified, Elasticsearch will generate an id for the document + /// which will be returned in the response. + pub fn id(mut self, id: S) -> Self + where + S: Into, + { + self.operation.header.metadata._id = Some(id.into()); + self + } + /// Specify the name of the index to perform the bulk update operation against. /// /// Each bulk operation can specify an index to operate against. If all bulk operations @@ -697,7 +700,8 @@ mod tests { .into(), ); ops.push( - BulkOperation::create("2", json!({ "bar": "create" })) + BulkOperation::create(json!({ "bar": "create" })) + .id("2") .pipeline("pipeline") .routing("routing") .index("create_index") @@ -779,7 +783,7 @@ mod tests { .index("index_doc") .routing("routing"), )?; - ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?; + ops.push(BulkOperation::create(CreateDoc { bar: "create" }).id("2"))?; ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?; ops.push(BulkOperation::<()>::delete("4"))?;