Skip to content

Commit

Permalink
Fix stored fields _none_ case
Browse files Browse the repository at this point in the history
  • Loading branch information
buinauskas committed Jun 14, 2023
1 parent 653e66f commit d9ff00d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "elasticsearch-dsl"
version = "0.4.11"
version = "0.4.12"
authors = ["Evaldas Buinauskas <evaldas.buinauskas@vinted.com>", "Boost <boost@vinted.com>"]
edition = "2018"
description = "Strongly typed Elasticsearch DSL"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
warnings,
while_true
)]
#![allow(ambiguous_glob_reexports)]

#[cfg(test)]
#[macro_use]
Expand Down
2 changes: 2 additions & 0 deletions src/search/queries/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod negative_boost;
mod operator;
mod rewrite;
mod script_object;
mod stored_fields;
mod zero_terms_query;

// Query specific parameters
Expand Down Expand Up @@ -40,6 +41,7 @@ pub use self::rewrite::*;
pub use self::script_object::*;
pub use self::shape_query::*;
pub use self::simple_query_string_query::*;
pub use self::stored_fields::*;
pub use self::terms_set_query::*;
pub use self::text_query_type::*;
pub use self::zero_terms_query::*;
78 changes: 78 additions & 0 deletions src/search/queries/params/stored_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use serde::Serialize;

use crate::{types::Set, util::ShouldSkip};

/// It’s also possible to store an individual field’s values by using the store mapping option.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/8.8/search-fields.html#stored-fields>
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StoredFields {
/// To disable the stored fields (and metadata fields) entirely use: `_none_`
None,

/// Allows to selectively load specific stored fields for each document represented by a search hit.
Fields(Set<String>),
}

impl Default for StoredFields {
fn default() -> Self {
Self::Fields(Set::default())
}
}

impl ShouldSkip for StoredFields {
fn should_skip(&self) -> bool {
match self {
Self::None => false,
Self::Fields(fields) => fields.should_skip(),
}
}
}

impl Serialize for StoredFields {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::None => serializer.serialize_str("_none_"),
Self::Fields(fields) => fields.serialize(serializer),
}
}
}

impl<T> From<T> for StoredFields
where
T: IntoIterator,
T::Item: ToString,
{
fn from(value: T) -> Self {
let fields = value.into_iter().map(|v| v.to_string()).collect::<Set<_>>();

if fields.len() == 1 && fields.contains("_none_") {
Self::None
} else {
Self::Fields(fields)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::util::assert_serialize;

#[test]
fn serialization() {
assert_serialize(StoredFields::None, json!("_none_"));
assert_serialize(StoredFields::from(["_none_"]), json!("_none_"));
assert_serialize(StoredFields::from(["abc", "def"]), json!(["abc", "def"]));
}

#[test]
fn should_skip() {
assert!(!StoredFields::None.should_skip());
assert!(!StoredFields::from(["abc", "def"]).should_skip());
assert!(StoredFields::from(Vec::<String>::new()).should_skip());
}
}
10 changes: 4 additions & 6 deletions src/search/queries/specialized/more_like_this_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::search::*;
use crate::util::*;
use crate::Set;

/// The More Like This Query finds documents that are "like" a given set of documents.
/// In order to do so, MLT selects a set of representative terms of these input documents,
Expand Down Expand Up @@ -147,7 +146,7 @@ pub struct Document {
_source: Option<SourceFilter>,

#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_stored_fields: Set<String>,
_stored_fields: StoredFields,
}

impl Document {
Expand All @@ -160,7 +159,7 @@ impl Document {
{
Self {
_id: id.to_string(),
_stored_fields: Set::new(),
_stored_fields: Default::default(),
_index: None,
_routing: None,
_source: None,
Expand Down Expand Up @@ -197,10 +196,9 @@ impl Document {
/// The stored fields you want to retrieve.
pub fn stored_fields<T>(mut self, stored_fields: T) -> Self
where
T: IntoIterator,
T::Item: ToString,
T: Into<StoredFields>,
{
self._stored_fields = stored_fields.into_iter().map(|x| x.to_string()).collect();
self._stored_fields = stored_fields.into();
self
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/search/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Search {
suggest: Map<String, Suggester>,

#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
stored_fields: Set<String>,
stored_fields: StoredFields,

#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
docvalue_fields: Set<String>,
Expand Down Expand Up @@ -195,11 +195,9 @@ impl Search {
/// A collection of stored fields
pub fn stored_fields<T>(mut self, stored_fields: T) -> Self
where
T: IntoIterator,
T::Item: ToString,
T: Into<StoredFields>,
{
self.stored_fields
.extend(stored_fields.into_iter().map(|x| x.to_string()));
self.stored_fields = stored_fields.into();
self
}

Expand Down

0 comments on commit d9ff00d

Please sign in to comment.