use crate::core::document::document::Document;
use crate::core::document::field::Store;
use crate::core::document::field_type::FieldType;
use crate::core::document::sorted_doc_values_field::SortedDocValuesField;
use crate::core::document::string_field::StringField;
use crate::core::index::BytesRef;
use crate::core::index::directory_reader;
use crate::core::index::index_reader::IndexReader;
use crate::core::index::index_reader_context::IndexReaderContext;
use crate::core::index::multi_reader::MultiReader;
use crate::core::index::standard_directory_reader::StandardDirectoryReader;
use crate::core::index::term::Term;
use crate::core::search::boolean_clause::Occur;
use crate::core::search::boolean_query::Builder;
use crate::core::search::constant_score_query::ConstantScoreQuery;
use crate::core::search::field_comparator::FieldComparatorValue;
use crate::core::search::field_doc::FieldDoc;
use crate::core::search::index_searcher::{self, IndexSearcher, IndexSearcherHook};
use crate::core::search::match_all_docs_query::MatchAllDocsQuery;
use crate::core::search::match_no_docs_query::MatchNoDocsQuery;
use crate::core::search::query::Query;
use crate::core::search::query::QueryWeight;
use crate::core::search::query_caching_policy::{QueryCachingPolicy, QueryCachingPolicyEnum};
use crate::core::search::score_doc::ScoreDoc;
use crate::core::search::sort::Sort;
use crate::core::search::sort_field::{SortField, SortFieldType};
use crate::core::search::term_query::TermQuery;
use crate::core::search::{QueryCache, QueryCacheEnum};
use crate::core::store::directory::DirEnum;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::test_framework::core::index::random_index_writer::RandomIndexWriter;
use crate::test_framework::core::search::test_index_searcher::{
GetSlicesIndexSearcher, SegmentPartitionsSameSliceIndexSearcher,
SlicesOffloadedToExecutorIndexSearcher,
};
use crate::test_framework::core::util::lucene_test_case::{
at_least, is_light_mode, new_directory_shared, new_search_executor, new_string_field, random,
};
use rand::RngExt;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[allow(dead_code)]
pub struct TestIndexSearcher {
pub(crate) dir: Arc<DirEnum>,
pub(crate) reader: StandardDirectoryReader<DirEnum>,
}
impl TestIndexSearcher {
pub fn set_up<R>(random: &mut R) -> Result<Self>
where
R: rand::Rng + ?Sized,
{
if is_light_mode() {
let dir = LIGHT_DIR.clone();
return Ok(Self {
reader: directory_reader::open(dir.clone())?,
dir,
});
}
let dir = Self::build_set_up(random)?;
Ok(Self {
reader: directory_reader::open(dir.clone())?,
dir,
})
}
fn build_set_up<R>(random: &mut R) -> Result<Arc<DirEnum>>
where
R: rand::Rng + ?Sized,
{
let dir = new_directory_shared(random)?;
let iw = RandomIndexWriter::new(random, dir.clone())?;
let mut field_to_type: HashMap<String, FieldType> = HashMap::new();
for i in 0..100 {
let mut doc = Document::new();
doc.add(new_string_field(
random,
"field",
i.to_string(),
Store::No,
&mut field_to_type,
)?);
let field2_value = if i % 2 == 0 { "true" } else { "false" };
doc.add(new_string_field(
random,
"field2",
field2_value,
Store::No,
&mut field_to_type,
)?);
doc.add(SortedDocValuesField::new(
"field2",
BytesRef::from_string(field2_value),
));
iw.add_document(random, doc)?;
if random.random_bool(0.5) {
iw.commit(random)?;
}
}
iw.close(random)?;
Ok(dir)
}
}
static LIGHT_DIR: std::sync::LazyLock<Arc<DirEnum>> = std::sync::LazyLock::new(|| {
let mut random = random();
TestIndexSearcher::build_set_up(&mut random).expect("failed to initialize TestIndexSearcher")
});
#[test]
fn test_huge_n() -> Result<()> {
let mut random = random();
let TestIndexSearcher { dir: _dir, reader } = TestIndexSearcher::set_up(&mut random)?;
let reader = Arc::new(reader);
let searchers = vec![
index_searcher::from_reader(reader.clone())?,
index_searcher::from_reader_with_executor(reader.clone(), new_search_executor(4)?)?,
];
let queries: Vec<Query> = vec![
MatchAllDocsQuery::new().into(),
TermQuery::new(Term::from_text("field", "1")).into(),
];
let sorts = [
None,
Some(Sort::with_fields(vec![SortField::new(
Some("field2"),
SortFieldType::String,
)?])?),
];
let afters: [Option<FieldDoc>; 2] = [
None,
Some(FieldDoc::with_fields(
0,
0.0,
vec![FieldComparatorValue::TermVal(BytesRef::from_string("boo!"))],
)),
];
for searcher in &searchers {
for after in &afters {
for query in &queries {
for sort in &sorts {
searcher.search(query.clone(), usize::MAX)?;
searcher.search_after_score(
after.as_ref().map(|a| a.base.clone()),
query.clone(),
usize::MAX,
)?;
if let Some(sort) = sort {
searcher.search_with_sort(query.clone(), usize::MAX, sort.clone())?;
searcher.search_with_sort_score(query.clone(), usize::MAX, sort.clone(), true)?;
searcher.search_with_sort_score(query.clone(), usize::MAX, sort.clone(), false)?;
searcher.search_after(after.clone(), query.clone(), usize::MAX, sort.clone())?;
searcher.search_after_field_with_score(
after.clone(),
query.clone(),
usize::MAX,
sort.clone(),
true,
)?;
searcher.search_after_field_with_score(
after.clone(),
query.clone(),
usize::MAX,
sort.clone(),
false,
)?;
}
}
}
}
}
Ok(())
}
#[test]
fn test_search_after_passed_max_doc() -> Result<()> {
let mut random = random();
let dir = new_directory_shared(&mut random)?;
let w = RandomIndexWriter::new(&mut random, dir.clone())?;
w.add_document(&mut random, Document::new())?;
let r = w.get_reader(&mut random)?;
w.close(&mut random)?;
let max_doc = r.max_doc()?;
let s = index_searcher::from_reader(r)?;
let err = match s.search_after_score(
Some(ScoreDoc::new(max_doc, 0.54)),
MatchAllDocsQuery::new(),
10,
) {
Ok(_) => panic!("searchAfter after maxDoc should fail"),
Err(err) => err,
};
assert!(matches!(err.kind(), LuceneErrorKind::IllegalArgument(_)));
Ok(())
}
#[test]
fn test_count() -> Result<()> {
let mut random = random();
let dir = new_directory_shared(&mut random)?;
let w = RandomIndexWriter::new(&mut random, dir.clone())?;
let num_docs = at_least(&mut random, 100);
for _ in 0..num_docs {
let mut doc = Document::new();
if random.random_bool(0.5) {
doc.add(StringField::from_string("foo", "bar", Store::No)?);
}
if random.random_bool(0.5) {
doc.add(StringField::from_string("foo", "baz", Store::No)?);
}
if random.random_range(0..100) == 0 {
doc.add(StringField::from_string("delete", "yes", Store::No)?);
}
w.add_document(&mut random, doc)?;
}
for delete in [false, true] {
if delete {
w.delete_documents_with_terms(&mut random, vec![Term::from_text("delete", "yes")])?;
}
let reader = w.get_reader(&mut random)?;
let searcher = index_searcher::from_reader(reader)?;
let mut boolean_query = Builder::new();
boolean_query.add(TermQuery::new(Term::from_text("foo", "bar")), Occur::Should)?;
boolean_query.add(TermQuery::new(Term::from_text("foo", "baz")), Occur::Should)?;
let queries: Vec<Query> = vec![
MatchAllDocsQuery::new().into(),
MatchNoDocsQuery::new().into(),
TermQuery::new(Term::from_text("foo", "bar")).into(),
ConstantScoreQuery::new(TermQuery::new(Term::from_text("foo", "baz"))).into(),
boolean_query.build().into(),
];
for query in queries {
assert_eq!(
searcher.count(query.clone())?,
searcher.search(query, 1)?.total_hits.value as i32
);
}
}
w.close(&mut random)?;
Ok(())
}
#[test]
fn test_get_query_cache() -> Result<()> {
let mut searcher = index_searcher::from_reader(MultiReader::empty()?)?;
assert!(searcher.get_query_cache().is_some());
let dummy_cache = QueryCacheEnum::custom(DummyQueryCache);
searcher.set_query_cache(Some(dummy_cache));
assert!(matches!(
searcher.get_query_cache(),
Some(QueryCacheEnum::Custom(_))
));
searcher.set_query_cache(None);
assert!(searcher.get_query_cache().is_none());
Ok(())
}
#[test]
fn test_get_query_caching_policy() -> Result<()> {
let mut searcher = index_searcher::from_reader(MultiReader::empty()?)?;
assert!(matches!(
searcher.get_query_caching_policy().as_ref(),
QueryCachingPolicyEnum::UsageTracking(_)
));
searcher.set_query_caching_policy(QueryCachingPolicyEnum::custom(DummyQueryCachingPolicy));
assert!(matches!(
searcher.get_query_caching_policy().as_ref(),
QueryCachingPolicyEnum::Custom(_)
));
Ok(())
}
#[test]
fn test_get_slices_no_leaves_no_executor() -> Result<()> {
let searcher = index_searcher::from_reader(MultiReader::empty()?)?;
let slices = searcher.get_slices()?;
assert_eq!(0, slices.len());
Ok(())
}
#[test]
fn test_get_slices_no_leaves_with_executor() -> Result<()> {
let searcher =
index_searcher::from_reader_with_executor(MultiReader::empty()?, new_search_executor(1)?)?;
let slices = searcher.get_slices()?;
assert_eq!(0, slices.len());
Ok(())
}
#[test]
fn test_get_slices() -> Result<()> {
let mut random = random();
let dir = new_directory_shared(&mut random)?;
let w = RandomIndexWriter::new(&mut random, dir.clone())?;
for _ in 0..10 {
w.add_document(&mut random, Document::new())?;
w.flush()?;
}
let r = Arc::new(w.get_reader(&mut random)?);
w.close(&mut random)?;
let context = r.clone().get_context()?;
let leaves_len = context.leaves()?.len();
let searcher = IndexSearcher::new(context)?;
let slices = searcher.get_slices()?;
assert_eq!(1, slices.len());
assert_eq!(leaves_len, slices[0].partitions.len());
let context = r.get_context()?;
let searcher = IndexSearcher::with_executor(context, new_search_executor(2)?)?
.with_hook(IndexSearcherHook::GetSlices(GetSlicesIndexSearcher));
let slices = searcher.get_slices()?;
for slice in slices.iter() {
assert_eq!(1, slice.partitions.len());
}
assert_eq!(leaves_len, slices.len());
Ok(())
}
#[test]
fn test_slices_offloaded_to_the_executor() -> Result<()> {
let mut random = random();
let TestIndexSearcher { dir: _dir, reader } = TestIndexSearcher::set_up(&mut random)?;
let context = reader.get_context()?;
let leaves_len = context.leaves()?.len();
let mut searcher =
IndexSearcher::with_executor(context, new_search_executor(leaves_len.max(1))?)?.with_hook(
IndexSearcherHook::SlicesOffloadedToExecutor(SlicesOffloadedToExecutorIndexSearcher),
);
let num_executions = Arc::new(AtomicUsize::new(0));
searcher.set_offloaded_slice_counter(num_executions.clone());
searcher.search(MatchAllDocsQuery::new(), 10)?;
let expected_executions = leaves_len.saturating_sub(1);
assert_eq!(expected_executions, num_executions.load(Ordering::SeqCst));
Ok(())
}
#[test]
fn test_null_executor_non_null_task_executor() -> Result<()> {
let searcher = index_searcher::from_reader(MultiReader::empty()?)?;
let _ = searcher.get_task_executor();
Ok(())
}
#[test]
fn test_segment_partitions_same_slice() -> Result<()> {
let mut random = random();
let TestIndexSearcher { dir: _dir, reader } = TestIndexSearcher::set_up(&mut random)?;
let context = reader.get_context()?;
let searcher = IndexSearcher::with_executor(context, new_search_executor(2)?)?.with_hook(
IndexSearcherHook::SegmentPartitionsSameSlice(SegmentPartitionsSameSliceIndexSearcher),
);
for ctx in searcher.get_leaf_contexts()? {
if ctx.reader().max_doc()? <= 1 {
return Ok(());
}
}
let error = match searcher.get_slices() {
Ok(_) => panic!("get_slices should reject multiple partitions of the same leaf in one slice"),
Err(error) => error,
};
assert!(error.is_illegal_state_error());
assert_eq!(
"The same slice targets multiple leaf partitions of the same leaf reader context. A physical segment should rather get partitioned to be searched concurrently from as many slices as the number of leaf partitions it is split into.",
error.to_string()
);
Ok(())
}
struct DummyQueryCache;
impl<IRC> QueryCache<IRC> for DummyQueryCache
where
IRC: IndexReaderContext + 'static,
{
fn do_cache(
&self,
weight: QueryWeight<IRC>,
_policy: Arc<QueryCachingPolicyEnum>,
) -> Result<QueryWeight<IRC>>
where
IRC: IndexReaderContext + 'static,
{
Ok(weight)
}
}
struct DummyQueryCachingPolicy;
impl QueryCachingPolicy for DummyQueryCachingPolicy {
fn on_use(&self, _query: &Query) {}
fn should_cache(&self, _query: &Query) -> Result<bool> {
Ok(false)
}
}