Skip to content

Commit 8a8d994

Browse files
authored
BulkOperation builder implementations (#56)
- BulkOperation builder implementations This commit introduces a collection of builders to help with the bulk API. The bulk API accepts a collection of operations in newline delimited JSON format, where each operation is represented as a header, followed by an optional source for the operation. The builders provide functions to build each of the supported operations, exposing the metadata options available for the operation. Operations can be converted to a common Operation type which implements the Body trait. - Add BulkOperations helps in building a collection of operations that vary over types used to represent source documents. - Add source filtering to update operation, and introduce SourceFilter enum to handle the variants. Add From<T> implementations for SourceFilter to ease usage. Closes #43
1 parent ea5cf50 commit 8a8d994

File tree

7 files changed

+919
-19
lines changed

7 files changed

+919
-19
lines changed

elasticsearch/src/http/request.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ where
9595
fn write(&self, bytes: &mut BytesMut) -> Result<(), Error> {
9696
for line in &self.0 {
9797
line.write(bytes)?;
98-
bytes.put_u8(b'\n');
98+
// only write a newline if the T impl does not
99+
if let Some(b) = bytes.last() {
100+
if b != &(b'\n') {
101+
bytes.put_u8(b'\n');
102+
}
103+
}
99104
}
100105
Ok(())
101106
}
@@ -111,6 +116,16 @@ impl Body for Bytes {
111116
}
112117
}
113118

119+
impl Body for BytesMut {
120+
fn bytes(&self) -> Option<Bytes> {
121+
Some(self.clone().freeze())
122+
}
123+
124+
fn write(&self, bytes: &mut BytesMut) -> Result<(), Error> {
125+
self.as_ref().write(bytes)
126+
}
127+
}
128+
114129
impl Body for Vec<u8> {
115130
fn write(&self, bytes: &mut BytesMut) -> Result<(), Error> {
116131
self.as_slice().write(bytes)

elasticsearch/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,10 @@ pub mod xpack;
331331
mod client;
332332
mod error;
333333
mod generated;
334+
mod root;
334335

335336
// exposes types within modules at the library root level
336-
pub use crate::{client::*, error::*, generated::root::*, http::transport::DEFAULT_ADDRESS};
337+
pub use crate::{client::*, error::*, http::transport::DEFAULT_ADDRESS, root::*};
337338

338339
#[cfg(test)]
339340
pub mod tests {

elasticsearch/src/params/mod.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,75 @@ pub enum TrackTotalHits {
1818
/// Accurately track the number of hits up to the specified value
1919
Count(i64),
2020
}
21+
22+
/// Control how the `_source` field is returned with every hit.
23+
///
24+
/// By default operations return the contents of the `_source` field
25+
/// unless you have used the `stored_fields` parameter or if the `_source` field is disabled.
26+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27+
#[serde(untagged)]
28+
pub enum SourceFilter {
29+
/// Whether `_source` retrieval should be enabled (`true`) or disabled (`false`)
30+
Enable(bool),
31+
32+
/// A wildcard pattern to control what parts of `_source` should be returned
33+
Include(String),
34+
35+
/// A collection of wildcard patterns to control what parts of `_source` should be returned
36+
Includes(Vec<String>),
37+
38+
/// A collection of wildcard patterns to control what parts of `_source` should
39+
/// and should not be returned
40+
IncludesExcludes {
41+
includes: Vec<String>,
42+
excludes: Vec<String>,
43+
},
44+
}
45+
46+
impl From<bool> for SourceFilter {
47+
fn from(b: bool) -> Self {
48+
SourceFilter::Enable(b)
49+
}
50+
}
51+
52+
impl From<String> for SourceFilter {
53+
fn from(include: String) -> Self {
54+
SourceFilter::Include(include)
55+
}
56+
}
57+
58+
impl<'a> From<&'a str> for SourceFilter {
59+
fn from(include: &'a str) -> Self {
60+
SourceFilter::Include(include.to_owned())
61+
}
62+
}
63+
64+
impl From<Vec<String>> for SourceFilter {
65+
fn from(includes: Vec<String>) -> Self {
66+
SourceFilter::Includes(includes)
67+
}
68+
}
69+
70+
impl<'a> From<Vec<&'a str>> for SourceFilter {
71+
fn from(includes: Vec<&'a str>) -> Self {
72+
SourceFilter::Includes(includes.iter().map(|s| (*s).to_string()).collect())
73+
}
74+
}
75+
76+
impl From<(Vec<String>, Vec<String>)> for SourceFilter {
77+
fn from(includes_excludes: (Vec<String>, Vec<String>)) -> Self {
78+
SourceFilter::IncludesExcludes {
79+
includes: includes_excludes.0,
80+
excludes: includes_excludes.1,
81+
}
82+
}
83+
}
84+
85+
impl<'a> From<(Vec<&'a str>, Vec<&'a str>)> for SourceFilter {
86+
fn from(includes_excludes: (Vec<&'a str>, Vec<&'a str>)) -> Self {
87+
SourceFilter::IncludesExcludes {
88+
includes: includes_excludes.0.iter().map(|s| (*s).to_string()).collect(),
89+
excludes: includes_excludes.1.iter().map(|s| (*s).to_string()).collect(),
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)