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
/*
 * 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::leaf_reader::LeafReader;
use crate::core::index::leaf_reader_context::LeafReaderContext;
use crate::core::search::field_comparator::{
  FieldComparator, FieldComparatorEnum, FieldComparatorValue,
};
use crate::core::search::field_doc::{FieldDoc, FieldsValue};
use crate::core::search::leaf_field_comparator::LeafFieldComparatorEnum;
use crate::core::search::pruning::Pruning;
use crate::core::search::score_doc::{ScoreDoc, ScoreDocLike};
use crate::core::search::sort_field::SortFiledBase;
use crate::core::search::sort_field_enum::SortFieldEnum;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::priority_queue::{Compare, CompareEnum2, PriorityQueue};
use crate::impl_from_for_enum;
use std::fmt;
use std::fmt::{Display, Formatter};

/// A hit queue for sorting by hits by terms in more than one field
pub struct FieldValueHitQueue;
pub fn new(
  fields: &[SortFieldEnum],
  size: usize,
) -> Result<PriorityQueue<Entry, FieldValueHitQueueComparator>> {
  let num_comparators = fields.len();
  let mut comparators = Vec::with_capacity(num_comparators);
  let mut reverse_mul = Vec::with_capacity(num_comparators);

  for (i, field) in fields.iter().enumerate() {
    let reverse = if field.get_reverse() { -1 } else { 1 };
    reverse_mul.push(reverse);

    let pruning = if i == 0 {
      if num_comparators > 1 {
        Pruning::GreaterThan
      } else {
        Pruning::GreaterThanOrEqualTo
      }
    } else {
      Pruning::None
    };

    let comparator = field.get_comparator(size, pruning)?;
    comparators.push(comparator);
  }
  let comparator = if num_comparators == 1 {
    CompareEnum2::A(OneComparatorComparator::new(comparators, reverse_mul))
  } else {
    CompareEnum2::B(MultiComparatorsComparator::new(comparators, reverse_mul))
  };
  PriorityQueue::new(size, comparator)
}
/// Creates a hit queue sorted by the given list of fields.
///
/// **NOTE:**
/// The instances returned by this method pre-allocate a full array of length `num_hits`.
///
/// # Arguments
///
/// * `fields` – SortField array we are sorting by in priority order (highest priority first);
///   cannot be empty.
/// * `size` – The number of hits to retain.
///   Must be greater than zero.
pub fn create(
  fields: &[SortFieldEnum],
  size: usize,
) -> Result<PriorityQueue<Entry, FieldValueHitQueueComparator>> {
  if fields.is_empty() {
    return Err(LuceneError::illegal_argument(
      "Sort must contain at least one field",
    ));
  }
  new(fields, size)
}

#[derive(Clone, Default, Debug)]
pub struct Entry {
  pub base: ScoreDoc,
  pub slot: usize,
}
impl Entry {
  pub fn new(slot: usize, doc: i32) -> Self {
    let base = ScoreDoc::new(doc, f32::NAN);
    Self { base, slot }
  }
}
impl ScoreDocLike for Entry {
  fn doc(&self) -> i32 {
    self.base.doc
  }

  fn score(&self) -> f32 {
    self.base.score
  }

  fn shard_index(&self) -> i32 {
    self.base.shard_index
  }

  fn set_shard_index(&mut self, shard_index: i32) {
    self.base.shard_index = shard_index
  }

  fn set_score(&mut self, score: f32) {
    self.base.score = score
  }
}
impl fmt::Display for Entry {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "slot:{} {}", self.slot, self.base)
  }
}

/// An implementation of FieldValueHitQueue which is optimized in case there is just one comparator.
pub struct OneComparatorComparator {
  one_comparator: Box<[FieldComparatorEnum]>,
  one_reverse_mul: Box<[i32]>,
}
impl OneComparatorComparator {
  pub fn new(one_comparator: Vec<FieldComparatorEnum>, one_reverse_mul: Vec<i32>) -> Self {
    debug_assert_eq!(one_comparator.len(), one_reverse_mul.len());
    debug_assert!(one_comparator.len() == 1);
    Self {
      one_comparator: one_comparator.into_boxed_slice(),
      one_reverse_mul: one_reverse_mul.into_boxed_slice(),
    }
  }
}
impl Compare<Entry> for OneComparatorComparator {
  fn less_than(&self, hit_a: &Entry, hit_b: &Entry) -> Result<bool> {
    debug_assert!(self.one_comparator.len() == self.one_reverse_mul.len());
    debug_assert_ne!(hit_a.slot, hit_b.slot);

    let cmp_result = self.one_comparator[0].compare(hit_a.slot, hit_b.slot);
    let c = self.one_reverse_mul[0] * cmp_result;

    if c != 0 {
      Ok(c > 0)
    } else {
      Ok(hit_a.base.doc > hit_b.base.doc)
    }
  }
}
/// An implementation of FieldValueHitQueue which is optimized in case there is more than one comparator.
pub struct MultiComparatorsComparator {
  comparators: Box<[FieldComparatorEnum]>,
  reverse_mul: Box<[i32]>,
}

impl MultiComparatorsComparator {
  pub fn new(comparators: Vec<FieldComparatorEnum>, reverse_mul: Vec<i32>) -> Self {
    debug_assert_eq!(
      comparators.len(),
      reverse_mul.len(),
      "comparators and reverse_mul length must match"
    );
    Self {
      comparators: comparators.into_boxed_slice(),
      reverse_mul: reverse_mul.into_boxed_slice(),
    }
  }
}

impl Compare<Entry> for MultiComparatorsComparator {
  fn less_than(&self, hit_a: &Entry, hit_b: &Entry) -> Result<bool> {
    debug_assert_eq!(
      self.comparators.len(),
      self.reverse_mul.len(),
      "comparators/reverse_mul length mismatch"
    );
    debug_assert_ne!(hit_a.slot, hit_b.slot);

    let num_comparators = self.comparators.len();

    for i in 0..num_comparators {
      let cmp_result = self.comparators[i].compare(hit_a.slot, hit_b.slot);
      let c = self.reverse_mul[i] * cmp_result;

      if c != 0 {
        return Ok(c > 0);
      }
    }
    Ok(hit_a.base.doc > hit_b.base.doc)
  }
}
pub type FieldValueHitQueueComparator =
  CompareEnum2<OneComparatorComparator, MultiComparatorsComparator>;

impl PriorityQueue<Entry, FieldValueHitQueueComparator> {
  pub fn get_leaf_comparator<LR>(
    &mut self,
    context: &LeafReaderContext<LR>,
  ) -> Result<Vec<LeafFieldComparatorEnum<LR>>>
  where
    LR: LeafReader,
  {
    match &mut self.compare {
      CompareEnum2::A(one_comp) => {
        debug_assert!(one_comp.one_comparator.len() == 1);
        let comp = &mut one_comp.one_comparator[0];
        Ok(vec![comp.get_leaf_comparator(context)?])
      },
      CompareEnum2::B(multi_comp) => {
        let mut v = Vec::with_capacity(multi_comp.comparators.len());
        for x in &mut multi_comp.comparators {
          v.push(x.get_leaf_comparator(context)?);
        }
        Ok(v)
      },
    }
  }
  pub fn get_reverse_mul(&self) -> &[i32] {
    match &self.compare {
      CompareEnum2::A(one_comp) => &one_comp.one_reverse_mul,
      CompareEnum2::B(multi_comp) => &multi_comp.reverse_mul,
    }
  }
  pub fn get_comparators(&self) -> &[FieldComparatorEnum] {
    match &self.compare {
      CompareEnum2::A(one_comp) => &one_comp.one_comparator,
      CompareEnum2::B(multi_comp) => &multi_comp.comparators,
    }
  }
  pub fn get_comparators_mut(&mut self) -> &mut [FieldComparatorEnum] {
    match &mut self.compare {
      CompareEnum2::A(one_comp) => &mut one_comp.one_comparator,
      CompareEnum2::B(multi_comp) => &mut multi_comp.comparators,
    }
  }
  pub(crate) fn get_comparators_and_reverse_mul(&mut self) -> (&mut [FieldComparatorEnum], &[i32]) {
    match &mut self.compare {
      CompareEnum2::A(one_comp) => (&mut one_comp.one_comparator, &one_comp.one_reverse_mul),
      CompareEnum2::B(multi_comp) => (&mut multi_comp.comparators, &multi_comp.reverse_mul),
    }
  }
  /// Given a queue [`Entry`], creates a corresponding [`FieldDoc`] that contains the values used to sort the
  /// given document. These values are not the raw values out of the index, but the internal
  /// representation of them. This allows the search hit to be collated with hits from other
  /// searchers.
  ///
  /// # Arguments
  ///
  /// * `entry` – The [`Entry`] used to create a [`FieldDoc`].
  ///
  /// # Returns
  ///
  /// The newly created [`FieldDoc`].
  pub(crate) fn fill_fields(&self, entry: Entry) -> FieldDoc {
    let comparators = self.get_comparators();
    let n = comparators.len();
    let mut fields = Vec::with_capacity(n);
    for comp in comparators {
      let value = comp
        .value(entry.slot)
        .unwrap_or_else(FieldComparatorValue::missing);
      fields.push(value);
    }
    FieldDoc::with_fields(entry.base.doc, entry.base.score, fields)
  }
}

#[derive(Clone, Debug)]
pub enum TopFieldScoreDoc {
  Entry(Entry),
  Field(FieldDoc),
  Score(ScoreDoc),
}
impl TopFieldScoreDoc {
  pub fn as_entry(&self) -> Option<&Entry> {
    match self {
      TopFieldScoreDoc::Entry(v) => Some(v),
      _ => None,
    }
  }

  pub fn as_field(&self) -> Option<&FieldDoc> {
    match self {
      TopFieldScoreDoc::Field(v) => Some(v),
      _ => None,
    }
  }

  pub fn as_score(&self) -> Option<&ScoreDoc> {
    match self {
      TopFieldScoreDoc::Score(v) => Some(v),
      _ => None,
    }
  }
  pub fn into_entry(self) -> Option<Entry> {
    match self {
      TopFieldScoreDoc::Entry(v) => Some(v),
      _ => None,
    }
  }

  pub fn into_field(self) -> Option<FieldDoc> {
    match self {
      TopFieldScoreDoc::Field(v) => Some(v),
      _ => None,
    }
  }

  pub fn into_score(self) -> Option<ScoreDoc> {
    match self {
      TopFieldScoreDoc::Score(v) => Some(v),
      _ => None,
    }
  }
  pub fn fields(&self) -> Result<&[FieldsValue]> {
    match self {
      TopFieldScoreDoc::Field(fd) => Ok(fd.fields.as_slice()),
      _ => Err(LuceneError::illegal_state("not a FieldDoc variant")),
    }
  }
  pub fn slot(&self) -> Result<usize> {
    match self {
      TopFieldScoreDoc::Entry(e) => Ok(e.slot),
      _ => Err(LuceneError::illegal_state(
        "other variants do not have slot",
      )),
    }
  }
  pub fn doc(&self) -> i32 {
    match self {
      TopFieldScoreDoc::Entry(e) => e.doc(),
      TopFieldScoreDoc::Field(fd) => fd.doc(),
      TopFieldScoreDoc::Score(sd) => sd.doc(),
    }
  }
  pub fn score_doc_mut(&mut self) -> &mut ScoreDoc {
    match self {
      TopFieldScoreDoc::Entry(e) => &mut e.base,
      TopFieldScoreDoc::Field(fd) => &mut fd.base,
      TopFieldScoreDoc::Score(sd) => sd,
    }
  }
  pub fn score_doc(&self) -> &ScoreDoc {
    match self {
      TopFieldScoreDoc::Entry(e) => &e.base,
      TopFieldScoreDoc::Field(fd) => &fd.base,
      TopFieldScoreDoc::Score(sd) => sd,
    }
  }
}

impl Display for TopFieldScoreDoc {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    match self {
      TopFieldScoreDoc::Entry(e) => write!(f, "Entry: {}", e),
      TopFieldScoreDoc::Field(fd) => write!(f, "FieldDoc: {}", fd),
      TopFieldScoreDoc::Score(sd) => write!(f, "ScoreDoc: {}", sd),
    }
  }
}

impl Default for TopFieldScoreDoc {
  fn default() -> Self {
    TopFieldScoreDoc::Entry(Entry::default())
  }
}

impl ScoreDocLike for TopFieldScoreDoc {
  fn doc(&self) -> i32 {
    match self {
      TopFieldScoreDoc::Entry(e) => e.doc(),
      TopFieldScoreDoc::Field(fd) => fd.doc(),
      TopFieldScoreDoc::Score(sd) => sd.doc(),
    }
  }

  fn score(&self) -> f32 {
    match self {
      TopFieldScoreDoc::Entry(e) => e.score(),
      TopFieldScoreDoc::Field(fd) => fd.score(),
      TopFieldScoreDoc::Score(sd) => sd.score(),
    }
  }

  fn shard_index(&self) -> i32 {
    match self {
      TopFieldScoreDoc::Entry(e) => e.shard_index(),
      TopFieldScoreDoc::Field(fd) => fd.shard_index(),
      TopFieldScoreDoc::Score(sd) => sd.shard_index(),
    }
  }

  fn set_shard_index(&mut self, shard_index: i32) {
    match self {
      TopFieldScoreDoc::Entry(e) => e.set_shard_index(shard_index),
      TopFieldScoreDoc::Field(fd) => fd.set_shard_index(shard_index),
      TopFieldScoreDoc::Score(sd) => sd.set_shard_index(shard_index),
    }
  }

  fn set_score(&mut self, score: f32) {
    match self {
      TopFieldScoreDoc::Entry(e) => e.set_score(score),
      TopFieldScoreDoc::Field(fd) => fd.set_score(score),
      TopFieldScoreDoc::Score(sd) => sd.set_score(score),
    }
  }
}
impl_from_for_enum!(
    TopFieldScoreDoc,
    Entry => Entry,
    FieldDoc => Field,
    ScoreDoc => Score
);