-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source method to UpdateRequestBuilder
This is a more ergonomic way to include the updated doc in the response. Also: - Add integration test - Add example - Update docs Close #381
- Loading branch information
1 parent
b60808a
commit 06a3e97
Showing
5 changed files
with
304 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! Update a document and return the updated document in a single request. | ||
//! | ||
//! NOTE: This sample expects you have a node running on `localhost:9200`. | ||
//! | ||
//! This sample demonstrates how to index & update a document and return | ||
//! the newly updated document. | ||
//! Also see the `typed` sample for a more complete implementation. | ||
|
||
use std::error::Error; | ||
|
||
use elastic::prelude::*; | ||
use elastic_derive::ElasticType; | ||
use env_logger; | ||
use serde::{ | ||
Deserialize, | ||
Serialize, | ||
}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, ElasticType)] | ||
#[elastic(index = "update_with_source_sample_index")] | ||
struct NewsArticle { | ||
#[elastic(id)] | ||
id: String, | ||
title: String, | ||
content: String, | ||
likes: i64, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, ElasticType)] | ||
#[elastic(index = "update_with_source_sample_index")] | ||
struct UpdatedNewsArticle { | ||
#[elastic(id)] | ||
id: String, | ||
title: String, | ||
content: String, | ||
likes: i64, | ||
} | ||
|
||
fn run() -> Result<(), Box<dyn Error>> { | ||
// A HTTP client and request parameters | ||
let client = SyncClient::builder().build()?; | ||
|
||
// Create a document to index | ||
let doc = NewsArticle { | ||
id: "1".to_string(), | ||
title: "A title".to_string(), | ||
content: "Some content.".to_string(), | ||
likes: 0, | ||
}; | ||
|
||
// Index the document | ||
client.document().index(doc).send()?; | ||
|
||
// Update the document using a script | ||
let update = client | ||
.document::<NewsArticle>() | ||
.update("1") | ||
.script("ctx._source.likes++") | ||
// Request that the updated document be returned with the response | ||
.source::<UpdatedNewsArticle>() | ||
.send()?; | ||
|
||
assert!(update.updated()); | ||
|
||
// Deserialize the updated document to the type we requested earlier; | ||
// this will return `None` if `source()` was not called on the request | ||
let updated_doc = update.into_document().unwrap(); | ||
|
||
assert!(updated_doc.likes > 0); | ||
|
||
println!("{:#?}", &updated_doc); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
env_logger::init(); | ||
run().unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.