feat: Use RustyLine in the query-database example

This commit is contained in:
Clément Renault 2019-07-26 13:27:38 +02:00
parent bf3c2c3725
commit a488c00a2e
2 changed files with 60 additions and 55 deletions

View File

@ -18,6 +18,7 @@ meilidb-core = { path = "../meilidb-core", version = "0.1.0" }
quickcheck = "0.8.2" quickcheck = "0.8.2"
rand = "0.6.5" rand = "0.6.5"
rand_xorshift = "0.1.1" rand_xorshift = "0.1.1"
rustyline = { version = "5.0.0", default-features = false }
serde = { version = "1.0.91" , features = ["derive"] } serde = { version = "1.0.91" , features = ["derive"] }
serde_json = "1.0.39" serde_json = "1.0.39"
structopt = "0.2.15" structopt = "0.2.15"

View File

@ -3,16 +3,17 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
use std::collections::btree_map::{BTreeMap, Entry}; use std::collections::btree_map::{BTreeMap, Entry};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use std::io::{self, Write};
use std::time::{Instant, Duration};
use std::path::PathBuf;
use std::error::Error; use std::error::Error;
use std::io::{self, Write};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::time::{Instant, Duration};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use structopt::StructOpt; use structopt::StructOpt;
use meilidb_core::Highlight; use rustyline::{Editor, Config};
use meilidb_core::Highlight;
use meilidb_data::Database; use meilidb_data::Database;
use meilidb_schema::SchemaAttr; use meilidb_schema::SchemaAttr;
@ -140,9 +141,6 @@ fn main() -> Result<(), Box<dyn Error>> {
let start = Instant::now(); let start = Instant::now();
let database = Database::start_default(&opt.database_path)?; let database = Database::start_default(&opt.database_path)?;
let mut buffer = String::new();
let input = io::stdin();
let index = database.open_index("test")?.unwrap(); let index = database.open_index("test")?.unwrap();
let schema = index.schema(); let schema = index.schema();
@ -151,17 +149,17 @@ fn main() -> Result<(), Box<dyn Error>> {
let fields = opt.displayed_fields.iter().map(String::as_str); let fields = opt.displayed_fields.iter().map(String::as_str);
let fields = HashSet::from_iter(fields); let fields = HashSet::from_iter(fields);
loop { let config = Config::builder().auto_add_history(true).build();
print!("Searching for: "); let mut readline = Editor::<()>::with_config(config);
io::stdout().flush()?; let _ = readline.load_history("query-history.txt");
if input.read_line(&mut buffer)? == 0 { break }
let query = buffer.trim_end_matches('\n');
for result in readline.iter("Searching for: ") {
match result {
Ok(query) => {
let start_total = Instant::now(); let start_total = Instant::now();
let builder = index.query_builder(); let builder = index.query_builder();
let documents = builder.query(query, 0..opt.number_results)?; let documents = builder.query(&query, 0..opt.number_results)?;
let mut retrieve_duration = Duration::default(); let mut retrieve_duration = Duration::default();
@ -208,8 +206,14 @@ fn main() -> Result<(), Box<dyn Error>> {
eprintln!("document field retrieve took {:.2?}", retrieve_duration); eprintln!("document field retrieve took {:.2?}", retrieve_duration);
eprintln!("===== Found {} results in {:.2?} =====", number_of_documents, start_total.elapsed()); eprintln!("===== Found {} results in {:.2?} =====", number_of_documents, start_total.elapsed());
buffer.clear(); },
Err(err) => {
println!("Error: {:?}", err);
break
}
}
} }
readline.save_history("query-history.txt").unwrap();
Ok(()) Ok(())
} }