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,65 +149,71 @@ 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 } for result in readline.iter("Searching for: ") {
let query = buffer.trim_end_matches('\n'); match result {
Ok(query) => {
let start_total = Instant::now();
let start_total = Instant::now(); let builder = index.query_builder();
let documents = builder.query(&query, 0..opt.number_results)?;
let builder = index.query_builder(); let mut retrieve_duration = Duration::default();
let documents = builder.query(query, 0..opt.number_results)?;
let mut retrieve_duration = Duration::default(); let number_of_documents = documents.len();
for mut doc in documents {
let number_of_documents = documents.len(); doc.highlights.sort_unstable_by_key(|m| (m.char_index, m.char_length));
for mut doc in documents {
doc.highlights.sort_unstable_by_key(|m| (m.char_index, m.char_length)); let start_retrieve = Instant::now();
let result = index.document::<Document>(Some(&fields), doc.id);
retrieve_duration += start_retrieve.elapsed();
let start_retrieve = Instant::now(); match result {
let result = index.document::<Document>(Some(&fields), doc.id); Ok(Some(document)) => {
retrieve_duration += start_retrieve.elapsed(); for (name, text) in document {
print!("{}: ", name);
match result { let attr = schema.attribute(&name).unwrap();
Ok(Some(document)) => { let highlights = doc.highlights.iter()
for (name, text) in document { .filter(|m| SchemaAttr::new(m.attribute) == attr)
print!("{}: ", name); .cloned();
let (text, highlights) = crop_text(&text, highlights, opt.char_context);
let attr = schema.attribute(&name).unwrap(); let areas = create_highlight_areas(&text, &highlights);
let highlights = doc.highlights.iter() display_highlights(&text, &areas)?;
.filter(|m| SchemaAttr::new(m.attribute) == attr) println!();
.cloned(); }
let (text, highlights) = crop_text(&text, highlights, opt.char_context); },
let areas = create_highlight_areas(&text, &highlights); Ok(None) => eprintln!("missing document"),
display_highlights(&text, &areas)?; Err(e) => eprintln!("{}", e),
println!();
} }
},
Ok(None) => eprintln!("missing document"), let mut matching_attributes = HashSet::new();
Err(e) => eprintln!("{}", e), for highlight in doc.highlights {
let attr = SchemaAttr::new(highlight.attribute);
let name = schema.attribute_name(attr);
matching_attributes.insert(name);
}
let matching_attributes = Vec::from_iter(matching_attributes);
println!("matching in: {:?}", matching_attributes);
println!();
}
eprintln!("document field retrieve took {:.2?}", retrieve_duration);
eprintln!("===== Found {} results in {:.2?} =====", number_of_documents, start_total.elapsed());
},
Err(err) => {
println!("Error: {:?}", err);
break
} }
let mut matching_attributes = HashSet::new();
for highlight in doc.highlights {
let attr = SchemaAttr::new(highlight.attribute);
let name = schema.attribute_name(attr);
matching_attributes.insert(name);
}
let matching_attributes = Vec::from_iter(matching_attributes);
println!("matching in: {:?}", matching_attributes);
println!();
} }
eprintln!("document field retrieve took {:.2?}", retrieve_duration);
eprintln!("===== Found {} results in {:.2?} =====", number_of_documents, start_total.elapsed());
buffer.clear();
} }
readline.save_history("query-history.txt").unwrap();
Ok(()) Ok(())
} }