mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 23:04:26 +01:00
Accept queries from standard input when not given as argument
This commit is contained in:
parent
d6705d5529
commit
c84012d655
@ -1,4 +1,5 @@
|
||||
use std::io::{self, Write};
|
||||
use std::io::{self, Write, BufRead};
|
||||
use std::iter::once;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Instant;
|
||||
|
||||
@ -19,7 +20,7 @@ struct Opt {
|
||||
database: PathBuf,
|
||||
|
||||
/// The query string to search for (doesn't support prefix search yet).
|
||||
query: String,
|
||||
query: Option<String>,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
@ -34,10 +35,19 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
let index = Index::new(&env)?;
|
||||
|
||||
let before = Instant::now();
|
||||
let rtxn = env.read_txn()?;
|
||||
|
||||
let documents_ids = index.search(&rtxn, &opt.query)?;
|
||||
let stdin = io::stdin();
|
||||
let lines = match opt.query {
|
||||
Some(query) => Box::new(once(Ok(query.to_string()))),
|
||||
None => Box::new(stdin.lock().lines()) as Box<dyn Iterator<Item = _>>,
|
||||
};
|
||||
|
||||
for result in lines {
|
||||
let before = Instant::now();
|
||||
|
||||
let query = result?;
|
||||
let documents_ids = index.search(&rtxn, &query)?;
|
||||
let headers = match index.headers(&rtxn)? {
|
||||
Some(headers) => headers,
|
||||
None => return Ok(()),
|
||||
@ -53,6 +63,7 @@ fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
eprintln!("Took {:.02?} to find {} documents", before.elapsed(), documents_ids.len());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user