Skip to content

Commit 13b4d4b

Browse files
authored
fix: Add support for numbers on TermsQuery (quickwit-oss#6097)
* fix: Add support for numbers on TermsQuery Signed-off-by: Darkheir <raphael.cohen@sekoia.io>
1 parent e2d9623 commit 13b4d4b

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

quickwit/quickwit-query/src/elastic_query_dsl/terms_query.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,36 @@ struct TermsQueryForSerialization {
3737
capture_other: serde_json::Value,
3838
}
3939

40+
#[derive(Deserialize)]
41+
#[serde(untagged)]
42+
enum TermValue {
43+
I64(i64),
44+
U64(u64),
45+
Str(String),
46+
}
47+
48+
impl From<TermValue> for String {
49+
fn from(term_value: TermValue) -> String {
50+
match term_value {
51+
TermValue::I64(val) => val.to_string(),
52+
TermValue::U64(val) => val.to_string(),
53+
TermValue::Str(val) => val,
54+
}
55+
}
56+
}
57+
4058
#[derive(Deserialize)]
4159
#[serde(untagged)]
4260
enum OneOrMany {
43-
One(String),
44-
Many(Vec<String>),
61+
One(TermValue),
62+
Many(Vec<TermValue>),
4563
}
64+
4665
impl From<OneOrMany> for Vec<String> {
4766
fn from(one_or_many: OneOrMany) -> Vec<String> {
4867
match one_or_many {
49-
OneOrMany::One(one_value) => vec![one_value],
50-
OneOrMany::Many(values) => values,
68+
OneOrMany::One(one_value) => vec![String::from(one_value)],
69+
OneOrMany::Many(values) => values.into_iter().map(String::from).collect(),
5170
}
5271
}
5372
}
@@ -109,6 +128,14 @@ mod tests {
109128
assert_eq!(&terms_query.values[..], &["hello".to_string()]);
110129
}
111130

131+
#[test]
132+
fn test_terms_query_not_string() {
133+
let terms_query_json = r#"{ "user.id": [1, 2] }"#;
134+
let terms_query: TermsQuery = serde_json::from_str(terms_query_json).unwrap();
135+
assert_eq!(&terms_query.field, "user.id");
136+
assert_eq!(&terms_query.values[..], &["1".to_string(), "2".to_string()]);
137+
}
138+
112139
#[test]
113140
fn test_terms_query_single_term_boost() {
114141
let terms_query_json = r#"{ "user.id": ["hello", "happy"], "boost": 2 }"#;

0 commit comments

Comments
 (0)