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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * 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::index::BytesRef;
use crate::core::index::directory_reader;
use crate::core::index::index_options::IndexOptions;
use crate::core::index::index_reader::IndexReader;
use crate::core::index::index_reader_context::{IRCLeafReader, IndexReaderContext};
use crate::core::index::index_writer::IndexWriter;
use crate::core::index::leaf_reader::LeafReader;
use crate::core::index::leaf_reader_context::LeafReaderContext;
use crate::core::index::live_index_writer_config::LiveIndexWriterConfig;
use crate::core::index::merge_policy::MergePolicy;
use crate::core::index::term::Term;
use crate::core::index::two_phase_commit::TwoPhaseCommit;
use crate::core::search::boolean_clause::Occur;
use crate::core::search::boolean_query::Builder;
use crate::core::search::collector::Collector;
use crate::core::search::collector_manager::CollectorManager;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::leaf_collector::LeafCollector;
use crate::core::search::phrase_query::PhraseQuery;
use crate::core::search::scorable::Scorable;
use crate::core::search::score_mode::ScoreMode;
use crate::core::search::simple_collector::SimpleCollector;
use crate::core::search::term_query::TermQuery;
use crate::core::search::weight::Weight;
use crate::core::store::directory::{DirEnum, Directory};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::test_framework::core::analysis::mock_analyzer::MockAnalyzer;
use crate::test_framework::core::index::random_index_writer::RandomIndexWriter;
pub use crate::test_framework::core::search::similarity::{
  SimpleSimilarity1, new_simple_similarity1,
};
use crate::test_framework::core::util::lucene_test_case::{
  get_only_leaf_reader, new_directory_shared, new_field, new_index_writer_config_with_analyzer,
  new_log_merge_policy_with_merge_factor, new_searcher_with_reader, random,
};
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::LazyLock;

#[allow(dead_code)] // for quick search
pub struct TestOmitTF;
static OMIT_TYPE: LazyLock<FieldType> = LazyLock::new(|| {
  let mut field_type = FieldType::from_ref(&*crate::core::document::text_field::TYPE_NOT_STORED)
    .expect("failed to create OMIT_TYPE");
  field_type
    .set_index_options(IndexOptions::Docs)
    .expect("failed to set index options for OMIT_TYPE");
  field_type
});

static NORMAL_TYPE: LazyLock<FieldType> = LazyLock::new(|| {
  FieldType::from_ref(&*crate::core::document::text_field::TYPE_NOT_STORED)
    .expect("failed to create NORMAL_TYPE")
});
// Make sure first adding docs that do not omitTermFreqAndPositions for
// field X, then adding docs that do omitTermFreqAndPositions for that same
// field,
#[test]
fn test_mixed_ram() -> Result<()> {
  let mut random = random();
  let ram = new_directory_shared(&mut random)?;
  let analyzer = MockAnalyzer::new(&mut random);
  let mut iwc = new_index_writer_config_with_analyzer(&mut random, analyzer)?;
  iwc.set_max_buffered_docs(10);
  let mut mp = new_log_merge_policy_with_merge_factor(&mut random, 2)?;
  mp.get_base_mut().set_no_cfs_ratio(0.0)?;
  iwc.set_merge_policy(mp);
  let writer = IndexWriter::new(ram.clone(), iwc)?;

  let mut d = Document::new();
  let mut field_to_type = HashMap::new();

  // this field will have Tf
  let f1 = new_field(
    &mut random,
    "f1",
    "This field has term freqs",
    &NORMAL_TYPE,
    &mut field_to_type,
  )?;
  d.add(f1);

  // this field will NOT have Tf
  let f2 = new_field(
    &mut random,
    "f2",
    "This field has NO Tf in all docs",
    &OMIT_TYPE,
    &mut field_to_type,
  )?;
  d.add(f2);

  for _ in 0..5 {
    writer.add_document(&mut d)?;
  }

  for _ in 0..20 {
    writer.add_document(&mut d)?;
  }

  // force merge
  writer.force_merge(1)?;

  // flush
  writer.close()?;

  let reader = directory_reader::open(ram.clone())?;
  let leaf = get_only_leaf_reader(&reader)?;
  let fi = leaf.get_field_infos()?;

  assert_eq!(
    IndexOptions::DocsAndFreqsAndPositions,
    *fi
      .field_info_by_name("f1")?
      .ok_or_else(|| LuceneError::illegal_state("field info for f1 is None"))?
      .get_index_options(),
    "OmitTermFreqAndPositions field bit should not be set."
  );
  assert_eq!(
    IndexOptions::Docs,
    *fi
      .field_info_by_name("f2")?
      .ok_or_else(|| LuceneError::illegal_state("field info for f2 is None"))?
      .get_index_options(),
    "OmitTermFreqAndPositions field bit should be set."
  );

  Ok(())
}
fn assert_no_prx(dir: &DirEnum) -> Result<()> {
  let files = dir.list_all()?;
  for file in files {
    assert!(!file.ends_with(".prx"));
    assert!(!file.ends_with(".pos"));
  }
  Ok(())
}

