pub struct TermQuery { /* fields omitted */ }
A Term query matches all of the documents
containing a specific term.
The score associated is defined as
idf
* sqrt(term_freq
/ field norm
)
in which :
idf
- inverse document frequency.
term_freq
- number of occurrences of the term in the field
field norm
- number of tokens in the field.
#[macro_use]
extern crate tantivy;
use tantivy::schema::{SchemaBuilder, TEXT, IndexRecordOption};
use tantivy::{Index, Result, Term};
use tantivy::collector::{CountCollector, TopCollector, chain};
use tantivy::query::TermQuery;
fn example() -> Result<()> {
let mut schema_builder = SchemaBuilder::new();
let title = schema_builder.add_text_field("title", TEXT);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
{
let mut index_writer = index.writer(3_000_000)?;
index_writer.add_document(doc!(
title => "The Name of the Wind",
));
index_writer.add_document(doc!(
title => "The Diary of Muadib",
));
index_writer.add_document(doc!(
title => "A Dairy Cow",
));
index_writer.add_document(doc!(
title => "The Diary of a Young Girl",
));
index_writer.commit()?;
}
index.load_searchers()?;
let searcher = index.searcher();
{
let mut top_collector = TopCollector::with_limit(2);
let mut count_collector = CountCollector::default();
{
let mut collectors = chain().push(&mut top_collector).push(&mut count_collector);
let query = TermQuery::new(
Term::from_field_text(title, "diary"),
IndexRecordOption::Basic,
);
searcher.search(&query, &mut collectors).unwrap();
}
assert_eq!(count_collector.count(), 2);
assert!(top_collector.at_capacity());
}
Ok(())
}
Creates a new term query.
The Term
this query is built out of.
Returns a weight object.
While .weight(...)
returns a boxed trait object,
this method return a specific implementation.
This is useful for optimization purpose.
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
Create the weight associated to a query. Read more
Extract all of the terms associated to the query and insert them in the term set given in arguments. Read more
Returns the number of documents matching the query.