diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e82a87e..db084a50 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)) @@ -79,4 +80,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 diff --git a/opensearch/src/root/bulk.rs b/opensearch/src/root/bulk.rs index e9f430a9..ddc6bff5 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 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) pub fn index(source: B) -> BulkIndexOperation { BulkIndexOperation::new(source) @@ -245,6 +250,22 @@ impl BulkCreateOperation { } } + /// Creates a new instance of [BulkCreateOperation] without an explicit id + pub fn new_without_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_without_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");