From cd7745da7ae69cacf8913028bc5e91082e576a37 Mon Sep 17 00:00:00 2001 From: Kat Lim Ruiz Date: Wed, 2 Apr 2025 17:52:18 -0500 Subject: [PATCH] set Leaf untagged, leave clause and boost the same (with own property) --- query-grammar/src/lib.rs | 4 ++-- query-grammar/src/user_input_ast.rs | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/query-grammar/src/lib.rs b/query-grammar/src/lib.rs index 6c52c8cff..2ba7c589e 100644 --- a/query-grammar/src/lib.rs +++ b/query-grammar/src/lib.rs @@ -35,11 +35,11 @@ mod tests { #[test] fn test_parse_query_serialization() { - let ast = parse_query("title:hello").unwrap(); + let ast = parse_query("title:hello OR title:x").unwrap(); let json = serde_json::to_string(&ast).unwrap(); assert_eq!( json, - r#"{"leaf":{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}}"# + r#"{"clause":[["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}],["should",{"type":"literal","field_name":"title","phrase":"x","delimiter":"none","slop":0,"prefix":false}]]}"# ); } diff --git a/query-grammar/src/user_input_ast.rs b/query-grammar/src/user_input_ast.rs index f87bcee64..76269a725 100644 --- a/query-grammar/src/user_input_ast.rs +++ b/query-grammar/src/user_input_ast.rs @@ -197,10 +197,12 @@ impl UserInputBound { #[derive(PartialEq, Clone, Serialize)] #[serde(rename_all = "snake_case")] +//#[serde(tag = "type", content = "value")] pub enum UserInputAst { Clause(Vec<(Option, UserInputAst)>), - Leaf(Box), Boost(Box, f64), + #[serde(untagged)] + Leaf(Box), } impl UserInputAst { @@ -303,7 +305,7 @@ mod tests { fn test_all_leaf_serialization() { let ast = UserInputAst::Leaf(Box::new(UserInputLeaf::All)); let json = serde_json::to_string(&ast).unwrap(); - assert_eq!(json, r#"{"leaf":{"type":"all"}}"#); + assert_eq!(json, r#"{"type":"all"}"#); } #[test] @@ -319,7 +321,7 @@ mod tests { let json = serde_json::to_string(&ast).unwrap(); assert_eq!( json, - r#"{"leaf":{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}}"# + r#"{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}"# ); } @@ -334,7 +336,7 @@ mod tests { let json = serde_json::to_string(&ast).unwrap(); assert_eq!( json, - r#"{"leaf":{"type":"range","field":"price","lower":{"type":"inclusive","value":"10"},"upper":{"type":"exclusive","value":"100"}}}"# + r#"{"type":"range","field":"price","lower":{"type":"inclusive","value":"10"},"upper":{"type":"exclusive","value":"100"}}"# ); } @@ -349,7 +351,7 @@ mod tests { let json = serde_json::to_string(&ast).unwrap(); assert_eq!( json, - r#"{"leaf":{"type":"range","field":"price","lower":{"type":"inclusive","value":"10"},"upper":{"type":"unbounded"}}}"# + r#"{"type":"range","field":"price","lower":{"type":"inclusive","value":"10"},"upper":{"type":"unbounded"}}"# ); } @@ -363,7 +365,7 @@ mod tests { let json = serde_json::to_string(&boost_ast).unwrap(); assert_eq!( json, - r#"{"boost":[{"leaf":{"type":"all"}},2.5]}"# + r#"{"boost":[{"type":"all"},2.5]}"# ); } @@ -382,7 +384,7 @@ mod tests { let json = serde_json::to_string(&clause).unwrap(); assert_eq!( json, - r#"{"clause":[["must",{"leaf":{"type":"all"}}],["should",{"leaf":{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}}]]}"# + r#"{"clause":[["must",{"type":"all"}],["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}]]}"# ); } }