From 2dc360c08d81cecd11265b04c15e710c4dd2626d Mon Sep 17 00:00:00 2001 From: Cameron Durham Date: Wed, 3 Apr 2024 01:56:42 -0700 Subject: [PATCH 1/5] Use Option when id is optional in bulk request Signed-off-by: Cameron Durham --- opensearch/src/root/bulk.rs | 38 +++++++++++++++++++++---------- opensearch/tests/common/client.rs | 5 ++-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index e9f430a9..f0ec5fb8 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,7 @@ impl BulkCreateOperation { header: BulkHeader { action: BulkAction::Create, metadata: BulkMetadata { - _id: Some(id.into()), + _id: id.map(|_id| _id.into()), ..Default::default() }, }, @@ -291,12 +294,16 @@ 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: id.map(|_id| _id.into()), ..Default::default() }, }, @@ -696,8 +703,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 +713,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 +788,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 +812,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); } From 2a3f725f1243fc228ec976cd669128d16b1632c2 Mon Sep 17 00:00:00 2001 From: Cameron Durham Date: Sat, 6 Apr 2024 02:27:20 -0700 Subject: [PATCH 2/5] Revert "Use Option when id is optional in bulk request" Signed-off-by: Cameron Durham --- opensearch/src/root/bulk.rs | 38 ++++++++++--------------------- opensearch/tests/common/client.rs | 5 ++-- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index f0ec5fb8..e9f430a9 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: Option, source: B) -> BulkCreateOperation + pub fn create(id: S, source: B) -> BulkCreateOperation where S: Into, { @@ -180,11 +180,8 @@ where } /// Creates a new instance of a [bulk index operation](BulkIndexOperation) - pub fn index(id: Option, source: B) -> BulkIndexOperation - where - S: Into, - { - BulkIndexOperation::new(id, source) + pub fn index(source: B) -> BulkIndexOperation { + BulkIndexOperation::new(source) } /// Creates a new instance of a [bulk delete operation](BulkDeleteOperation) @@ -230,7 +227,7 @@ pub struct BulkCreateOperation { impl BulkCreateOperation { /// Creates a new instance of [BulkCreateOperation] - pub fn new(id: Option, source: B) -> Self + pub fn new(id: S, source: B) -> Self where S: Into, { @@ -239,7 +236,7 @@ impl BulkCreateOperation { header: BulkHeader { action: BulkAction::Create, metadata: BulkMetadata { - _id: id.map(|_id| _id.into()), + _id: Some(id.into()), ..Default::default() }, }, @@ -294,16 +291,12 @@ pub struct BulkIndexOperation { impl BulkIndexOperation { /// Creates a new instance of [BulkIndexOperation] - pub fn new(id: Option, source: B) -> Self - where - S: Into, - { + pub fn new(source: B) -> Self { Self { operation: BulkOperation { header: BulkHeader { action: BulkAction::Index, metadata: BulkMetadata { - _id: id.map(|_id| _id.into()), ..Default::default() }, }, @@ -703,7 +696,8 @@ mod tests { let mut ops: Vec> = Vec::with_capacity(4); ops.push( - BulkOperation::index(Some("1"), json!({ "foo": "index" })) + BulkOperation::index(json!({ "foo": "index" })) + .id("1") .pipeline("pipeline") .routing("routing") .if_seq_no(1) @@ -713,7 +707,7 @@ mod tests { .into(), ); ops.push( - BulkOperation::create(Some("2"), json!({ "bar": "create" })) + BulkOperation::create("2", json!({ "bar": "create" })) .pipeline("pipeline") .routing("routing") .index("create_index") @@ -788,19 +782,13 @@ mod tests { let mut ops = BulkOperations::new(); ops.push( - BulkOperation::index(Some("1"), IndexDoc { foo: "index" }) + BulkOperation::index(IndexDoc { foo: "index" }) + .id("1") .pipeline("pipeline") .index("index_doc") .routing("routing"), )?; - ops.push(BulkOperation::create( - Some("2"), - CreateDoc { bar: "create" }, - ))?; - ops.push(BulkOperation::create( - None::, - CreateDoc { bar: "create" }, - ))?; + ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?; ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?; ops.push(BulkOperation::<()>::delete("4"))?; @@ -812,8 +800,6 @@ 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 6e438182..c5935cf7 100644 --- a/opensearch/tests/common/client.rs +++ b/opensearch/tests/common/client.rs @@ -151,8 +151,9 @@ 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(Some(i.to_string()), json!({"title":"OpenSearch"})).into(); + let op = BulkOperation::index(json!({"title":"OpenSearch"})) + .id(i.to_string()) + .into(); body.push(op); } From 6a19e428b84c203dcc36815f2cd07c427569b5b6 Mon Sep 17 00:00:00 2001 From: Cameron Durham Date: Sat, 6 Apr 2024 02:29:18 -0700 Subject: [PATCH 3/5] Add no_id constructor and test case Signed-off-by: Cameron Durham --- opensearch/src/root/bulk.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index e9f430a9..f8c76677 100644 --- a/opensearch/src/root/bulk.rs +++ b/opensearch/src/root/bulk.rs @@ -179,6 +179,11 @@ where BulkCreateOperation::new(id, source) } + /// Creates a new instance of [bulk create operation](BulkCreateOperation) without explicit id + pub fn create_no_id(source: B) -> BulkCreateOperation { + BulkCreateOperation::new_no_id(source) + } + /// Creates a new instance of a [bulk index operation](BulkIndexOperation) pub fn index(source: B) -> BulkIndexOperation { BulkIndexOperation::new(source) @@ -245,6 +250,22 @@ impl BulkCreateOperation { } } + /// Creates a new instance of [BulkCreateOperation] without explicit id + pub fn new_no_id(source: B) -> Self { + Self { + operation: BulkOperation { + header: BulkHeader { + action: BulkAction::Create, + metadata: BulkMetadata { + _id: None, + ..Default::default() + }, + }, + source: Some(source), + }, + } + } + /// 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 @@ -789,6 +810,7 @@ mod tests { .routing("routing"), )?; ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?; + ops.push(BulkOperation::create_no_id(CreateDoc { bar: "create" }))?; ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?; ops.push(BulkOperation::<()>::delete("4"))?; @@ -800,6 +822,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"); From 378af3dfd9c9cd090936649039bed207affe1143 Mon Sep 17 00:00:00 2001 From: Cam <17013462+camerondurham@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:11:53 -0700 Subject: [PATCH 4/5] Change constructor name from _no_id to _without_id Co-authored-by: Thomas Farr Signed-off-by: Cam <17013462+camerondurham@users.noreply.github.com> --- opensearch/src/root/bulk.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index f8c76677..ddc6bff5 100644 --- a/opensearch/src/root/bulk.rs +++ b/opensearch/src/root/bulk.rs @@ -179,9 +179,9 @@ where BulkCreateOperation::new(id, source) } - /// Creates a new instance of [bulk create operation](BulkCreateOperation) without explicit id - pub fn create_no_id(source: B) -> BulkCreateOperation { - BulkCreateOperation::new_no_id(source) + /// Creates a new instance of [bulk create operation](BulkCreateOperation) without an explicit id + pub fn create_without_id(source: B) -> BulkCreateOperation { + BulkCreateOperation::new_without_id(source) } /// Creates a new instance of a [bulk index operation](BulkIndexOperation) @@ -250,8 +250,8 @@ impl BulkCreateOperation { } } - /// Creates a new instance of [BulkCreateOperation] without explicit id - pub fn new_no_id(source: B) -> Self { + /// Creates a new instance of [BulkCreateOperation] without an explicit id + pub fn new_without_id(source: B) -> Self { Self { operation: BulkOperation { header: BulkHeader { @@ -810,7 +810,7 @@ mod tests { .routing("routing"), )?; ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?; - ops.push(BulkOperation::create_no_id(CreateDoc { bar: "create" }))?; + ops.push(BulkOperation::create_without_id(CreateDoc { bar: "create" }))?; ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?; ops.push(BulkOperation::<()>::delete("4"))?; From 56e03168cde3e079ed2e5b323e0f464cd49c3dc4 Mon Sep 17 00:00:00 2001 From: camerondurham Date: Sun, 7 Apr 2024 18:14:57 -0700 Subject: [PATCH 5/5] Add entry to CHANGELOG Signed-off-by: camerondurham --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fad46740..dcf8df84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added - Added middleware types to allow intercepting construction and handling of the underlying `reqwest` client & requests ([#232](https://github.com/opensearch-project/opensearch-rs/pull/232)) +- Added new BulkCreate operation constructor without providing optional `id` field ([#245](https://github.com/opensearch-project/opensearch-rs/pull/245)) ### Dependencies - Bumps `aws-*` dependencies to `1` ([#219](https://github.com/opensearch-project/opensearch-rs/pull/219)) @@ -78,4 +79,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixes `DEVELOPER_GUIDE.md` to include complete information about setting up ([#194](https://github.com/opensearch-project/opensearch-rs/pull/194)) [Unreleased]: https://github.com/opensearch-project/opensearch-rs/compare/v2.2.0...HEAD [2.2.0]: https://github.com/opensearch-project/opensearch-rs/compare/v2.1.0...v2.2.0 -[2.1.0]: https://github.com/opensearch-project/opensearch-rs/compare/v2.0.0...v2.1.0 \ No newline at end of file +[2.1.0]: https://github.com/opensearch-project/opensearch-rs/compare/v2.0.0...v2.1.0