-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::path::Path; | ||
|
||
use clap::Args; | ||
use tabby_common::index::IndexSchema; | ||
use tantivy::{DocAddress, DocSet, Document, Index, TantivyDocument, Term, TERMINATED}; | ||
|
||
#[derive(Args)] | ||
pub struct HeadArgs { | ||
/// Number of documents to display | ||
#[clap(short, long, default_value = "1")] | ||
num_docs: usize, | ||
|
||
#[clap(short, long, default_value = "code")] | ||
corpus: String, | ||
} | ||
|
||
pub fn run_head_cli(index_path: &Path, args: &HeadArgs) -> anyhow::Result<()> { | ||
let index = | ||
Index::open_in_dir(index_path)?; | ||
|
||
let searcher = index.reader()?.searcher(); | ||
let schema = IndexSchema::instance(); | ||
|
||
let mut count = 0; | ||
'outer: for (segment_ordinal, segment_reader) in searcher.segment_readers().iter().enumerate() { | ||
let Ok(inverted_index) = segment_reader.inverted_index(schema.field_corpus) else { | ||
continue; | ||
}; | ||
|
||
let term_corpus = Term::from_field_text(schema.field_corpus, &args.corpus); | ||
let Ok(Some(mut postings)) = inverted_index.read_postings(&term_corpus, tantivy::schema::IndexRecordOption::Basic) else { | ||
continue; | ||
}; | ||
|
||
let mut doc_id = postings.doc(); | ||
while doc_id != TERMINATED { | ||
if !segment_reader.is_deleted(doc_id) { | ||
let doc_address = DocAddress::new(segment_ordinal as u32, doc_id); | ||
let doc: TantivyDocument = searcher.doc(doc_address).expect("Failed to read document"); | ||
|
||
let json = doc.to_json(&schema.schema); | ||
|
||
println!("{}", json); | ||
|
||
count += 1; | ||
if count >= args.num_docs { | ||
break 'outer; | ||
} | ||
} | ||
doc_id = postings.advance(); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod bench; | ||
mod inspect; | ||
mod head; | ||
|
||
pub use self::inspect::run_inspect_cli; | ||
pub use self::bench::{run_bench_cli, BenchArgs}; | ||
pub use self::bench::{run_bench_cli, BenchArgs}; | ||
pub use self::head::{run_head_cli, HeadArgs}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters