You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.8KB

  1. #[macro_use]
  2. extern crate clap;
  3. extern crate serde_json;
  4. #[macro_use]
  5. extern crate log;
  6. extern crate ansi_term;
  7. extern crate bincode;
  8. extern crate byteorder;
  9. extern crate chan;
  10. extern crate env_logger;
  11. extern crate futures;
  12. extern crate iron;
  13. extern crate mount;
  14. extern crate persistent;
  15. extern crate staticfile;
  16. extern crate tantivy;
  17. extern crate time;
  18. extern crate urlencoded;
  19. #[macro_use]
  20. extern crate serde_derive;
  21. use std::io::Write;
  22. use clap::{App, AppSettings, Arg, SubCommand};
  23. mod commands;
  24. pub mod timer;
  25. use self::commands::*;
  26. fn main() {
  27. env_logger::init().unwrap();
  28. let index_arg = Arg::with_name("index")
  29. .short("i")
  30. .long("index")
  31. .value_name("directory")
  32. .help("Tantivy index directory filepath")
  33. .required(true);
  34. let cli_options = App::new("Tantivy")
  35. .setting(AppSettings::SubcommandRequiredElseHelp)
  36. .version(env!("CARGO_PKG_VERSION"))
  37. .author("Paul Masurel <paul.masurel@gmail.com>")
  38. .about("Tantivy Search Engine's command line interface.")
  39. .subcommand(
  40. SubCommand::with_name("new")
  41. .about("Create a new index. The schema will be populated with a simple example schema")
  42. .arg(index_arg.clone())
  43. )
  44. .subcommand(
  45. SubCommand::with_name("serve")
  46. .about("Start a server")
  47. .arg(index_arg.clone())
  48. .arg(Arg::with_name("host")
  49. .long("host")
  50. .value_name("host")
  51. .help("host to listen to")
  52. )
  53. .arg(Arg::with_name("port")
  54. .short("p")
  55. .long("port")
  56. .value_name("port")
  57. .help("Port")
  58. .default_value("localhost")
  59. )
  60. )
  61. .subcommand(
  62. SubCommand::with_name("index")
  63. .about("Index files")
  64. .arg(index_arg.clone())
  65. .arg(Arg::with_name("file")
  66. .short("f")
  67. .long("file")
  68. .value_name("file")
  69. .help("File containing the documents to index."))
  70. .arg(Arg::with_name("num_threads")
  71. .short("t")
  72. .long("num_threads")
  73. .value_name("num_threads")
  74. .help("Number of indexing threads. By default num cores - 1 will be used")
  75. .default_value("3"))
  76. .arg(Arg::with_name("memory_size")
  77. .short("m")
  78. .long("memory_size")
  79. .value_name("memory_size")
  80. .help("Total memory_size in bytes. It will be split for the different threads.")
  81. .default_value("1000000000"))
  82. .arg(Arg::with_name("nomerge")
  83. .long("nomerge")
  84. .help("Do not merge segments"))
  85. )
  86. .subcommand(
  87. SubCommand::with_name("search")
  88. .about("Search an index.")
  89. .arg(index_arg.clone())
  90. .arg(Arg::with_name("query")
  91. .short("q")
  92. .long("query")
  93. .value_name("query")
  94. .help("Query")
  95. .required(true))
  96. )
  97. .subcommand(
  98. SubCommand::with_name("bench")
  99. .about("Run a benchmark on your index")
  100. .arg(index_arg.clone())
  101. .arg(Arg::with_name("queries")
  102. .short("q")
  103. .long("queries")
  104. .value_name("queries")
  105. .help("File containing queries (one per line) to run in the benchmark.")
  106. .required(true))
  107. .arg(Arg::with_name("num_repeat")
  108. .short("n")
  109. .long("num_repeat")
  110. .value_name("num_repeat")
  111. .help("Number of times to repeat the benchmark.")
  112. .default_value("1"))
  113. )
  114. .subcommand(
  115. SubCommand::with_name("merge")
  116. .about("Merge all the segments of an index")
  117. .arg(index_arg.clone())
  118. )
  119. .get_matches();
  120. let (subcommand, some_options) = cli_options.subcommand();
  121. let options = some_options.unwrap();
  122. let run_cli = match subcommand {
  123. "new" => run_new_cli,
  124. "index" => run_index_cli,
  125. "serve" => run_serve_cli,
  126. "search" => run_search_cli,
  127. "merge" => run_merge_cli,
  128. "bench" => run_bench_cli,
  129. _ => panic!("Subcommand {} is unknown", subcommand),
  130. };
  131. if let Err(ref e) = run_cli(options) {
  132. let stderr = &mut std::io::stderr();
  133. let errmsg = "Error writing ot stderr";
  134. writeln!(stderr, "{}", e).expect(errmsg);
  135. std::process::exit(1);
  136. }
  137. }