Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new bulk create constructor without optional _id field #245

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
[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
Loading