// Verifies no *.prx exists when all fields omit term freq:
#[test]
fn test_no_prx_file() -> Result<()> {
  let mut random = random();
  let ram = new_directory_shared(&mut random)?;
  let analyzer = MockAnalyzer::new(&mut random);
  let mut iwc = new_index_writer_config_with_analyzer(&mut random, analyzer)?;
  iwc.set_max_buffered_docs(3);
  iwc.set_max_buffered_docs(10);
  let mut mp = new_log_merge_policy_with_merge_factor(&mut random, 2)?;
  mp.get_base_mut().set_no_cfs_ratio(0.0)?;
  iwc.set_merge_policy(mp);
  let writer = IndexWriter::new(ram.clone(), iwc)?;

  let mut d = Document::new();
  let mut field_to_type = HashMap::new();

  let f1 = new_field(
    &mut random,
    "f1",
    "This field has term freqs",
    &OMIT_TYPE,
    &mut field_to_type,
  )?;
  d.add(f1);

  for _ in 0..30 {
    writer.add_document(&mut d)?;
  }

  writer.commit()?;

  assert_no_prx(ram.as_ref())?;

  writer.close()?;
  Ok(())
}
#[test]
fn test_basic() -> Result<()> {
  let mut random = random();
  let dir = new_directory_shared(&mut random)?;
  let analyzer = MockAnalyzer::new(&mut random);
  let indexing_similarity_iwc = new_simple_similarity1();
  let mut iwc =
    crate::test_framework::core::util::lucene_test_case::new_index_writer_config_with_analyzer(
      &mut random,
      analyzer,
    )?;
  iwc.set_max_buffered_docs(2);
  iwc.set_max_buffered_docs(10);
  let mut mp = new_log_merge_policy_with_merge_factor(&mut random, 2)?;
  mp.get_base_mut().set_no_cfs_ratio(0.0)?;
  iwc.set_merge_policy(mp);
  let writer = IndexWriter::new(dir.clone(), iwc)?;

  let mut sb = String::with_capacity(265);
  let term = "term";
  let mut field_to_type = HashMap::new();

  for i in 0..30 {
    let mut d = Document::new();
    sb.push_str(term);
    sb.push(' ');
    let content = &sb;

    let no_tf = new_field(
      &mut random,
      "noTf",
      format!("{}{}", content, if i % 2 == 0 { "" } else { " notf" }),
      &OMIT_TYPE,
      &mut field_to_type,
    )?;
    d.add(no_tf);

    let tf = new_field(
      &mut random,
      "tf",
      format!("{}{}", content, if i % 2 == 0 { " tf" } else { "" }),
      &NORMAL_TYPE,
      &mut field_to_type,
    )?;
    d.add(tf);

    writer.add_document_with_similarity(d, &indexing_similarity_iwc)?;
  }

  writer.force_merge(1)?;
  writer.close()?;

  let reader = directory_reader::open(dir.clone())?;
  let searcher =
    crate::test_framework::core::util::lucene_test_case::new_searcher_with_reader_and_similarity(
      reader,
      std::sync::Arc::new(new_simple_similarity1()),
    )?;

  let a = Term::from_text("noTf", term);
  let b = Term::from_text("tf", term);
  let c = Term::from_text("noTf", "notf");
  let d = Term::from_text("tf", "tf");

  let q1 = TermQuery::new(a.clone());
  let q2 = TermQuery::new(b);
  let q3 = TermQuery::new(c.clone());
  let q4 = TermQuery::new(d);

  let pq = PhraseQuery::from_bytes_no_slop(a.field(), vec![a.bytes().clone(), c.bytes().clone()])?;
  let error = match searcher.search(pq, 10) {
    Ok(_) => panic!("phrase queries require term positions"),
    Err(error) => error,
  };
  assert!(error.is_illegal_state_error(), "{error}");

  let collector_manager = CollectorManagerImpl;
  searcher.search_with_collector_manager(q1.clone(), &collector_manager)?;

  let collector_manager = CollectorManagerImpl1;
  searcher.search_with_collector_manager(q2, &collector_manager)?;

  let collector_manager = CollectorManagerImpl2;
  searcher.search_with_collector_manager(q3, &collector_manager)?;

  let collector_manager = CollectorManagerImpl3;
  searcher.search_with_collector_manager(q4.clone(), &collector_manager)?;

  let mut bq = Builder::new();
  bq.add(q1, Occur::Must)?;
  bq.add(q4, Occur::Must)?;

  let count = searcher.count(bq.build())?;
  assert_eq!(15, count);

  Ok(())
}
#[test]
fn test_stats() -> Result<()> {
  let mut random = random();
  let dir = new_directory_shared(&mut random)?;
  let analyzer = MockAnalyzer::new(&mut random);
  let iwc = new_index_writer_config_with_analyzer(&mut random, analyzer)?;
  let iw = RandomIndexWriter::with_config(&mut random, dir.clone(), iwc);

  let mut doc = Document::new();
  let mut ft = FieldType::from_ref(&*crate::core::document::text_field::TYPE_NOT_STORED)?;
  ft.set_index_options(IndexOptions::Docs)?;
  ft.freeze();

  let mut field_to_type = HashMap::new();
  let f = new_field(&mut random, "foo", "bar", &ft, &mut field_to_type)?;
  doc.add(f);

  iw.add_document(&mut random, doc)?;
  let ir = iw.get_reader(&mut random)?;
  iw.close(&mut random)?;

  assert_eq!(
    ir.doc_freq(&Term::new("foo", BytesRef::from_string("bar")))? as i64,
    ir.total_term_freq(&Term::new("foo", BytesRef::from_string("bar")))?
  );
  assert_eq!(
    ir.get_sum_doc_freq("foo")?,
    ir.get_sum_total_term_freq("foo")?
  );

  Ok(())
}

