From 904fd2f6d1b162dab5a20505b1bd23377ed708d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Wed, 14 Dec 2022 11:54:12 +0100 Subject: [PATCH] Add a search strategy option to the cli --- cli/src/main.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 6b7f2078b..c28d3de59 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::fmt::Display; use std::fs::File; use std::io::{stdin, BufRead, BufReader, Cursor, Read, Write}; use std::path::PathBuf; @@ -349,6 +350,29 @@ fn documents_from_csv(reader: impl Read) -> Result> { documents.into_inner().map_err(Into::into) } +#[derive(Debug, Clone, Copy)] +struct SearchStrategyOption(CriterionImplementationStrategy); +impl FromStr for SearchStrategyOption { + type Err = String; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "dynamic" => Ok(SearchStrategyOption(CriterionImplementationStrategy::Dynamic)), + "set" => Ok(SearchStrategyOption(CriterionImplementationStrategy::OnlySetBased)), + "iterative" => Ok(SearchStrategyOption(CriterionImplementationStrategy::OnlyIterative)), + _ => Err("could not parse {s} as a criterion implementation strategy, available options are `dynamic`, `set`, and `iterative`".to_owned()), + } + } +} +impl Display for SearchStrategyOption { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + CriterionImplementationStrategy::OnlyIterative => Display::fmt("iterative", f), + CriterionImplementationStrategy::OnlySetBased => Display::fmt("set", f), + CriterionImplementationStrategy::Dynamic => Display::fmt("dynamic", f), + } + } +} + #[derive(Debug, StructOpt)] struct Search { query: Option, @@ -360,6 +384,8 @@ struct Search { limit: Option, #[structopt(short, long, conflicts_with = "query")] interactive: bool, + #[structopt(short, long)] + strategy: Option, } impl Performer for Search { @@ -379,6 +405,7 @@ impl Performer for Search { &self.filter, &self.offset, &self.limit, + &self.strategy, )?; let time = now.elapsed(); @@ -386,6 +413,7 @@ impl Performer for Search { let hits = serde_json::to_string_pretty(&jsons)?; println!("{}", hits); + eprintln!("found {} results in {:.02?}", jsons.len(), time); } _ => break, @@ -399,6 +427,7 @@ impl Performer for Search { &self.filter, &self.offset, &self.limit, + &self.strategy, )?; let time = now.elapsed(); @@ -420,6 +449,7 @@ impl Search { filter: &Option, offset: &Option, limit: &Option, + strategy: &Option, ) -> Result> { let txn = index.read_txn()?; let mut search = index.search(&txn); @@ -441,7 +471,10 @@ impl Search { if let Some(limit) = limit { search.limit(*limit); } - search.criterion_implementation_strategy(CriterionImplementationStrategy::OnlyIterative); + if let Some(strategy) = strategy { + search.criterion_implementation_strategy(strategy.0); + } + let result = search.execute()?; let fields_ids_map = index.fields_ids_map(&txn)?;