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.

149 lines
4.9KB

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