struct CollectorManagerImpl;

impl CollectorManager for CollectorManagerImpl {
  type C = ScoreAssertingCollectorImpl;
  type T = ();

  fn new_collector(&self) -> Result<Self::C> {
    Ok(ScoreAssertingCollectorImpl::new())
  }

  fn reduce(&self, _collectors: Vec<Self::C>) -> Result<Self::T> {
    Ok(())
  }
}

struct ScoreAssertingCollectorImpl;

impl ScoreAssertingCollectorImpl {
  fn new() -> Self {
    Self
  }
}

impl Collector for ScoreAssertingCollectorImpl {
  type LeafCollector<'a, IRC>
    = &'a mut Self
  where
    Self: 'a,
    IRC: IndexReaderContext + 'a;

  fn get_leaf_collector<'a, W, IRC, SIM>(
    &'a mut self,
    context: &LeafReaderContext<IRCLeafReader<IRC>>,
    _weight: Option<&W>,
    _searcher: &IndexSearcher<IRC, SIM>,
  ) -> Result<Self::LeafCollector<'a, IRC>>
  where
    SIM: crate::core::search::similarities_impl::similarities::Similarity,
    IRC: IndexReaderContext,
    W: Weight<IRC, SIM> + ?Sized,
    crate::core::search::index_searcher::IndexSearcher<IRC, SIM>: Sync,
  {
    SimpleCollector::do_set_next_reader(self, context)?;
    Ok(self)
  }

  fn score_mode(&self) -> ScoreMode {
    ScoreMode::Complete
  }
}

