Skip to content

Instantly share code, notes, and snippets.

@hermeGarcia
Last active February 14, 2024 15:31
Show Gist options
  • Save hermeGarcia/47d958fb62b603be4b44032ea7b51003 to your computer and use it in GitHub Desktop.
Save hermeGarcia/47d958fb62b603be4b44032ea7b51003 to your computer and use it in GitHub Desktop.
Unexpected behaviour using MustNot, checked till 0.21.1
use tantivy::collector::DocSetCollector;
use tantivy::query::{AllQuery, BooleanQuery, Occur, TermQuery};
use tantivy::schema::IndexRecordOption;
use tantivy::schema::Schema;
use tantivy::schema::Term;
use tantivy::schema::{STORED, STRING};
use tantivy::Index;
let dir = tempfile::tempdir().unwrap();
let mut sb = Schema::builder();
let uuid = sb.add_text_field("uuid", STRING | STORED);
let schema = sb.build();
let index_builder = Index::builder().schema(schema.clone());
let index = index_builder.create_in_dir(&dir.path()).expect("Index directory should exist");
let mut writer = index.writer_with_num_threads(1, 6_000_000).unwrap();
let this_doc = tantivy::doc!(
uuid => "this"
);
let that_doc = tantivy::doc!(
uuid => "that"
);
writer.add_document(this_doc).unwrap();
writer.add_document(that_doc).unwrap();
writer.commit().unwrap();
let reader = index.reader().unwrap();
let searcher = reader.searcher();
assert_eq!(searcher.num_docs(), 2);
let query = TermQuery::new(Term::from_field_text(uuid, "this"), IndexRecordOption::Basic);
let collector = DocSetCollector;
let results = searcher.search(&query, &collector).unwrap();
assert_eq!(results.len(), 1);
// For some reason this query yields no results, although one is expected.
let unexpected_result_query = BooleanQuery::new(vec![(Occur::MustNot, Box::new(query.clone()))]);
let results = searcher.search(&unexpected_result_query, &collector).unwrap();
assert_eq!(results.len(), 0);
// Turns out we can get the query to work as expected by adding an AllQuery
let bypass = BooleanQuery::new(vec![(Occur::Must, Box::new(AllQuery)), (Occur::MustNot, Box::new(query))]);
let results = searcher.search(&bypass, &collector).unwrap();
assert_eq!(results.len(), 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment