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
/*
 * 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::search::bulk_scorer::BulkScorer;
use crate::core::search::disi_wrapper::DisiWrapper;
use crate::core::search::doc_id_stream::{DocIdStream, DocIdStreamConsumer};
use crate::core::search::scorable::Scorable;
use crate::core::search::score::Score;
use crate::core::search::scorer::Scorer;
use crate::core::search::scorer_util::ScorerUtil;
use crate::core::util::bits::Bits;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::priority_queue::{Compare, PriorityQueue};

pub(crate) const SHIFT: usize = 12;
pub(crate) const SIZE: usize = 1 << SHIFT;
pub(crate) const MASK: usize = SIZE - 1;

pub(crate) const SET_SIZE: usize = 1 << (SHIFT - 6);
#[allow(dead_code)]
pub(crate) const SET_MASK: usize = SET_SIZE - 1;

/// **BulkScorer** that is used for pure disjunctions and disjunctions that have low values of
/// `MinimumNumberShouldMatch` and dense clauses.
///
/// This scorer scores documents by batches of **4,096 docs**.
pub struct BooleanScorer<S> {
  // One bucket per doc ID in the window, present if scores are needed or if frequencies need to be
  // counted
  pub(crate) buckets: Option<Vec<Bucket>>,
  // This is basically an inlined FixedBitSet... seems to help with bound checks
  pub(crate) matching: [u64; SET_SIZE],
  pub(crate) head: PriorityQueue<DisiWrapper<S>, HeadPriorityQueueCmp>,
  pub(crate) tail: PriorityQueue<DisiWrapper<S>, TailPriorityQueueCmp>,
  leads: Vec<DisiWrapper<S>>,
  pub(crate) score: Score,
  pub(crate) min_should_match: usize,
  pub(crate) cost: i64,
  pub(crate) needs_scores: bool,
  word_cache_min_cost: i64,
}
impl<S> BooleanScorer<S>
where
  S: Scorer,
{
  pub(crate) fn new(
    scorers: Vec<S>,
    min_should_match: usize,
    needs_scores: bool,
    max_doc: i32,
  ) -> Result<Self> {
    if min_should_match < 1 || min_should_match > scorers.len() {
      return Err(LuceneError::illegal_argument(format!(
        "minShouldMatch should be within 1..num_scorers. Got {}",
        min_should_match
      )));
    }
    if scorers.len() <= 1 {
      return Err(LuceneError::illegal_argument(format!(
        "This scorer can only be used with two scorers or more, got {}",
        scorers.len()
      )));
    }

    let buckets = if needs_scores || min_should_match > 1 {
      Some((0..SIZE).map(|_| Bucket::new()).collect())
    } else {
      None
    };

    let matching = [0u64; SET_SIZE];

    let head_size = scorers.len() - min_should_match + 1;
    let tail_size = min_should_match - 1;

    let mut head = PriorityQueue::new(head_size, HeadPriorityQueueCmp)?;
    let mut tail = PriorityQueue::new(tail_size, TailPriorityQueueCmp)?;

    for s in scorers {
      let w = DisiWrapper::new(s)?;

      if let Some(evicted) = tail.insert_with_overflow(w)? {
        head.add(evicted)?;
      }
    }
    let cost = ScorerUtil::cost_with_min_should_match(
      head.iter_ref().chain(tail.iter_ref()).map(|w| w.cost),
      head_size + tail_size,
      min_should_match,
    )?;

    Ok(Self {
      buckets,
      matching,
      head,
      tail,
      leads: Vec::with_capacity(head_size + tail_size),
      score: Score::new(0.0),
      min_should_match,
      cost,
      needs_scores,
      // Word reuse needs enough documents to amortize its per-word loop control.
      word_cache_min_cost: (i64::from(max_doc) + 7) / 8,
    })
  }
  fn score_disi_wrapper_into_bitset<AB: Bits + ?Sized>(
    &mut self,
    w: &mut DisiWrapper<S>,
    accept_docs: Option<&AB>,
    min: i32,
    max: i32,
  ) -> Result<()> {
    let mut doc = w.doc;
    if doc < min {
      doc = w.scorer.advance(min)?;
    }
    if self.buckets.is_none() && accept_docs.is_none() && w.cost >= self.word_cache_min_cost {
      while doc < max {
        let index = (doc as usize & MASK) >> 6;
        let word_end = max.min((doc | 0x3f).saturating_add(1));
        let matching = &mut self.matching[index];
        let mut bits = *matching;
        while doc < word_end {
          bits |= 1u64 << (doc as usize & 0x3f);
          // Commit before advancing so errors and panics retain every prior hit.
          *matching = bits;
          doc = w.scorer.next_doc()?;
        }
      }
      w.doc = doc;
      return Ok(());
    }
    while doc < max {
      let doc_index = doc as usize;
      let accepted = match accept_docs {
        Some(accept_docs) => accept_docs.get(doc_index)?,
        None => true,
      };
      if accepted {
        let i = doc_index & MASK;
        let idx = i >> 6;

        self.matching[idx] |= 1u64 << (i & 0x3f);
        if let Some(ref mut buckets) = self.buckets {
          let bucket = &mut buckets[i];
          bucket.freq += 1;
          if self.needs_scores {
            bucket.score += w.scorer.score()? as f64;
          }
        }
      }

      doc = w.scorer.next_doc()?;
    }

    w.doc = doc;
    Ok(())
  }
  #[allow(clippy::too_many_arguments)]
  fn score_window_into_bitset_and_replay<
    CLC: crate::core::search::leaf_collector::LeafCollector,
    AB: Bits + ?Sized,
  >(
    &mut self,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    base: i32,
    min: i32,
    max: i32,
    scorers: &mut [DisiWrapper<S>],
    num_scorers: usize,
  ) -> Result<()> {
    for w in scorers.iter_mut().take(num_scorers) {
      debug_assert!(w.doc < max);
      self.score_disi_wrapper_into_bitset(w, accept_docs, min, max)?;
    }
    // take Ownership
    let mut score = std::mem::take(&mut self.score);
    let mut stream = DocIdStreamView::new(self, base);
    let result = collector.collect_stream(&mut stream, &mut score);
    // give back Ownership
    self.score = score;
    result?;
    for m in self.matching.iter_mut() {
      *m = 0;
    }
    Ok(())
  }
  fn advance(&mut self, min: i32) -> Result<&DisiWrapper<S>> {
    debug_assert!(self.tail.size() == (self.min_should_match - 1));

    match self.head.take_top() {
      None => return Err(LuceneError::illegal_state("head queue is empty")),
      Some(mut head_top) => {
        while head_top.doc < min {
          match self.tail.take_top() {
            None => {
              let v = head_top.scorer.advance(min)?;
              head_top.doc = v;
              let _ = self.head.update_top_with_new_top(head_top)?;
              head_top = self
                .head
                .take_top()
                .ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
            },
            Some(mut tail_top) => {
              if head_top.cost <= tail_top.cost {
                let v = head_top.scorer.advance(min)?;
                head_top.doc = v;
                let _ = self.head.update_top_with_new_top(head_top)?;
                head_top = self
                  .head
                  .take_top()
                  .ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
                // return tail_top back
                self.tail.update_top_with_new_top(tail_top)?;
              } else {
                let v = tail_top.scorer.advance(min)?;
                tail_top.doc = v;
                let _ = self.head.update_top_with_new_top(tail_top)?;
                let _ = self.tail.update_top_with_new_top(head_top)?;
                head_top = self
                  .head
                  .take_top()
                  .ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
              }
            },
          }
        }
        self.head.update_top_with_new_top(head_top)?;
      },
    }
    match self.head.top() {
      None => Err(LuceneError::illegal_state("head queue is empty")),
      Some(top) => Ok(top),
    }
  }
  #[allow(clippy::too_many_arguments)]
  pub(crate) fn score_window_multiple_scorers<
    CLC: crate::core::search::leaf_collector::LeafCollector,
    AB: Bits + ?Sized,
  >(
    &mut self,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    window_base: i32,
    window_min: i32,
    window_max: i32,
    mut max_freq: usize,
    leads: &mut Vec<DisiWrapper<S>>,
  ) -> Result<()> {
    while max_freq < self.min_should_match && max_freq + self.tail.size() >= self.min_should_match {
      // a match is still possible
      let mut candidate = self
        .tail
        .pop()?
        .ok_or_else(|| LuceneError::illegal_state("tail.pop returned None"))?;

      if candidate.doc < window_min {
        let new_doc = candidate.scorer.advance(window_min)?;
        candidate.doc = new_doc;
      }

      if candidate.doc < window_max {
        leads.push(candidate);
        max_freq += 1;
      } else {
        self.head.add(candidate)?;
      }
    }

    if max_freq >= self.min_should_match {
      // There might be matches in other scorers from the tail too
      for x in self.tail.get().into_iter() {
        leads.push(x);
        max_freq += 1;
      }
      self.tail.clear();

      self.score_window_into_bitset_and_replay(
        collector,
        accept_docs,
        window_base,
        window_min,
        window_max,
        leads.as_mut_slice(),
        max_freq,
      )?;
    }

    for v in leads.drain(..) {
      let evicted = self.head.insert_with_overflow(v)?;
      if let Some(e) = evicted {
        self.tail.add(e)?;
      }
    }

    Ok(())
  }
  pub(crate) fn score_window_single_scorer<
    CLC: crate::core::search::leaf_collector::LeafCollector,
    AB: Bits + ?Sized,
  >(
    &mut self,
    w: &mut DisiWrapper<S>,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    window_min: i32,
    window_max: i32,
    max_doc: i32,
  ) -> Result<()> {
    debug_assert!(self.tail.size() == 0);
    let next_window_base = match self.head.top() {
      None => return Err(LuceneError::illegal_state("head queue is empty")),
      Some(top) => top.doc & !(MASK as i32),
    };
    let end = std::cmp::max(window_max, std::cmp::min(max_doc, next_window_base));
    let doc = {
      let mut doc;
      {
        doc = w.doc;
        let it = &mut w.scorer;
        if doc < window_min {
          doc = it.advance(window_min)?;
        }
      }
      collector.set_scorer(&mut w.scorer)?;
      while doc < end {
        let accepted = match accept_docs {
          None => true,
          Some(bits) => bits.get(doc as usize)?,
        };
        if accepted {
          collector.collect(doc, &mut w.scorer)?;
        }
        doc = w.scorer.next_doc()?;
      }
      doc
    };
    w.doc = doc;
    // reset the scorer that should be used for the general case
    collector.set_scorer(&mut self.score)?;
    Ok(())
  }
  pub(crate) fn score_window<
    CLC: crate::core::search::leaf_collector::LeafCollector,
    AB: Bits + ?Sized,
  >(
    &mut self,
    top_doc: i32,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    min: i32,
    max: i32,
  ) -> Result<&DisiWrapper<S>> {
    let window_base = top_doc & !(MASK as i32);
    let window_min = std::cmp::max(min, window_base);
    let window_max = std::cmp::min(max, window_base + SIZE as i32);
    let mut leads = std::mem::take(&mut self.leads);
    let head_top = self
      .head
      .pop()?
      .ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
    leads.push(head_top);
    let mut max_freq = 1usize;

    while self.head.size() > 0 {
      let head_top_doc = self
        .head
        .top()
        .ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?
        .doc;
      if head_top_doc >= window_max {
        break;
      }
      let w = self
        .head
        .pop()?
        .ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
      leads.push(w);
      max_freq += 1;
    }

    if self.min_should_match == 1 && max_freq == 1 {
      // special case: only one scorer can match in the current window,
      // we can collect directly
      let mut bulk_scorer = leads.remove(0);

      self.score_window_single_scorer(
        &mut bulk_scorer,
        collector,
        accept_docs,
        window_min,
        window_max,
        max,
      )?;
      let top = self.head.add(bulk_scorer)?;
      self.leads = leads;
      return Ok(top);
    }
    // general case, collect through a bit set first and then replay
    self.score_window_multiple_scorers(
      collector,
      accept_docs,
      window_base,
      window_min,
      window_max,
      max_freq,
      &mut leads,
    )?;
    let top = self
      .head
      .top()
      .ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
    self.leads = leads;
    Ok(top)
  }
}

impl<S> BulkScorer for BooleanScorer<S>
where
  S: Scorer,
{
  fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
    &mut self,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    min: i32,
    max: i32,
  ) -> Result<i32> {
    collector.set_scorer(&mut self.score)?;
    let mut top = self.advance(min)?;
    let mut doc = top.doc;
    while doc < max {
      top = self.score_window(doc, collector, accept_docs, min, max)?;
      doc = top.doc;
    }
    Ok(top.doc)
  }

  fn cost(&mut self) -> Result<i64> {
    Ok(self.cost)
  }
}

pub struct HeadPriorityQueueCmp;
impl<S> Compare<DisiWrapper<S>> for HeadPriorityQueueCmp
where
  S: Scorer,
{
  fn less_than(&self, a: &DisiWrapper<S>, b: &DisiWrapper<S>) -> Result<bool> {
    Ok(a.doc < b.doc)
  }
}
pub struct TailPriorityQueueCmp;
impl<S> Compare<DisiWrapper<S>> for TailPriorityQueueCmp
where
  S: Scorer,
{
  fn less_than(&self, a: &DisiWrapper<S>, b: &DisiWrapper<S>) -> Result<bool> {
    Ok(a.cost < b.cost)
  }
}
pub struct Bucket {
  score: f64,
  freq: usize,
}
impl Bucket {
  fn new() -> Self {
    Self {
      score: 0.0,
      freq: 0,
    }
  }
}
impl<S> PriorityQueue<DisiWrapper<S>, TailPriorityQueueCmp>
where
  S: Scorer,
{
  fn get(&mut self) -> Vec<DisiWrapper<S>> {
    self.take_heap_array()
  }
}

struct DocIdStreamView<'a, S> {
  scorer: &'a mut BooleanScorer<S>,
  base: i32,
}
impl<'a, S> DocIdStreamView<'a, S>
where
  S: Scorer,
{
  fn new(scorer: &'a mut BooleanScorer<S>, base: i32) -> Self {
    Self { scorer, base }
  }
}
impl<'a, S> DocIdStream for DocIdStreamView<'a, S>
where
  S: Scorer,
{
  fn for_each<DC: DocIdStreamConsumer + ?Sized>(&mut self, consumer: &mut DC) -> Result<()> {
    for (idx, bits_ref) in self.scorer.matching.iter().enumerate() {
      let mut bits = *bits_ref;

      while bits != 0 {
        let ntz = bits.trailing_zeros() as usize;
        let index_in_window = (idx << 6) | ntz;
        match self.scorer.buckets {
          Some(ref mut buckets) => {
            let bucket = &mut buckets[index_in_window];
            if bucket.freq >= self.scorer.min_should_match {
              consumer
                .accept_with_score(self.base | index_in_window as i32, bucket.score as f32)?;
            }
            bucket.freq = 0;
            bucket.score = 0.0;
          },
          None => {
            consumer.accept(self.base | index_in_window as i32)?;
          },
        }
        bits &= bits - 1;
      }
    }
    Ok(())
  }

  fn count<SC: Scorable + ?Sized>(&mut self, scorer: &mut SC) -> Result<i32> {
    if self.scorer.min_should_match > 1 {
      return self.default_count(scorer);
    }

    let count: i32 = self
      .scorer
      .matching
      .iter()
      .map(|l| l.count_ones() as i32)
      .sum();
    Ok(count)
  }
}