impl LeafCollector for ScoreAssertingCollectorImpl {
  type CompetitiveIterator<'a>
    = crate::core::search::doc_id_set_iterator::EmptyDISI
  where
    Self: 'a;

  fn collect<SC: Scorable + ?Sized>(&mut self, _doc: i32, scorer: &mut SC) -> Result<()> {
    let score = scorer.score()?;
    assert!((score - 1.0f32).abs() <= 0.00001f32, "got score={}", score);
    Ok(())
  }
}

impl SimpleCollector for ScoreAssertingCollectorImpl {}

impl Display for ScoreAssertingCollectorImpl {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", std::any::type_name::<Self>())
  }
}

struct CollectorManagerImpl1;

impl CollectorManager for CollectorManagerImpl1 {
  type C = ScoreAssertingCollectorImpl1;
  type T = ();

  fn new_collector(&self) -> Result<Self::C> {
    Ok(ScoreAssertingCollectorImpl1::new())
  }

  fn reduce(&self, _collectors: Vec<Self::C>) -> Result<Self::T> {
    Ok(())
  }
}

struct ScoreAssertingCollectorImpl1;

impl ScoreAssertingCollectorImpl1 {
  fn new() -> Self {
    Self
  }
}

impl Collector for ScoreAssertingCollectorImpl1 {
  type LeafCollector<'a, IRC>
    = &'a mut Self
  where
    Self: 'a,
    IRC: IndexReaderContext + 'a;

  fn get_leaf_collector<'a, W, IRC, SIM>(
    &'a mut self,
    context: &LeafReaderContext<IRCLeafReader<IRC>>,
    _weight: Option<&W>,
    _searcher: &IndexSearcher<IRC, SIM>,
  ) -> Result<Self::LeafCollector<'a, IRC>>
  where
    SIM: crate::core::search::similarities_impl::similarities::Similarity,
    IRC: IndexReaderContext,
    W: Weight<IRC, SIM> + ?Sized,
    crate::core::search::index_searcher::IndexSearcher<IRC, SIM>: Sync,
  {
    SimpleCollector::do_set_next_reader(self, context)?;
    Ok(self)
  }

  fn score_mode(&self) -> ScoreMode {
    ScoreMode::Complete
  }
}

impl LeafCollector for ScoreAssertingCollectorImpl1 {
  type CompetitiveIterator<'a>
    = crate::core::search::doc_id_set_iterator::EmptyDISI
  where
    Self: 'a;

  fn collect<SC: Scorable + ?Sized>(&mut self, doc: i32, scorer: &mut SC) -> Result<()> {
    let score = scorer.score()?;
    let expected = 1.0f32 + doc as f32;
    assert!(
      (expected - score).abs() <= 0.00001f32,
      "expected score={}, actual score={}",
      expected,
      score
    );
    Ok(())
  }
}

impl SimpleCollector for ScoreAssertingCollectorImpl1 {}

impl Display for ScoreAssertingCollectorImpl1 {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", std::any::type_name::<Self>())
  }
}

struct CollectorManagerImpl2;

impl CollectorManager for CollectorManagerImpl2 {
  type C = ScoreAssertingCollectorImpl2;
  type T = ();

  fn new_collector(&self) -> Result<Self::C> {
    Ok(ScoreAssertingCollectorImpl2::new())
  }

  fn reduce(&self, _collectors: Vec<Self::C>) -> Result<Self::T> {
    Ok(())
  }
}

struct ScoreAssertingCollectorImpl2;

impl ScoreAssertingCollectorImpl2 {
  fn new() -> Self {
    Self
  }
}

