Skip to content

Commit

Permalink
Use Option<S> when id is optional in bulk request
Browse files Browse the repository at this point in the history
Signed-off-by: Cameron Durham <[email protected]>
  • Loading branch information
camerondurham committed Apr 3, 2024
1 parent 41417d4 commit 2dc360c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
38 changes: 26 additions & 12 deletions opensearch/src/root/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,19 @@ where
B: Serialize,
{
/// Creates a new instance of a [bulk create operation](BulkCreateOperation)
pub fn create<S>(id: S, source: B) -> BulkCreateOperation<B>
pub fn create<S>(id: Option<S>, source: B) -> BulkCreateOperation<B>
where
S: Into<String>,
{
BulkCreateOperation::new(id, source)
}

/// Creates a new instance of a [bulk index operation](BulkIndexOperation)
pub fn index(source: B) -> BulkIndexOperation<B> {
BulkIndexOperation::new(source)
pub fn index<S>(id: Option<S>, source: B) -> BulkIndexOperation<B>
where
S: Into<String>,
{
BulkIndexOperation::new(id, source)
}

/// Creates a new instance of a [bulk delete operation](BulkDeleteOperation)
Expand Down Expand Up @@ -227,7 +230,7 @@ pub struct BulkCreateOperation<B> {

impl<B> BulkCreateOperation<B> {
/// Creates a new instance of [BulkCreateOperation]
pub fn new<S>(id: S, source: B) -> Self
pub fn new<S>(id: Option<S>, source: B) -> Self
where
S: Into<String>,
{
Expand All @@ -236,7 +239,7 @@ impl<B> BulkCreateOperation<B> {
header: BulkHeader {
action: BulkAction::Create,
metadata: BulkMetadata {
_id: Some(id.into()),
_id: id.map(|_id| _id.into()),
..Default::default()
},
},
Expand Down Expand Up @@ -291,12 +294,16 @@ pub struct BulkIndexOperation<B> {

impl<B> BulkIndexOperation<B> {
/// Creates a new instance of [BulkIndexOperation]
pub fn new(source: B) -> Self {
pub fn new<S>(id: Option<S>, source: B) -> Self
where
S: Into<String>,
{
Self {
operation: BulkOperation {
header: BulkHeader {
action: BulkAction::Index,
metadata: BulkMetadata {
_id: id.map(|_id| _id.into()),
..Default::default()
},
},
Expand Down Expand Up @@ -696,8 +703,7 @@ mod tests {
let mut ops: Vec<BulkOperation<Value>> = Vec::with_capacity(4);

ops.push(
BulkOperation::index(json!({ "foo": "index" }))
.id("1")
BulkOperation::index(Some("1"), json!({ "foo": "index" }))
.pipeline("pipeline")
.routing("routing")
.if_seq_no(1)
Expand All @@ -707,7 +713,7 @@ mod tests {
.into(),
);
ops.push(
BulkOperation::create("2", json!({ "bar": "create" }))
BulkOperation::create(Some("2"), json!({ "bar": "create" }))
.pipeline("pipeline")
.routing("routing")
.index("create_index")
Expand Down Expand Up @@ -782,13 +788,19 @@ mod tests {
let mut ops = BulkOperations::new();

ops.push(
BulkOperation::index(IndexDoc { foo: "index" })
.id("1")
BulkOperation::index(Some("1"), IndexDoc { foo: "index" })
.pipeline("pipeline")
.index("index_doc")
.routing("routing"),
)?;
ops.push(BulkOperation::create("2", CreateDoc { bar: "create" }))?;
ops.push(BulkOperation::create(
Some("2"),
CreateDoc { bar: "create" },
))?;
ops.push(BulkOperation::create(
None::<String>,
CreateDoc { bar: "create" },
))?;
ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?;
ops.push(BulkOperation::<()>::delete("4"))?;

Expand All @@ -800,6 +812,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
5 changes: 2 additions & 3 deletions opensearch/tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ pub async fn index_documents(client: &OpenSearch) -> Result<Response, Error> {
if exists_response.status_code() == StatusCode::NOT_FOUND {
let mut body: Vec<BulkOperation<_>> = vec![];
for i in 1..=10 {
let op = BulkOperation::index(json!({"title":"OpenSearch"}))
.id(i.to_string())
.into();
let op =
BulkOperation::index(Some(i.to_string()), json!({"title":"OpenSearch"})).into();
body.push(op);
}

Expand Down

0 comments on commit 2dc360c

Please sign in to comment.