-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the string_or_struct logic and enables it for TermQuery. (#3906
) * Refactor the string_or_struct logic and enables it for TermQuery. Closes #3900 * Allowing integers in term queries.
- Loading branch information
1 parent
8d89c49
commit 91540c6
Showing
9 changed files
with
343 additions
and
140 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
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
95 changes: 95 additions & 0 deletions
95
quickwit/quickwit-query/src/elastic_query_dsl/string_or_struct.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,95 @@ | ||
// Copyright (C) 2023 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at hello@quickwit.io. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::fmt; | ||
use std::marker::PhantomData; | ||
|
||
use serde::de::{MapAccess, Visitor}; | ||
use serde::{de, Deserialize, Deserializer}; | ||
|
||
/// The point of `StringOrStructForSerialization` is to support | ||
/// the two following formats for various queries. | ||
/// | ||
/// `{"field": {"query": "my query", "default_operator": "OR"}}` | ||
/// | ||
/// and the shorter. | ||
/// `{"field": "my query"}` | ||
/// | ||
/// If a integer is passed, we cast it to string. Floats are not supported. | ||
/// | ||
/// We don't use untagged enum to support this, in order to keep good errors. | ||
/// | ||
/// The code below is adapted from solution described here: https://serde.rs/string-or-struct.html | ||
#[derive(Deserialize)] | ||
#[serde(transparent)] | ||
pub(crate) struct StringOrStructForSerialization<T> | ||
where | ||
T: From<String>, | ||
for<'de2> T: Deserialize<'de2>, | ||
{ | ||
#[serde(deserialize_with = "string_or_struct")] | ||
pub inner: T, | ||
} | ||
|
||
struct StringOrStructVisitor<T> { | ||
phantom_data: PhantomData<T>, | ||
} | ||
|
||
fn string_or_struct<'de, D, T>(deserializer: D) -> Result<T, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: From<String> + Deserialize<'de>, | ||
{ | ||
deserializer.deserialize_any(StringOrStructVisitor { | ||
phantom_data: Default::default(), | ||
}) | ||
} | ||
|
||
impl<'de, T> Visitor<'de> for StringOrStructVisitor<T> | ||
where | ||
T: From<String>, | ||
T: Deserialize<'de>, | ||
{ | ||
type Value = T; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
let type_str = std::any::type_name::<T>(); | ||
formatter.write_str(&format!("string or map to deserialize {type_str}.")) | ||
} | ||
|
||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> | ||
where E: de::Error { | ||
self.visit_str(&v.to_string()) | ||
} | ||
|
||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> | ||
where E: de::Error { | ||
self.visit_str(&v.to_string()) | ||
} | ||
|
||
fn visit_str<E>(self, query: &str) -> Result<Self::Value, E> | ||
where E: serde::de::Error { | ||
Ok(T::from(query.to_string())) | ||
} | ||
|
||
fn visit_map<M>(self, map: M) -> Result<T, M::Error> | ||
where M: MapAccess<'de> { | ||
Deserialize::deserialize(de::value::MapAccessDeserializer::new(map)) | ||
} | ||
} |
Oops, something went wrong.