rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * 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::index::BytesRefValue;

use crate::core::index::BytesRef;
use crate::core::index::composite_reader::CompositeReader;
use crate::core::index::index_reader::{IndexReader, IndexReaderContextType};
use crate::core::index::multi_reader::MultiReader;
use crate::core::index::multi_terms;
use crate::core::index::term::Term;
use crate::core::index::terms::Terms;
use crate::core::index::terms_enum::TermsEnum;
use crate::core::search::field_value_hit_queue::TopFieldScoreDoc;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::prefix_query::PrefixQuery;
use crate::core::search::query::{IntoQuery, Query};
use crate::core::search::score_doc::{ScoreDoc, ScoreDocLike};
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::top_docs::TopDocs;
use crate::core::util::bytes_ref_iterator::BytesRefIterator;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::io_utils::IOUtils;
use crate::test_framework::core::search::shard_searching_test_base::{
  SearcherExpiredException, ShardIndexSearcher, ShardSearchingTestContext,
};
use crate::test_framework::core::util::DefaultCRReader;
use crate::test_framework::core::util::lucene_test_case::{at_least, is_night_mode, random};
use crate::test_framework::core::util::test_util::TestUtil;
use rand::prelude::SliceRandom;
use rand::{Rng, RngExt};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
use std::time::Instant;

//   - other queries besides PrefixQuery & TermQuery (but:
//     FuzzyQ will be problematic... the top N terms it
//     takes means results will differ)
//   - NRQ/F
//   - BQ, negated clauses, negated prefix clauses
//   - test pulling docs in 2nd round trip...
//   - filter too

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

struct PreviousSearchState {
  search_time: Instant,
  versions: Vec<i64>,
  search_after_local: Option<ScoreDoc>,
  search_after_shard: Option<ScoreDoc>,
  sort: Option<Arc<Sort>>,
  query: Query,
  num_hits_paged: usize,
}

impl PreviousSearchState {
  fn new(
    query: Query,
    sort: Option<Arc<Sort>>,
    search_after_local: Option<ScoreDoc>,
    search_after_shard: Option<ScoreDoc>,
    versions: &[i64],
    num_hits_paged: usize,
  ) -> Self {
    Self {
      versions: versions.to_vec(),
      search_after_local,
      search_after_shard,
      sort,
      query,
      num_hits_paged,
      search_time: Instant::now(),
    }
  }
}

type MockReader = Arc<MultiReader<Arc<DefaultCRReader>>>;
type MockSearcher = IndexSearcher<IndexReaderContextType<MockReader>>;

