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.

101 lines
3.7KB

  1. use tantivy::Index;
  2. use tantivy::schema::{Field, Schema};
  3. use tantivy::query::QueryParser;
  4. use std::path::Path;
  5. use std::io::BufReader;
  6. use std::io::BufRead;
  7. use std::io;
  8. use std::fs::File;
  9. use tantivy::collector::chain;
  10. use tantivy::collector::TopCollector;
  11. use tantivy::collector::CountCollector;
  12. use clap::ArgMatches;
  13. use std::path::PathBuf;
  14. pub fn run_bench_cli(matches: &ArgMatches) -> Result<(), String> {
  15. let index_path = PathBuf::from(matches.value_of("index").unwrap());
  16. let queries_path = PathBuf::from(matches.value_of("queries").unwrap()); // the unwrap is safe as long as it is comming from the main cli.
  17. let num_repeat = value_t!(matches, "num_repeat", usize).map_err(|e| format!("Failed to read num_repeat argument as an integer. {:?}", e))?;
  18. run_bench(&index_path, &queries_path, num_repeat).map_err(From::from)
  19. }
  20. fn extract_search_fields(schema: &Schema) -> Vec<Field> {
  21. schema.fields()
  22. .iter()
  23. .enumerate()
  24. .filter(|&(_, field_entry)| {
  25. field_entry.is_indexed()
  26. })
  27. .map(|(field_id, _)| Field(field_id as u32))
  28. .collect()
  29. }
  30. fn read_query_file(query_path: &Path) -> io::Result<Vec<String>> {
  31. let query_file: File = File::open(&query_path)?;
  32. let file = BufReader::new(&query_file);
  33. let mut queries = Vec::new();
  34. for line_res in file.lines() {
  35. queries.push(line_res?);
  36. }
  37. Ok(queries)
  38. }
  39. fn run_bench(index_path: &Path,
  40. query_filepath: &Path,
  41. num_repeat: usize) -> Result<(), String> {
  42. println!("index_path : {:?}", index_path);
  43. println!("Query : {:?}", index_path);
  44. println!("-------------------------------\n\n\n");
  45. let index = Index::open_in_dir(index_path).map_err(|e| format!("Failed to open index.\n{:?}", e))?;
  46. let searcher = index.searcher();
  47. let default_search_fields: Vec<Field> = extract_search_fields(&index.schema());
  48. let queries = read_query_file(query_filepath).map_err(|e| format!("Failed reading the query file: {}", e))?;
  49. let query_parser = QueryParser::new(index.schema(), default_search_fields, index.tokenizers().clone());
  50. println!("SEARCH\n");
  51. println!("{}\t{}\t{}\t{}", "query", "num_terms", "num hits", "time in microsecs");
  52. for _ in 0..num_repeat {
  53. for query_txt in &queries {
  54. let query = query_parser.parse_query(&query_txt).unwrap();
  55. // let num_terms = query.num_terms();
  56. let mut top_collector = TopCollector::with_limit(10);
  57. let mut count_collector = CountCollector::default();
  58. {
  59. let mut collector = chain().push(&mut top_collector).push(&mut count_collector);
  60. query.search(&searcher, &mut collector)
  61. .map_err(|e| format!("Failed while searching query {:?}.\n\n{:?}", query_txt, e))?;
  62. }
  63. }
  64. }
  65. println!("\n\nFETCH STORE\n");
  66. println!("{}\t{}", "query", "time in microsecs");
  67. for _ in 0..num_repeat {
  68. for query_txt in &queries {
  69. let query = query_parser.parse_query(&query_txt).unwrap();
  70. let mut top_collector = TopCollector::with_limit(10);
  71. query.search(&searcher, &mut top_collector)
  72. .map_err(|e| format!("Failed while retrieving document for query {:?}.\n{:?}", query, e))?;
  73. /*
  74. let mut timer = TimerTree::default();
  75. {
  76. let _scoped_timer_ = timer.open("total");
  77. for doc_address in top_collector.docs() {
  78. searcher.doc(&doc_address).unwrap();
  79. }
  80. }
  81. println!("{}\t{}", query_txt, timer.total_time());
  82. */
  83. }
  84. }
  85. Ok(())
  86. }