Skip to content

Commit 6416135

Browse files
authored
feat!: make id optional for BulkCreateOperation (#193)
1 parent a15f168 commit 6416135

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

elasticsearch/src/root/bulk.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,8 @@ where
162162
B: Serialize,
163163
{
164164
/// Creates a new instance of a [bulk create operation](BulkCreateOperation)
165-
pub fn create<S>(id: S, source: B) -> BulkCreateOperation<B>
166-
where
167-
S: Into<String>,
168-
{
169-
BulkCreateOperation::new(id, source)
165+
pub fn create(source: B) -> BulkCreateOperation<B> {
166+
BulkCreateOperation::new(source)
170167
}
171168

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

218215
impl<B> BulkCreateOperation<B> {
219216
/// Creates a new instance of [BulkCreateOperation]
220-
pub fn new<S>(id: S, source: B) -> Self
221-
where
222-
S: Into<String>,
223-
{
217+
pub fn new(source: B) -> Self {
224218
Self {
225219
operation: BulkOperation {
226220
header: BulkHeader {
227221
action: BulkAction::Create,
228-
metadata: BulkMetadata {
229-
_id: Some(id.into()),
230-
..Default::default()
231-
},
222+
metadata: BulkMetadata::default(),
232223
},
233224
source: Some(source),
234225
},
235226
}
236227
}
237228

229+
/// Specify the id for the document
230+
///
231+
/// If an id is not specified, Elasticsearch will generate an id for the document
232+
/// which will be returned in the response.
233+
pub fn id<S>(mut self, id: S) -> Self
234+
where
235+
S: Into<String>,
236+
{
237+
self.operation.header.metadata._id = Some(id.into());
238+
self
239+
}
240+
238241
/// Specify the name of the index to perform the bulk update operation against.
239242
///
240243
/// Each bulk operation can specify an index to operate against. If all bulk operations
@@ -697,7 +700,8 @@ mod tests {
697700
.into(),
698701
);
699702
ops.push(
700-
BulkOperation::create("2", json!({ "bar": "create" }))
703+
BulkOperation::create(json!({ "bar": "create" }))
704+
.id("2")
701705
.pipeline("pipeline")
702706
.routing("routing")
703707
.index("create_index")
@@ -779,7 +783,7 @@ mod tests {
779783
.index("index_doc")
780784
.routing("routing"),
781785
)?;
782-
ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?;
786+
ops.push(BulkOperation::create(CreateDoc { bar: "create" }).id("2"))?;
783787
ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?;
784788
ops.push(BulkOperation::<()>::delete("4"))?;
785789

0 commit comments

Comments
 (0)