-
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.
Merge pull request #381 from mwilliammyers/feat/update-source
Add support for _source on update requests
- Loading branch information
Showing
5 changed files
with
279 additions
and
2 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() | ||
.send()?; | ||
|
||
assert!(update.updated()); | ||
|
||
// Deserialize the updated document, | ||
// will return `None` if `source()` was not called on the request | ||
let updated_doc = update.into_document::<UpdatedNewsArticle>().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
85 changes: 85 additions & 0 deletions
85
tests/integration/src/tests/document/update_with_source.rs
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,85 @@ | ||
use elastic::{ | ||
error::Error, | ||
prelude::*, | ||
}; | ||
use futures::Future; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize, ElasticType)] | ||
#[elastic(index = "update_doc_source_idx")] | ||
pub struct Doc { | ||
#[elastic(id)] | ||
id: String, | ||
title: String, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize, ElasticType)] | ||
#[elastic(index = "update_doc_source_idx")] | ||
pub struct UpdatedDoc { | ||
#[elastic(id)] | ||
id: String, | ||
title: String, | ||
} | ||
|
||
const EXPECTED_TITLE: &'static str = "Edited title"; | ||
const ID: &'static str = "1"; | ||
|
||
fn doc() -> Doc { | ||
Doc { | ||
id: ID.to_owned(), | ||
title: "Not edited title".to_owned(), | ||
} | ||
} | ||
|
||
test! { | ||
const description: &'static str = "update and return source"; | ||
|
||
type Response = UpdateResponse; | ||
|
||
// Ensure the index doesn't exist | ||
fn prepare(&self, client: AsyncClient) -> Box<dyn Future<Item = (), Error = Error>> { | ||
let delete_res = client | ||
.index(Doc::static_index()) | ||
.delete() | ||
.send() | ||
.map(|_| ()); | ||
|
||
Box::new(delete_res) | ||
} | ||
|
||
// Execute an update request against that index using a new document & request | ||
// that the updated document's `source` be returned with the response. | ||
fn request( | ||
&self, | ||
client: AsyncClient, | ||
) -> Box<dyn Future<Item = Self::Response, Error = Error>> { | ||
let index_res = client | ||
.document() | ||
.index(doc()) | ||
.params_fluent(|p| p.url_param("refresh", true)) | ||
.send(); | ||
|
||
let update_res = client | ||
.document::<Doc>() | ||
.update(ID) | ||
.doc(json!({ | ||
"title": EXPECTED_TITLE.to_owned(), | ||
})) | ||
.source() | ||
.send(); | ||
|
||
Box::new( | ||
index_res | ||
.and_then(|_| update_res) | ||
.map(|update| update) | ||
) | ||
} | ||
|
||
// Ensure the response contains the expected document | ||
fn assert_ok(&self, res: &Self::Response) -> bool { | ||
let updated = res.updated(); | ||
let correct_version = res.version() == Some(2); | ||
let correct_title = res.into_document::<UpdatedDoc>().unwrap().title == EXPECTED_TITLE; | ||
|
||
updated && correct_version && correct_title | ||
} | ||
} |