#[test]
fn test_simple() -> Result<()> {
  let mut random = random();
  let num_nodes = TestUtil::next_usize(&mut random, 1, 10);

  let run_time_sec = if is_night_mode() {
    at_least(&mut random, 5)
  } else {
    at_least(&mut random, 1)
  } as f64;

  let min_docs_to_make_terms = TestUtil::next_int(&mut random, 5, 20);

  let max_searcher_age_seconds = TestUtil::next_int(&mut random, 1, 3);

  let context = ShardSearchingTestContext::new();
  context.start(
    &mut random,
    num_nodes,
    run_time_sec,
    max_searcher_age_seconds,
  )?;

  let mut prior_searches = Vec::new();
  let mut terms: Option<Vec<BytesRef<Vec<u8>>>> = None;
  while Instant::now() < context.end_time() {
    let do_followon = !prior_searches.is_empty() && random.random_range(0..7) == 1;

    // Pick a random node; we will run the query on this node:
    let my_node_id = random.random_range(0..num_nodes);
    let nodes = context.nodes();

    let (local_shard_searcher, prev_search_index) = if do_followon {
      // Pretend user issued a followon query:
      let prev_search_index = random.random_range(0..prior_searches.len());
      let prev_search_state: &PreviousSearchState = &prior_searches[prev_search_index];
      let _search_age = prev_search_state.search_time.elapsed();
      match nodes[my_node_id].acquire_versions(&prev_search_state.versions) {
        Ok(searcher) => (searcher, Some(prev_search_index)),
        Err(error) if SearcherExpiredException::is_instance(&error) => {
          // Expected, sometimes; in a "real" app we would
          // either forward this error to the user ("too
          // much time has passed; please re-run your
          // search") or sneakily just switch to newest
          // searcher w/o telling them...
          prior_searches.remove(prev_search_index);
          continue;
        },
        Err(error) => return Err(error),
      }
    } else {
      // Do fresh query:
      (nodes[my_node_id].acquire()?, None)
    };

    let mut subs = Vec::with_capacity(num_nodes);
    let body_result = catch_unwind(AssertUnwindSafe(
      || -> Result<Option<PreviousSearchState>> {
        // Mock: now make a single reader (MultiReader) from all node
        // searchers.  In a real shard env you can't do this... we
        // do it to confirm results from the shard searcher
        // are correct:
        let mock_reader_result = (|| -> Result<(i32, MockReader, MockSearcher)> {
          let mut doc_count = 0;
          for (node_id, node) in nodes.iter().enumerate().take(num_nodes) {
            let sub_version = local_shard_searcher.node_versions[node_id];
            let Some(sub) = node.searchers.acquire(sub_version)? else {
              return Err(
                SearcherExpiredException::new(format!("nodeID=-1 version={sub_version}")).into(),
              );
            };
            let reader = sub.get_index_reader().clone();
            doc_count += reader.max_doc()?;
            subs.push(reader);
          }

          let mock_reader = Arc::new(MultiReader::new(subs.clone())?);
          let mock_searcher = IndexSearcher::new(mock_reader.clone().get_context()?)?;
          Ok((doc_count, mock_reader, mock_searcher))
        })();
        let (doc_count, mock_reader, mock_searcher) = match mock_reader_result {
          Ok(value) => value,
          Err(error) if SearcherExpiredException::is_instance(&error) => {
            // Expected
            return Ok(None);
          },
          Err(error) => return Err(error),
        };

        let (query, sort) = if let Some(prev_search_index) = prev_search_index {
          let prev_search_state: &PreviousSearchState = &prior_searches[prev_search_index];
          (
            prev_search_state.query.clone(),
            prev_search_state.sort.clone(),
          )
        } else {
          if terms.is_none()
            && doc_count > min_docs_to_make_terms
            && let Some(body_terms) = multi_terms::get_terms(mock_reader.clone(), "body")?
          {
            let mut terms_enum = body_terms.iterator()?;
            let mut new_terms = Vec::new();
            while let Some(term) = terms_enum.next()? {
              new_terms.push(term.into_owned());
            }
            if !new_terms.is_empty() {
              terms = Some(new_terms);
            }
          }

          let Some(terms) = terms.as_ref() else {
            return Ok(None);
          };
          let query = if random.random_bool(0.5) {
            TermQuery::new(Term::new(
              "body",
              terms[random.random_range(0..terms.len())].clone(),
            ))
            .into_query()
          } else {
            let term = terms[random.random_range(0..terms.len())].utf8_to_string()?;
            let prefix = if term.chars().count() <= 1 {
              term
            } else {
              term
                .chars()
                .take(TestUtil::next_usize(&mut random, 1, 2))
                .collect()
            };
            PrefixQuery::new(Term::new("body", prefix))?.into_query()
          };

          let sort = if random.random_bool(0.5) {
            None
          } else {
            match random.random_range(0..3) {
              0 => Some(Arc::new(Sort::new()?)),
              1 => None,
              2 => Some(Arc::new(Sort::with_fields(vec![SortField::with_reverse(
                Some("docid_intDV"),
                SortFieldType::Int,
                random.random_bool(0.5),
              )?])?)),
              _ => Some(Arc::new(Sort::with_fields(vec![SortField::with_reverse(
                Some("titleDV"),
                SortFieldType::String,
                random.random_bool(0.5),
              )?])?)),
            }
          };
          (query, sort)
        };

        let previous = prev_search_index.map(|index| &prior_searches[index]);
        match assert_same(
          &mut random,
          &mock_searcher,
          local_shard_searcher.as_ref(),
          query,
          sort,
          previous,
        ) {
          Ok(search_state) => Ok(search_state),
          Err(error) if SearcherExpiredException::is_instance(&error) => {
            // Expected; in a "real" app we would either
            // forward this error to the user or switch to the newest searcher.
            if let Some(prev_search_index) = prev_search_index {
              prior_searches.remove(prev_search_index);
            }
            Ok(None)
          },
          Err(error) => Err(error),
        }
      },
    ));
    let close_result = catch_unwind(AssertUnwindSafe(|| -> Result<()> {
      nodes[my_node_id].release(local_shard_searcher)?;
      for sub in &subs {
        sub.dec_ref()?;
      }
      Ok(())
    }));
    let search_state = IOUtils::finally_caught_result(body_result, close_result)?;

    if let Some(search_state) = search_state
      && search_state.search_after_local.is_some()
      && random.random_range(0..5) == 3
    {
      prior_searches.push(search_state);
      if prior_searches.len() > 200 {
        prior_searches.shuffle(&mut random);
        prior_searches.truncate(100);
      }
    }
  }

  context.finish()
}

