rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use crate::core::document::document::Document;
use crate::core::document::field_type::FieldType;
use crate::core::document::fields::Fields;
use crate::core::index::index_reader_context::IndexReaderContext;
use crate::core::index::live_index_writer_config::LiveIndexWriterConfig;
use crate::core::index::term::Term;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::regexp_query::RegexpQuery;
use crate::core::util::error::lucene_error::Result;
use crate::test_framework::core::index::random_index_writer::RandomIndexWriter;
use crate::test_framework::core::util::DefaultIndexSearchCR;
use crate::test_framework::core::util::lucene_test_case::{
  at_least, is_light_mode, new_directory_shared, new_field, new_index_writer_config,
  new_searcher_with_reader, random,
};
use crate::test_framework::core::util::test_util::TestUtil;
use rand::Rng;
use rand::RngExt;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock};

#[allow(dead_code)] // for quick search
pub struct TestRegexpRandom;

static LIGHT_SEARCHER: LazyLock<Arc<DefaultIndexSearchCR>> = LazyLock::new(|| {
  let mut random = random();
  Arc::new(build_set_up(&mut random).expect("failed to initialize TestRegexpRandom"))
});

fn set_up<R>(random: &mut R) -> Result<Arc<DefaultIndexSearchCR>>
where
  R: Rng + ?Sized,
{
  if is_light_mode() {
    return Ok(LIGHT_SEARCHER.clone());
  }

  Ok(Arc::new(build_set_up(random)?))
}

fn build_set_up<R>(random: &mut R) -> Result<DefaultIndexSearchCR>
where
  R: Rng + ?Sized,
{
  let dir = new_directory_shared(random)?;

  let mut config = new_index_writer_config(random)?;
  config.set_max_buffered_docs(TestUtil::next_int(random, 50, 1000));

  let writer = RandomIndexWriter::with_config(random, dir.clone(), config);
  let mut field_to_type = HashMap::new();

  let mut custom_type = FieldType::from_ref(&*crate::core::document::text_field::TYPE_STORED)?;
  custom_type.set_omit_norms(true)?;

  let mut doc = Document::new();
  doc.add(new_field(
    random,
    "field",
    "",
    &custom_type,
    &mut field_to_type,
  )?);

  for i in 0..1000 {
    let s = format!("{:03}", i);
    let Some(Fields::Field(field)) = doc.get_field_mut("field") else {
      unreachable!("field must exist")
    };
    field.set_string_value(s)?;
    writer.add_document(random, &mut doc)?;
  }

  let reader = writer.get_reader(random)?;
  writer.close(random)?;
  new_searcher_with_reader(reader)
}
fn n<R>(random: &mut R) -> char
where
  R: Rng + ?Sized,
{
  (0x30u8 + random.random_range(0..10) as u8) as char
}

fn fill_pattern<R>(random: &mut R, wildcard_pattern: &str) -> String
where
  R: Rng + ?Sized,
{
  let mut sb = String::new();
  for ch in wildcard_pattern.chars() {
    match ch {
      'N' => sb.push(n(random)),
      _ => sb.push(ch),
    }
  }
  sb
}

fn assert_pattern_hits<IRC, R, SIM>(
  random: &mut R,
  searcher: &IndexSearcher<IRC, SIM>,
  pattern: &str,
  num_hits: usize,
) -> Result<()>
where
  SIM: crate::core::search::similarities_impl::similarities::Similarity,
  R: Rng + ?Sized,
  IRC: IndexReaderContext + Sync,
  crate::core::search::index_searcher::IndexSearcher<IRC, SIM>: Sync,
{
  let wq = RegexpQuery::new(Term::from_text("field", fill_pattern(random, pattern)))?;
  let docs = searcher.search(wq, 25)?;
  assert_eq!(
    num_hits,
    docs.total_hits.value(),
    "Incorrect hits for pattern: {}",
    pattern
  );
  Ok(())
}

#[test]
fn test_regexps() -> Result<()> {
  let mut random = random();
  let searcher = set_up(&mut random)?;

  let num = at_least(&mut random, 1);
  for _ in 0..num {
    assert_pattern_hits(&mut random, &searcher, "NNN", 1)?;
    assert_pattern_hits(&mut random, &searcher, ".NN", 10)?;
    assert_pattern_hits(&mut random, &searcher, "N.N", 10)?;
    assert_pattern_hits(&mut random, &searcher, "NN.", 10)?;
  }

  for _ in 0..num {
    assert_pattern_hits(&mut random, &searcher, ".{1,2}N", 100)?;
    assert_pattern_hits(&mut random, &searcher, "N.{1,2}", 100)?;
    assert_pattern_hits(&mut random, &searcher, ".{1,3}", 1000)?;

    assert_pattern_hits(&mut random, &searcher, "NN[3-7]", 5)?;
    assert_pattern_hits(&mut random, &searcher, "N[2-6][3-7]", 25)?;
    assert_pattern_hits(&mut random, &searcher, "[1-5][2-6][3-7]", 125)?;
    assert_pattern_hits(&mut random, &searcher, "[0-4][3-7][4-8]", 125)?;
    assert_pattern_hits(&mut random, &searcher, "[2-6][0-4]N", 25)?;
    assert_pattern_hits(&mut random, &searcher, "[2-6]NN", 5)?;

    assert_pattern_hits(&mut random, &searcher, "NN.*", 10)?;
    assert_pattern_hits(&mut random, &searcher, "N.*", 100)?;
    assert_pattern_hits(&mut random, &searcher, ".*", 1000)?;

    assert_pattern_hits(&mut random, &searcher, ".*NN", 10)?;
    assert_pattern_hits(&mut random, &searcher, ".*N", 100)?;

    assert_pattern_hits(&mut random, &searcher, "N.*N", 10)?;

    // combo of ? and * operators
    assert_pattern_hits(&mut random, &searcher, ".N.*", 100)?;
    assert_pattern_hits(&mut random, &searcher, "N..*", 100)?;

    assert_pattern_hits(&mut random, &searcher, ".*N.", 100)?;
    assert_pattern_hits(&mut random, &searcher, ".*..", 1000)?;
    assert_pattern_hits(&mut random, &searcher, ".*.N", 100)?;
  }

  Ok(())
}