diff --git a/src/elastic/examples/update_with_source.rs b/src/elastic/examples/update_with_source.rs index 0226db420d..d2d2cd5223 100644 --- a/src/elastic/examples/update_with_source.rs +++ b/src/elastic/examples/update_with_source.rs @@ -57,14 +57,14 @@ fn run() -> Result<(), Box> { .update("1") .script("ctx._source.likes++") // Request that the updated document be returned with the response - .source::() + .source() .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(); + // Deserialize the updated document, + // will return `None` if `source()` was not called on the request + let updated_doc = update.into_document::().unwrap(); assert!(updated_doc.likes > 0); diff --git a/src/elastic/src/client/requests/document_update.rs b/src/elastic/src/client/requests/document_update.rs index f3b7f85486..27822b5917 100644 --- a/src/elastic/src/client/requests/document_update.rs +++ b/src/elastic/src/client/requests/document_update.rs @@ -49,8 +49,6 @@ pub use crate::client::requests::common::{ ScriptBuilder, }; -pub(crate) type DefaultUpdatedSource = serde_json::Value; - /** An [update document request][docs-update] builder that can be configured before sending. @@ -62,17 +60,15 @@ The `send` method will either send the request [synchronously][send-sync] or [as [send-async]: #send-asynchronously [Client.document.update]: ../../struct.DocumentClient.html#update-document-request */ -pub type UpdateRequestBuilder = - RequestBuilder>; +pub type UpdateRequestBuilder = RequestBuilder>; #[doc(hidden)] -pub struct UpdateRequestInner { +pub struct UpdateRequestInner { index: Index<'static>, ty: Type<'static>, id: Id<'static>, body: TBody, - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, } /** @@ -221,8 +217,7 @@ where ty: ty, id: id.into(), body: Doc::empty(), - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, }, ) } @@ -277,14 +272,13 @@ where ty: DEFAULT_DOC_TYPE.into(), id: id.into(), body: Doc::empty(), - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, }, ) } } -impl UpdateRequestInner +impl UpdateRequestInner where TBody: Serialize, { @@ -388,8 +382,7 @@ where index: self.inner.index, ty: self.inner.ty, id: self.inner.id, - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, }, ) } @@ -468,8 +461,7 @@ where index: self.inner.index, ty: self.inner.ty, id: self.inner.id, - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, }, ) } @@ -560,7 +552,7 @@ where } } -impl UpdateRequestBuilder +impl UpdateRequestBuilder where TSender: Sender, { @@ -583,17 +575,17 @@ where # fn main() { run().unwrap() } # fn run() -> Result<(), Box> { # #[derive(Serialize, Deserialize, ElasticType)] - # struct NewsArticle { likes: i64 } + # struct NewsArticle { id: i64, likes: i64 } # #[derive(Serialize, Deserialize, ElasticType)] - # struct UpdatedNewsArticle { likes: i64 } + # struct UpdatedNewsArticle { id: i64, likes: i64 } # let client = SyncClientBuilder::new().build()?; let response = client.document::() .update(1) .script("ctx._source.likes++") - .source::() + .source() .send()?; - assert!(response.into_document().unwrap().likes >= 1); + assert!(response.into_document::().unwrap().likes >= 1); # Ok(()) # } ``` @@ -602,12 +594,11 @@ where [`UpdateResponse`]: ../../responses/struct.UpdateResponse.html [`into_document`]: ../../responses/struct.UpdateResponse.html#method.into_document */ - pub fn source(self) -> UpdateRequestBuilder - where - T: DeserializeOwned, - { + pub fn source(self) -> UpdateRequestBuilder { RequestBuilder::new( self.client, + // TODO: allow passing in `source` parameter add `_source` to body + // instead because it supports more options self.params_builder .fluent(|params| params.url_param("_source", true)) .shared(), @@ -616,8 +607,7 @@ where index: self.inner.index, ty: self.inner.ty, id: self.inner.id, - _body_marker: PhantomData, - _update_source_marker: PhantomData, + _marker: PhantomData, }, ) } @@ -626,7 +616,7 @@ where /** # Send synchronously */ -impl UpdateRequestBuilder +impl UpdateRequestBuilder where TBody: Serialize, { @@ -666,7 +656,7 @@ where [SyncClient]: ../../type.SyncClient.html [documents-mod]: ../../types/document/index.html */ - pub fn send(self) -> Result, Error> { + pub fn send(self) -> Result { let req = self.inner.into_request()?; RequestBuilder::new(self.client, self.params_builder, RawRequestInner::new(req)) @@ -678,10 +668,9 @@ where /** # Send asynchronously */ -impl UpdateRequestBuilder +impl UpdateRequestBuilder where TBody: Serialize + Send + 'static, - TSource: Send + 'static, { /** Send an `UpdateRequestBuilder` asynchronously using an [`AsyncClient`][AsyncClient]. @@ -726,7 +715,7 @@ where [AsyncClient]: ../../type.AsyncClient.html [documents-mod]: ../../types/document/index.html */ - pub fn send(self) -> Pending { + pub fn send(self) -> Pending { let (client, params_builder, inner) = (self.client, self.params_builder, self.inner); let req_future = client.sender.maybe_async(move || inner.into_request()); @@ -758,7 +747,7 @@ mod tests { #[test] fn is_send() { - assert_send::>(); + assert_send::(); } #[derive(Serialize, ElasticType)] diff --git a/src/elastic/src/client/responses/document_update.rs b/src/elastic/src/client/responses/document_update.rs index 13ba9a5ed6..434bf2b7d0 100644 --- a/src/elastic/src/client/responses/document_update.rs +++ b/src/elastic/src/client/responses/document_update.rs @@ -2,15 +2,12 @@ Response types for a [update document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html). */ -use std::marker::PhantomData; - use serde::de::DeserializeOwned; use serde_json::Value; use super::common::DocumentResult; use crate::{ - client::requests::document_update::DefaultUpdatedSource, http::receiver::IsOkOnSuccess, types::document::{ Id, @@ -21,7 +18,7 @@ use crate::{ /** Response for an [update document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html). */ #[derive(Deserialize, Debug)] -pub struct UpdateResponse { +pub struct UpdateResponse { #[serde(rename = "_index")] index: String, #[serde(rename = "_type")] @@ -38,14 +35,9 @@ pub struct UpdateResponse { routing: Option, result: DocumentResult, get: Option, - #[serde(skip)] - _marker: PhantomData, } -impl UpdateResponse -where - TSource: DeserializeOwned, -{ +impl UpdateResponse { /** Convert the source in the response into the updated document. @@ -61,17 +53,17 @@ where # fn main() { run().unwrap() } # fn run() -> Result<(), Box> { # #[derive(Serialize, Deserialize, ElasticType)] - # struct NewsArticle { likes: i64 } + # struct NewsArticle { id: i64, likes: i64 } # #[derive(Serialize, Deserialize, ElasticType)] - # struct UpdatedNewsArticle { likes: i64 } + # struct UpdatedNewsArticle { id: i64, likes: i64 } # let client = SyncClientBuilder::new().build()?; let response = client.document::() .update(1) .script("ctx._source.likes++") - .source::() + .source() .send()?; - assert!(response.into_document().unwrap().likes >= 1); + assert!(response.into_document::().unwrap().likes >= 1); # Ok(()) # } ``` @@ -79,13 +71,16 @@ where [`source`]: ../requests/document_update/type.UpdateRequestBuilder.html#method.source [`UpdateRequestBuilder`]: ../requests/document_update/type.UpdateRequestBuilder.html */ - pub fn into_document(&self) -> Option { + pub fn into_document(&self) -> Option + where + T: DeserializeOwned, + { self.get.as_ref().map_or_else( || None, |obj| { obj.get("_source") .cloned() - .map_or_else(|| None, |doc| serde_json::from_value::(doc).ok()) + .map_or_else(|| None, |doc| serde_json::from_value::(doc).ok()) }, ) } @@ -137,4 +132,4 @@ where } } -impl IsOkOnSuccess for UpdateResponse {} +impl IsOkOnSuccess for UpdateResponse {} diff --git a/tests/integration/src/tests/document/update_with_source.rs b/tests/integration/src/tests/document/update_with_source.rs index 3b1e406214..6392b830eb 100644 --- a/tests/integration/src/tests/document/update_with_source.rs +++ b/tests/integration/src/tests/document/update_with_source.rs @@ -33,7 +33,7 @@ fn doc() -> Doc { test! { const description: &'static str = "update and return source"; - type Response = UpdateResponse; + type Response = UpdateResponse; // Ensure the index doesn't exist fn prepare(&self, client: AsyncClient) -> Box> { @@ -64,7 +64,7 @@ test! { .doc(json!({ "title": EXPECTED_TITLE.to_owned(), })) - .source::() + .source() .send(); Box::new( @@ -78,7 +78,7 @@ test! { 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().unwrap().title == EXPECTED_TITLE; + let correct_title = res.into_document::().unwrap().title == EXPECTED_TITLE; updated && correct_version && correct_title }