fn assert_same<R>(
  random: &mut R,
  mock_searcher: &MockSearcher,
  shard_searcher: &ShardIndexSearcher,
  query: Query,
  sort: Option<Arc<Sort>>,
  state: Option<&PreviousSearchState>,
) -> Result<Option<PreviousSearchState>>
where
  R: Rng + ?Sized,
{
  let mut num_hits = TestUtil::next_usize(random, 1, 100);
  if let Some(state) = state
    && state.search_after_local.is_none()
  {
    // In addition to what we last searched:
    num_hits += state.num_hits_paged;
  }

  let (hits, mut shard_hits) = if let Some(sort) = sort.as_ref() {
    // Single (mock local) searcher:
    let hits = mock_searcher.search_with_sort(query.clone(), num_hits, sort.clone())?;

    // Shard searcher:
    let shard_hits = shard_searcher.search_with_sort(query.clone(), num_hits, sort.clone())?;
    (hits.base, shard_hits.base)
  } else {
    // Single (mock local) searcher:
    let hits = if let Some(after) = state.and_then(|state| state.search_after_local.clone()) {
      mock_searcher.search_after_score(Some(after), query.clone(), num_hits)?
    } else {
      mock_searcher.search(query.clone(), num_hits)?
    };

    // Shard searcher:
    let shard_hits = if let Some(after) = state.and_then(|state| state.search_after_shard.clone()) {
      shard_searcher.search_after(Some(after), query.clone(), num_hits)?
    } else {
      shard_searcher.search(query.clone(), num_hits)?
    };

    (
      TopDocs::new(
        hits.total_hits,
        hits.score_docs.into_iter().map(TopFieldScoreDoc::from),
      ),
      TopDocs::new(
        shard_hits.total_hits,
        shard_hits
          .score_docs
          .into_iter()
          .map(TopFieldScoreDoc::from),
      ),
    )
  };

  let num_nodes = shard_searcher.node_versions.len();
  let mut base = Vec::with_capacity(num_nodes);
  let mut doc_base = 0;
  for sub in mock_searcher
    .get_index_reader()
    .get_sequential_sub_readers()
  {
    base.push(doc_base);
    doc_base += sub.max_doc()?;
  }
  assert_eq!(num_nodes, base.len());

  let mut num_hits_paged = hits.score_docs.len();
  if let Some(state) = state
    && state.search_after_local.is_some()
  {
    num_hits_paged += state.num_hits_paged;
  }

  let (more_hits, bottom_hit, bottom_hit_shards) = if num_hits_paged < hits.total_hits.value() {
    // More hits to page through
    if sort.is_none() {
      let bottom_hit = hits.score_docs.last().unwrap().as_score().unwrap().clone();
      let score_doc = shard_hits.score_docs.last().unwrap().as_score().unwrap();
      // Must copy because below we rebase:
      let bottom_hit_shards =
        ScoreDoc::with_shard_index(score_doc.doc, score_doc.score, score_doc.shard_index);
      (true, Some(bottom_hit), Some(bottom_hit_shards))
    } else {
      (true, None, None)
    }
  } else {
    assert_eq!(hits.total_hits.value(), num_hits_paged);
    (false, None, None)
  };

  // Must rebase so the equality assertion passes:
  for score_doc in &mut shard_hits.score_docs {
    let shard_index = score_doc.shard_index() as usize;
    score_doc.score_doc_mut().doc += base[shard_index];
  }

  TestUtil::assert_consistent(&hits, &shard_hits);

  if more_hits {
    // Return a continuation:
    Ok(Some(PreviousSearchState::new(
      query,
      sort,
      bottom_hit,
      bottom_hit_shards,
      &shard_searcher.node_versions,
      num_hits_paged,
    )))
  } else {
    Ok(None)
  }
}