pub struct DisjunctionMaxQuery { /* private fields */ }Expand description
The disjunction max query returns documents matching one or more wrapped queries, called query clauses or clauses.
If a returned document matches multiple query clauses,
the DisjunctionMaxQuery assigns the document the highest relevance score from any matching
clause, plus a tie breaking increment for any additional matching subqueries.
use tantivy::collector::TopDocs;
use tantivy::doc;
use tantivy::query::{DisjunctionMaxQuery, Query, QueryClone, TermQuery};
use tantivy::schema::{IndexRecordOption, Schema, TEXT};
use tantivy::Term;
use tantivy::Index;
use tantivy::IndexWriter;
fn main() -> tantivy::Result<()> {
let mut schema_builder = Schema::builder();
let title = schema_builder.add_text_field("title", TEXT);
let body = schema_builder.add_text_field("body", TEXT);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
{
let mut index_writer: IndexWriter = index.writer(15_000_000)?;
index_writer.add_document(doc!(
title => "The Name of Girl",
))?;
index_writer.add_document(doc!(
title => "The Diary of Muadib",
))?;
index_writer.add_document(doc!(
title => "The Diary of Girl",
))?;
index_writer.commit()?;
}
let reader = index.reader()?;
let searcher = reader.searcher();
// Make TermQuery's for "girl" and "diary" in the title
let girl_term_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(title, "girl"),
IndexRecordOption::Basic,
));
let diary_term_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(title, "diary"),
IndexRecordOption::Basic,
));
// TermQuery "diary" and "girl" should be present and only one should be accounted in score
let queries1 = vec![diary_term_query.box_clone(), girl_term_query.box_clone()];
let diary_and_girl = DisjunctionMaxQuery::new(queries1);
let documents = searcher.search(&diary_and_girl, &TopDocs::with_limit(3))?;
assert_eq!(documents[0].0, documents[1].0);
assert_eq!(documents[1].0, documents[2].0);
// TermQuery "diary" and "girl" should be present
// and one should be accounted with multiplier 0.7
let queries2 = vec![diary_term_query.box_clone(), girl_term_query.box_clone()];
let tie_breaker = 0.7;
let diary_and_girl_with_tie_breaker = DisjunctionMaxQuery::with_tie_breaker(queries2, tie_breaker);
let documents = searcher.search(&diary_and_girl_with_tie_breaker, &TopDocs::with_limit(3))?;
assert_eq!(documents[1].0, documents[2].0);
// For this test all terms brings the same score. So we can do easy math and assume that
// `DisjunctionMaxQuery` with tie breakers score should be equal
// to term1 score + `tie_breaker` * term2 score or (1.0 + tie_breaker) * term score
assert!(f32::abs(documents[0].0 - documents[1].0 * (1.0 + tie_breaker)) < 0.001);
Ok(())
}Implementations§
Source§impl DisjunctionMaxQuery
impl DisjunctionMaxQuery
Sourcepub fn with_tie_breaker(
disjuncts: Vec<Box<dyn Query>>,
tie_breaker: Score,
) -> DisjunctionMaxQuery
pub fn with_tie_breaker( disjuncts: Vec<Box<dyn Query>>, tie_breaker: Score, ) -> DisjunctionMaxQuery
Creates a new DisjunctionMaxQuery with tie breaker.
Trait Implementations§
Source§impl Clone for DisjunctionMaxQuery
impl Clone for DisjunctionMaxQuery
Source§impl Debug for DisjunctionMaxQuery
impl Debug for DisjunctionMaxQuery
Source§impl Query for DisjunctionMaxQuery
impl Query for DisjunctionMaxQuery
Source§fn weight(&self, enable_scoring: EnableScoring<'_>) -> Result<Box<dyn Weight>>
fn weight(&self, enable_scoring: EnableScoring<'_>) -> Result<Box<dyn Weight>>
Create the weight associated with a query. Read more
Source§fn query_terms<'a>(&'a self, visitor: &mut dyn FnMut(&'a Term, bool))
fn query_terms<'a>(&'a self, visitor: &mut dyn FnMut(&'a Term, bool))
Extract all of the terms associated with the query and pass them to the
given closure. Read more
Source§fn explain(
&self,
searcher: &Searcher,
doc_address: DocAddress,
) -> Result<Explanation>
fn explain( &self, searcher: &Searcher, doc_address: DocAddress, ) -> Result<Explanation>
Returns an
Explanation for the score of the document.Auto Trait Implementations§
impl !RefUnwindSafe for DisjunctionMaxQuery
impl !UnwindSafe for DisjunctionMaxQuery
impl Freeze for DisjunctionMaxQuery
impl Send for DisjunctionMaxQuery
impl Sync for DisjunctionMaxQuery
impl Unpin for DisjunctionMaxQuery
impl UnsafeUnpin for DisjunctionMaxQuery
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<T> Fruit for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more