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

Make id optional for BulkCreateOperation #193

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
34 changes: 19 additions & 15 deletions elasticsearch/src/root/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,8 @@ where
B: Serialize,
{
/// Creates a new instance of a [bulk create operation](BulkCreateOperation)
pub fn create<S>(id: S, source: B) -> BulkCreateOperation<B>
where
S: Into<String>,
{
BulkCreateOperation::new(id, source)
pub fn create(source: B) -> BulkCreateOperation<B> {
BulkCreateOperation::new(source)
}

/// Creates a new instance of a [bulk index operation](BulkIndexOperation)
Expand Down Expand Up @@ -217,24 +214,30 @@ pub struct BulkCreateOperation<B> {

impl<B> BulkCreateOperation<B> {
/// Creates a new instance of [BulkCreateOperation]
pub fn new<S>(id: S, source: B) -> Self
where
S: Into<String>,
{
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<S>(mut self, id: S) -> Self
where
S: Into<String>,
{
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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"))?;

Expand Down