Struct tantivy::query::TermQuery[][src]

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(())
}

Methods

impl TermQuery
[src]

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.

Trait Implementations

impl Clone for TermQuery
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for TermQuery
[src]

Formats the value using the given formatter. Read more

impl Query for TermQuery
[src]

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.

Search works as follows : Read more

Auto Trait Implementations

impl Send for TermQuery

impl Sync for TermQuery