From c15360ddb3ab6c837fe72107463a516658ec2fed Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 18 Jan 2024 18:17:21 -0500 Subject: [PATCH] ElasticSearch test --- Cargo.lock | 1 + crates/lib/elasticsearch_schema/Cargo.toml | 3 +- .../src/searches/search_model_weights.rs | 67 ++++++++++++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcc378de3e..a2ac2e31bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2149,6 +2149,7 @@ dependencies = [ "errors", "log", "my-workspace-hack", + "regex", "serde", "serde_json", "tokens", diff --git a/crates/lib/elasticsearch_schema/Cargo.toml b/crates/lib/elasticsearch_schema/Cargo.toml index 37dac8d081..2f974bac32 100644 --- a/crates/lib/elasticsearch_schema/Cargo.toml +++ b/crates/lib/elasticsearch_schema/Cargo.toml @@ -27,6 +27,7 @@ chrono = { version = "0.4.22", features = ["serde"] } serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.108" my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } +regex = "1.10.2" [dev-dependencies] -# None yet +# None yet \ No newline at end of file diff --git a/crates/lib/elasticsearch_schema/src/searches/search_model_weights.rs b/crates/lib/elasticsearch_schema/src/searches/search_model_weights.rs index 85a5143744..1a2553e593 100644 --- a/crates/lib/elasticsearch_schema/src/searches/search_model_weights.rs +++ b/crates/lib/elasticsearch_schema/src/searches/search_model_weights.rs @@ -1,8 +1,7 @@ use elasticsearch::{Elasticsearch, SearchParts}; -use log::error; use serde_json::{json, Value}; -use enums::by_table::model_weights::weights_types::WeightsType; +use enums::by_table::model_weights::weights_types::WeightsType; use errors::AnyhowResult; use crate::documents::model_weight_document::{MODEL_WEIGHT_INDEX, ModelWeightDocument}; @@ -202,3 +201,67 @@ fn query_model_weights_with_model_weights_type(search_term: &str, weight_type: W } }) } + +#[cfg(test)] +mod tests { + use regex::Regex; + use crate::searches::search_model_weights::query_model_weights; + + #[test] + fn default_search_only_keyword() { + let json = query_model_weights("FOO_BAR_BAZ"); + let json = serde_json::to_string(&json).unwrap(); + + // NB: Keys in emitted JSON are sorted. + let expected_json = r#" + { + "query": { + "bool": { + "must": [ + { + "bool": { + "should": [ + { + "fuzzy": { + "title": { + "fuzziness": 2, + "value": "FOO_BAR_BAZ" + } + } + }, + { + "match": { + "title": { + "boost": 1, + "query": "FOO_BAR_BAZ" + } + } + }, + { + "multi_match": { + "boost": 50, + "fields": [ + "title", + "title._2gram", + "title._3gram" + ], + "query": "FOO_BAR_BAZ", + "type": "bool_prefix" + } + } + ] + } + } + ] + } + } + } + "#; + + let regex = Regex::new("\\s+").expect("regex should parse"); + + let expected_json = regex.replace_all(&expected_json, ""); + + assert_eq!(&json, &expected_json); + } +}