From 780ca2a819a6d1c931580c8e8e31657072971876 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 24 Aug 2020 14:13:56 -0700 Subject: more progress and tests --- src/lib.rs | 94 +++++++++++++++++++++++------ tests/files/DELETE_scroll_body.txt | 4 -- tests/files/DELETE_scroll_path.txt | 1 - tests/files/POST_scroll_continue.txt | 5 -- tests/files/scroll/DELETE_scroll_body.txt | 4 ++ tests/files/scroll/DELETE_scroll_path.txt | 1 + tests/files/scroll/POST_scroll_continue.txt | 5 ++ tests/parse_es_requests.rs | 23 ++++++- 8 files changed, 105 insertions(+), 32 deletions(-) delete mode 100644 tests/files/DELETE_scroll_body.txt delete mode 100644 tests/files/DELETE_scroll_path.txt delete mode 100644 tests/files/POST_scroll_continue.txt create mode 100644 tests/files/scroll/DELETE_scroll_body.txt create mode 100644 tests/files/scroll/DELETE_scroll_path.txt create mode 100644 tests/files/scroll/POST_scroll_continue.txt diff --git a/src/lib.rs b/src/lib.rs index 615e4ed..7d26360 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,7 @@ URL query parameters: // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html #[derive(Serialize, Deserialize, Debug)] +#[serde(deny_unknown_fields)] pub struct SearchBody { pub query: Option, pub highlight: Option, @@ -61,13 +62,13 @@ pub struct SearchBody { // script_fields disabled // https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html - // pub sort: Option>, // XXX: array of object or string + pub sort: Option>, pub slice: Option, pub stored_fields: Option, // array of strings, or "_none_" // overlap with URL query parameters - pub docvalue_fields: Option>, // XXX: array of strings or objects? {field, format} in the objects + pub docvalue_fields: Option>, pub explain: Option, pub from: Option, pub min_score: Option, @@ -80,6 +81,13 @@ pub struct SearchBody { // not enumerated in direct docs, but seems to be allowed? } +#[derive(Serialize, Deserialize, Debug)] +#[serde(deny_unknown_fields)] +pub struct ScrollBody { + pub scroll_id: String, + pub scroll: Option, +} + #[derive(Serialize, Deserialize, Debug)] pub struct ApiSlice { id: u32, @@ -93,31 +101,26 @@ pub struct ApiRescore{ pub window_size: Option, } +// TODO: could revert to having query types as an enum, with flattening #[derive(Serialize, Deserialize, Debug)] -pub enum ApiQuery { +pub struct ApiQuery { // compound queries #[serde(rename = "bool")] - Bool(HashMap), - #[serde(rename = "boosting")] - Boosting {positive: Box, negative: Box, negative_boost: f64}, - #[serde(rename = "constant_score")] - ConstantScore {filter: Box, boost: Option}, + bool_query: Option, + boosting: Option, + constant_score: Option, // fulltext (leaf) queries // term-level (leaf) queries #[serde(rename = "match")] - Match(HashMap), - #[serde(rename = "match_phrase")] - MatchPhrase(HashMap), - #[serde(rename = "query_string")] - QueryString(QueryField), + match_query: Option>, + match_phrase: Option>, + query_string: Option, // other - #[serde(rename = "nested")] - Nested(NestedQuery), - #[serde(rename = "rescore_query")] - Rescore(Box), + nested: Option, + rescore_query: Option>, } #[derive(Serialize, Deserialize, Debug)] @@ -129,6 +132,27 @@ pub struct ApiHighlight{ settings: HighlightField, } +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +pub enum SortMapValue { + String(String), + Object { order: String, mode: Option }, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +pub enum SortElement{ + String(String), + Object(HashMap), +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +pub enum DocValOrString { + String(String), + Object {field: String, format: Option}, +} + #[derive(Serialize, Deserialize, Debug)] #[serde(untagged)] pub enum QueryFieldOrString { @@ -145,13 +169,36 @@ pub struct QueryField{ } #[derive(Serialize, Deserialize, Debug)] -pub struct NestedQuery{ +pub struct BoolQuery { + must: Option>, + filter: Option>, + should: Option>, + must_not: Option>, + minimum_should_match: Option, + boost: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct NestedQuery { path: String, query: Box, score_mode: Option, ignore_unmapped: Option, } +#[derive(Serialize, Deserialize, Debug)] +pub struct BoostingQuery { + positive: Box, + negative: Box, + negative_boost: f64, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct ConstantScoreQuery { + filter: Box, + boost: Option, +} + // https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html #[derive(Serialize, Deserialize, Debug)] pub struct HighlightField{ @@ -181,13 +228,20 @@ pub struct HighlightField{ #[derive(Serialize, Deserialize, Debug)] pub struct ApiCollapse{ field: String, - inner_hits: Option, + inner_hits: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +pub enum InnerHitsOneOrMore { + Single(InnerHits), + Multiple(Vec), } #[derive(Serialize, Deserialize, Debug)] pub struct InnerHits { from: Option, size: Option, - sort: Option>, + sort: Option>, name: Option, } diff --git a/tests/files/DELETE_scroll_body.txt b/tests/files/DELETE_scroll_body.txt deleted file mode 100644 index 26960a9..0000000 --- a/tests/files/DELETE_scroll_body.txt +++ /dev/null @@ -1,4 +0,0 @@ -DELETE /_search/scroll -{ - "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" -} diff --git a/tests/files/DELETE_scroll_path.txt b/tests/files/DELETE_scroll_path.txt deleted file mode 100644 index 7910ed8..0000000 --- a/tests/files/DELETE_scroll_path.txt +++ /dev/null @@ -1 +0,0 @@ -DELETE /_search/scroll/DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ== diff --git a/tests/files/POST_scroll_continue.txt b/tests/files/POST_scroll_continue.txt deleted file mode 100644 index 9aef4d3..0000000 --- a/tests/files/POST_scroll_continue.txt +++ /dev/null @@ -1,5 +0,0 @@ -POST /_search/scroll -{ - "scroll" : "1m", - "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" -} diff --git a/tests/files/scroll/DELETE_scroll_body.txt b/tests/files/scroll/DELETE_scroll_body.txt new file mode 100644 index 0000000..26960a9 --- /dev/null +++ b/tests/files/scroll/DELETE_scroll_body.txt @@ -0,0 +1,4 @@ +DELETE /_search/scroll +{ + "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" +} diff --git a/tests/files/scroll/DELETE_scroll_path.txt b/tests/files/scroll/DELETE_scroll_path.txt new file mode 100644 index 0000000..7910ed8 --- /dev/null +++ b/tests/files/scroll/DELETE_scroll_path.txt @@ -0,0 +1 @@ +DELETE /_search/scroll/DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ== diff --git a/tests/files/scroll/POST_scroll_continue.txt b/tests/files/scroll/POST_scroll_continue.txt new file mode 100644 index 0000000..9aef4d3 --- /dev/null +++ b/tests/files/scroll/POST_scroll_continue.txt @@ -0,0 +1,5 @@ +POST /_search/scroll +{ + "scroll" : "1m", + "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" +} diff --git a/tests/parse_es_requests.rs b/tests/parse_es_requests.rs index 57be06a..a4950c0 100644 --- a/tests/parse_es_requests.rs +++ b/tests/parse_es_requests.rs @@ -1,7 +1,7 @@ use std::fs; use std::ffi::OsStr; -use es_public_proxy::SearchBody; +use es_public_proxy::{ScrollBody, SearchBody}; mod common; @@ -22,7 +22,7 @@ fn basic_parse() { } #[test] -fn parse_all_requests() { +fn parse_search_requests() { let file_paths = fs::read_dir("tests/files").unwrap(); @@ -39,3 +39,22 @@ fn parse_all_requests() { } } } + +#[test] +fn parse_scroll_requests() { + + let file_paths = fs::read_dir("tests/files/scroll").unwrap(); + + for path in file_paths { + let path = path.unwrap().path(); + if path.extension() != Some(OsStr::new("txt")) { + continue + } + let request = common::load_request(&path); + if let Some(body) = request.body { + println!("parsing: {}", path.display()); + println!("BODY: {}", body); + let _parsed: ScrollBody = serde_json::from_str(&body).unwrap(); + } + } +} -- cgit v1.2.3