Skip to content

Commit

Permalink
Add new bulk create constructor without optional _id field (#245)
Browse files Browse the repository at this point in the history
* Use Option<S> when id is optional in bulk request

Signed-off-by: Cameron Durham <[email protected]>

* Revert "Use Option<S> when id is optional in bulk request"

Signed-off-by: Cameron Durham <[email protected]>

* Add no_id constructor and test case

Signed-off-by: Cameron Durham <[email protected]>

* Change constructor name from _no_id to _without_id

Co-authored-by: Thomas Farr <[email protected]>
Signed-off-by: Cam <[email protected]>

* Add entry to CHANGELOG

Signed-off-by: camerondurham <[email protected]>

---------

Signed-off-by: Cameron Durham <[email protected]>
Signed-off-by: Cam <[email protected]>
Signed-off-by: camerondurham <[email protected]>
Co-authored-by: Thomas Farr <[email protected]>
  • Loading branch information
camerondurham and Xtansia authored Apr 8, 2024
1 parent b4d3c88 commit 28e3d47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
[2.1.0]: https://github.com/opensearch-project/opensearch-rs/compare/v2.0.0...v2.1.0
24 changes: 24 additions & 0 deletions opensearch/src/root/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B> {
BulkCreateOperation::new_without_id(source)
}

/// Creates a new instance of a [bulk index operation](BulkIndexOperation)
pub fn index(source: B) -> BulkIndexOperation<B> {
BulkIndexOperation::new(source)
Expand Down Expand Up @@ -245,6 +250,22 @@ impl<B> BulkCreateOperation<B> {
}
}

/// 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
Expand Down Expand Up @@ -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"))?;

Expand All @@ -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");
Expand Down

0 comments on commit 28e3d47

Please sign in to comment.