Browse Source

improved stdout perf

develop
Jonathan Strong 4 years ago
parent
commit
f73716d36a
1 changed files with 6 additions and 1 deletions
  1. +6
    -1
      src/commands/search.rs

+ 6
- 1
src/commands/search.rs View File

@@ -4,6 +4,7 @@ use std::str::FromStr;
use std::convert::From;
use std::path::Path;
use std::path::PathBuf;
use std::io::Write;
use tantivy;
use tantivy::query::QueryParser;
use tantivy::schema::Field;
@@ -47,6 +48,9 @@ fn run_search(directory: &Path, query: &str) -> tantivy::Result<()> {
let searcher = index.reader()?.searcher();
let weight = query.weight(&searcher, false)?;
let schema = index.schema();
let stdout = std::io::stdout();
let handle = stdout.lock();
let mut wtr = std::io::BufWriter::new(handle);
for segment_reader in searcher.segment_readers() {
let mut scorer = weight.scorer(segment_reader, 1.0)?;
let store_reader = segment_reader.get_store_reader();
@@ -54,7 +58,8 @@ fn run_search(directory: &Path, query: &str) -> tantivy::Result<()> {
let doc_id = scorer.doc();
let doc = store_reader.get(doc_id)?;
let named_doc = schema.to_named_doc(&doc);
println!("{}", serde_json::to_string(&named_doc).unwrap());
serde_json::to_writer(&mut wtr, &named_doc).unwrap();
wtr.write_all(b"\n")?;
}
}
Ok(())


Loading…
Cancel
Save