diff --git a/quickwit/quickwit-serve/src/elasticsearch_api/model/search_query_params.rs b/quickwit/quickwit-serve/src/elasticsearch_api/model/search_query_params.rs index 903dfd4ed9c..479febaf4a1 100644 --- a/quickwit/quickwit-serve/src/elasticsearch_api/model/search_query_params.rs +++ b/quickwit/quickwit-serve/src/elasticsearch_api/model/search_query_params.rs @@ -21,7 +21,6 @@ use std::fmt; use std::str::FromStr; use std::time::Duration; -use quickwit_proto::search::SortOrder; use quickwit_query::BooleanOperand; use quickwit_search::SearchError; use serde::{Deserialize, Serialize}; @@ -232,10 +231,16 @@ pub struct DeleteQueryParams { pub timeout: Option, } +/// Parses a string as if it was a json value string. +fn parse_str_like_json(s: &str) -> Option { + let json_value = serde_json::Value::String(s.to_string()); + serde_json::from_value::(json_value).ok() +} + // Parse a single sort field parameter from ES sort query string parameter. fn parse_sort_field_str(sort_field_str: &str) -> Result { if let Some((field, order_str)) = sort_field_str.split_once(':') { - let order = SortOrder::from_str_name(order_str).ok_or_else(|| { + let order = parse_str_like_json(order_str).ok_or_else(|| { SearchError::InvalidArgument(format!( "invalid sort order `{}`. expected `asc` or `desc`", field @@ -375,3 +380,58 @@ impl fmt::Display for SuggestMode { } } } + +#[cfg(test)] +mod tests { + + use quickwit_proto::search::SortOrder; + + use super::*; + + #[derive(Deserialize, PartialEq, Eq, Debug)] + #[serde(rename_all = "snake_case")] + enum TestEnum { + FirstItem, + SecondItem, + } + + #[test] + fn test_parse_str_like_json() { + assert_eq!( + parse_str_like_json::("first_item").unwrap(), + TestEnum::FirstItem + ); + assert!(parse_str_like_json::("FirstItem").is_none()); + } + + #[test] + fn test_sort_order_qs() { + let sort_order_qs = parse_sort_field_str("timestamp:DESC").unwrap(); + assert_eq!( + sort_order_qs, + SortField { + field: "timestamp".to_string(), + order: SortOrder::Desc, + date_format: None + } + ); + let sort_order_qs = parse_sort_field_str("timestamp:desc").unwrap(); + assert_eq!( + sort_order_qs, + SortField { + field: "timestamp".to_string(), + order: SortOrder::Desc, + date_format: None + } + ); + let sort_order_qs = parse_sort_field_str("timestamp:asc").unwrap(); + assert_eq!( + sort_order_qs, + SortField { + field: "timestamp".to_string(), + order: SortOrder::Asc, + date_format: None + } + ); + } +} diff --git a/quickwit/rest-api-tests/scenarii/es_compatibility/0008-sort_by.yaml b/quickwit/rest-api-tests/scenarii/es_compatibility/0008-sort_by.yaml index e730f287be3..3d3a324db10 100644 --- a/quickwit/rest-api-tests/scenarii/es_compatibility/0008-sort_by.yaml +++ b/quickwit/rest-api-tests/scenarii/es_compatibility/0008-sort_by.yaml @@ -15,6 +15,21 @@ expected: actor: id: 10791502 --- +# Checking that passing the sort params as a query string works. +params: + sort: "actor.id:desc" + q: "*" + size: 1 +expected: + hits: + total: + value: 100 + relation: eq + hits: + - _source: + actor: + id: 10791502 +--- json: size: 1 query: @@ -113,4 +128,3 @@ expected: - _source: actor: id: 1762355 -