impl Collector for ScoreAssertingCollectorImpl2 {
  type LeafCollector<'a, IRC>
    = &'a mut Self
  where
    Self: 'a,
    IRC: IndexReaderContext + 'a;

  fn get_leaf_collector<'a, W, IRC, SIM>(
    &'a mut self,
    context: &LeafReaderContext<IRCLeafReader<IRC>>,
    _weight: Option<&W>,
    _searcher: &IndexSearcher<IRC, SIM>,
  ) -> Result<Self::LeafCollector<'a, IRC>>
  where
    SIM: crate::core::search::similarities_impl::similarities::Similarity,
    IRC: IndexReaderContext,
    W: Weight<IRC, SIM> + ?Sized,
    crate::core::search::index_searcher::IndexSearcher<IRC, SIM>: Sync,
  {
    SimpleCollector::do_set_next_reader(self, context)?;
    Ok(self)
  }

  fn score_mode(&self) -> ScoreMode {
    ScoreMode::Complete
  }
}

impl LeafCollector for ScoreAssertingCollectorImpl2 {
  type CompetitiveIterator<'a>
    = crate::core::search::doc_id_set_iterator::EmptyDISI
  where
    Self: 'a;

  fn collect<SC: Scorable + ?Sized>(&mut self, doc: i32, scorer: &mut SC) -> Result<()> {
    let score = scorer.score()?;
    assert!((score - 1.0f32).abs() <= 0.00001f32);
    assert_ne!(doc % 2, 0);
    Ok(())
  }
}

impl SimpleCollector for ScoreAssertingCollectorImpl2 {}

impl Display for ScoreAssertingCollectorImpl2 {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", std::any::type_name::<Self>())
  }
}
struct CollectorManagerImpl3;

impl CollectorManager for CollectorManagerImpl3 {
  type C = ScoreAssertingCollectorImpl3;
  type T = ();

  fn new_collector(&self) -> Result<Self::C> {
    Ok(ScoreAssertingCollectorImpl3::new())
  }

  fn reduce(&self, _collectors: Vec<Self::C>) -> Result<Self::T> {
    Ok(())
  }
}

struct ScoreAssertingCollectorImpl3;

impl ScoreAssertingCollectorImpl3 {
  fn new() -> Self {
    Self
  }
}

impl Collector for ScoreAssertingCollectorImpl3 {
  type LeafCollector<'a, IRC>
    = &'a mut Self
  where
    Self: 'a,
    IRC: IndexReaderContext + 'a;

  fn get_leaf_collector<'a, W, IRC, SIM>(
    &'a mut self,
    context: &LeafReaderContext<IRCLeafReader<IRC>>,
    _weight: Option<&W>,
    _searcher: &IndexSearcher<IRC, SIM>,
  ) -> Result<Self::LeafCollector<'a, IRC>>
  where
    SIM: crate::core::search::similarities_impl::similarities::Similarity,
    IRC: IndexReaderContext,
    W: Weight<IRC, SIM> + ?Sized,
    crate::core::search::index_searcher::IndexSearcher<IRC, SIM>: Sync,
  {
    SimpleCollector::do_set_next_reader(self, context)?;
    Ok(self)
  }

  fn score_mode(&self) -> ScoreMode {
    ScoreMode::Complete
  }
}

impl LeafCollector for ScoreAssertingCollectorImpl3 {
  type CompetitiveIterator<'a>
    = crate::core::search::doc_id_set_iterator::EmptyDISI
  where
    Self: 'a;

  fn collect<SC: Scorable + ?Sized>(&mut self, doc: i32, scorer: &mut SC) -> Result<()> {
    let score = scorer.score()?;
    assert!((score - 1.0f32).abs() <= 0.00001f32);
    assert!(doc % 2 == 0);
    Ok(())
  }
}

impl SimpleCollector for ScoreAssertingCollectorImpl3 {}

impl Display for ScoreAssertingCollectorImpl3 {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", std::any::type_name::<Self>())
  }
}