Merge pull request #252 from meilisearch/examples-specify-index-name

Allow users to specify the index name to use with examples bins
This commit is contained in:
Clément Renault 2019-10-31 17:02:00 +01:00 committed by GitHub
commit b73874bf24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,14 +15,15 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use meilidb_core::{Database, Highlight, ProcessedUpdateResult}; use meilidb_core::{Database, Highlight, ProcessedUpdateResult};
use meilidb_schema::SchemaAttr; use meilidb_schema::SchemaAttr;
const INDEX_NAME: &str = "default";
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
struct IndexCommand { struct IndexCommand {
/// The destination where the database must be created. /// The destination where the database must be created.
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
database_path: PathBuf, database_path: PathBuf,
#[structopt(long, default_value = "default")]
index_name: String,
/// The csv file to index. /// The csv file to index.
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
csv_data_path: PathBuf, csv_data_path: PathBuf,
@ -44,6 +45,9 @@ struct SearchCommand {
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
database_path: PathBuf, database_path: PathBuf,
#[structopt(long, default_value = "default")]
index_name: String,
/// Timeout after which the search will return results. /// Timeout after which the search will return results.
#[structopt(long)] #[structopt(long)]
fetch_timeout_ms: Option<u64>, fetch_timeout_ms: Option<u64>,
@ -70,6 +74,9 @@ struct ShowUpdatesCommand {
/// The path of the database to work with. /// The path of the database to work with.
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
database_path: PathBuf, database_path: PathBuf,
#[structopt(long, default_value = "default")]
index_name: String,
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -98,12 +105,12 @@ fn index_command(command: IndexCommand, database: Database) -> Result<(), Box<dy
let (sender, receiver) = mpsc::sync_channel(100); let (sender, receiver) = mpsc::sync_channel(100);
let update_fn = move |update: ProcessedUpdateResult| sender.send(update.update_id).unwrap(); let update_fn = move |update: ProcessedUpdateResult| sender.send(update.update_id).unwrap();
let index = match database.open_index(INDEX_NAME) { let index = match database.open_index(&command.index_name) {
Some(index) => index, Some(index) => index,
None => database.create_index(INDEX_NAME).unwrap(), None => database.create_index(&command.index_name).unwrap(),
}; };
let done = database.set_update_callback(INDEX_NAME, Box::new(update_fn)); let done = database.set_update_callback(&command.index_name, Box::new(update_fn));
assert!(done, "could not set the index update function"); assert!(done, "could not set the index update function");
let env = &database.env; let env = &database.env;
@ -306,7 +313,7 @@ fn crop_text(
fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<dyn Error>> { fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<dyn Error>> {
let env = &database.env; let env = &database.env;
let index = database let index = database
.open_index(INDEX_NAME) .open_index(&command.index_name)
.expect("Could not find index"); .expect("Could not find index");
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();
@ -429,12 +436,12 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<
} }
fn show_updates_command( fn show_updates_command(
_command: ShowUpdatesCommand, command: ShowUpdatesCommand,
database: Database, database: Database,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let env = &database.env; let env = &database.env;
let index = database let index = database
.open_index(INDEX_NAME) .open_index(&command.index_name)
.expect("Could not find index"); .expect("Could not find index");
let reader = env.read_txn().unwrap(); let reader = env.read_txn().unwrap();