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
/*
 * 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::BytesRef;
use crate::core::index::index_reader::{IndexReader, IndexReaderContextType};
use crate::core::index::index_reader_context::{IRCLeafReader, IndexReaderContext};
use crate::core::index::leaf_reader::LeafReader;
use crate::core::index::multi_terms_enum::{MultiTermsEnum, MultiTermsEnumType};
use crate::core::index::postings_enum::ALL;
use crate::core::index::reader_slice::ReaderSlice;
use crate::core::index::terms::Terms;
use crate::core::index::terms_enum::{
  EmptyTermsEnum, TermsEnum, TermsEnumWithUnsupportedSecondAttributes2,
};
use crate::core::index::terms_enum_index::TermsEnumIndex;
use crate::core::util::ToInt;
use crate::core::util::automation::compiled_automaton::CompiledAutomaton;
use crate::core::util::error::lucene_error::Result;
use std::borrow::Cow;

/// Exposes flex API, merged from flex API of sub-segments.
pub struct MultiTerms<T> {
  subs: Box<[T]>,
  sub_slices: Box<[ReaderSlice]>,
  has_freqs: bool,
  has_offsets: bool,
  has_positions: bool,
  has_payloads: bool,
}
impl<T> MultiTerms<T>
where
  T: Terms,
{
  /// Creates a new instance. Use [`get_terms`] instead if possible.
  ///
  /// # Parameters
  /// * `subs` – The [`Terms`] instances of all sub-readers.
  /// * `sub_slices` – A parallel array (matching `subs`) describing the sub-reader slices.
  pub fn new(subs: Vec<T>, sub_slices: Vec<ReaderSlice>) -> Result<Self> {
    debug_assert!(
      !subs.is_empty(),
      "inefficient: don't use MultiTerms over one sub"
    );

    let mut has_freqs = true;
    let mut has_offsets = true;
    let mut has_positions = true;
    let mut has_payloads_any = false;

    for t in &subs {
      has_freqs &= t.has_freqs();
      has_offsets &= t.has_offsets();
      has_positions &= t.has_positions();
      has_payloads_any |= t.has_payloads();
    }

    // if all subs have pos, and at least one has payloads
    let has_payloads = has_positions && has_payloads_any;

    Ok(Self {
      subs: subs.into_boxed_slice(),
      sub_slices: sub_slices.into_boxed_slice(),
      has_freqs,
      has_offsets,
      has_positions,
      has_payloads,
    })
  }
}
pub type IntersectIterType<T> = MultiTermsEnumType<<T as Terms>::IntersectIter>;
pub type IteratorType<T> = MultiTermsEnumType<<T as Terms>::TermsEnum>;
impl<T> Terms for MultiTerms<T>
where
  T: Terms,
{
  type TermsEnum = IteratorType<T>;

  fn iterator(&self) -> Result<Self::TermsEnum> {
    let mut terms_enums = Vec::with_capacity(self.subs.len());

    let mut i = 0;
    while i < self.subs.len() {
      let sub = &self.subs[i];
      let terms_enum = sub.iterator()?;
      terms_enums.push(TermsEnumIndex::new(Some(terms_enum), i));
      i += 1;
    }

    if !terms_enums.is_empty() {
      let v = MultiTermsEnum::new(self.sub_slices.iter().copied())?;
      v.reset(terms_enums)
    } else {
      Ok(MultiTermsEnumType::B(EmptyTermsEnum))
    }
  }

  type IntersectIter = IntersectIterType<T>;

  fn intersect(
    &self,
    compiled: &CompiledAutomaton,
    start_term: Option<&BytesRef<Vec<u8>>>,
  ) -> Result<Self::IntersectIter> {
    let mut terms_enums = Vec::with_capacity(self.subs.len());

    let mut i = 0;
    while i < self.subs.len() {
      let sub = &self.subs[i];
      let terms_enum = sub.intersect(compiled, start_term)?;
      terms_enums.push(TermsEnumIndex::new(Some(terms_enum), i));
      i += 1;
    }
    if !terms_enums.is_empty() {
      let v = MultiTermsEnum::new(self.sub_slices.iter().copied())?;
      v.reset(terms_enums)
    } else {
      Ok(MultiTermsEnumType::B(EmptyTermsEnum))
    }
  }

  fn size(&self) -> Result<i64> {
    Ok(-1)
  }

  fn get_sum_total_term_freq(&self) -> Result<i64> {
    let mut sum = 0i64;
    for terms in &self.subs {
      let v = terms.get_sum_total_term_freq()?;
      debug_assert!(v != -1);
      sum += v;
    }
    Ok(sum)
  }

  fn get_sum_doc_freq(&self) -> Result<i64> {
    let mut sum = 0i64;
    for terms in &self.subs {
      let v = terms.get_sum_doc_freq()?;
      debug_assert!(v != -1);
      sum += v;
    }
    Ok(sum)
  }

  fn get_doc_count(&self) -> Result<i32> {
    let mut sum = 0;
    for terms in &self.subs {
      let v = terms.get_doc_count()?;
      debug_assert!(v != -1);
      sum += v;
    }
    Ok(sum)
  }

  fn has_freqs(&self) -> bool {
    self.has_freqs
  }

  fn has_offsets(&self) -> bool {
    self.has_offsets
  }

  fn has_positions(&self) -> bool {
    self.has_positions
  }

  fn has_payloads(&self) -> bool {
    self.has_payloads
  }

  fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
    let mut min_term = None;

    for terms in &self.subs {
      if let Some(term) = terms.get_min()? {
        match &min_term {
          None => min_term = Some(term),
          Some(cur) => {
            if term.as_ref().cmp(cur.as_ref()).to_int() < 0 {
              min_term = Some(term);
            }
          },
        }
      }
    }

    Ok(min_term)
  }

  fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
    let mut max_term = None;

    for terms in &self.subs {
      if let Some(term) = terms.get_max()? {
        match &max_term {
          None => max_term = Some(term),
          Some(cur) => {
            if term.as_ref().cmp(cur.as_ref()).to_int() > 0 {
              max_term = Some(term);
            }
          },
        }
      }
    }

    Ok(max_term)
  }
}

pub enum MultiTermsType<T> {
  A(T),
  B(MultiTerms<T>),
}

impl<T> Terms for MultiTermsType<T>
where
  T: Terms,
{
  type TermsEnum =
    TermsEnumWithUnsupportedSecondAttributes2<T::TermsEnum, <MultiTerms<T> as Terms>::TermsEnum>;
  type IntersectIter = TermsEnumWithUnsupportedSecondAttributes2<
    T::IntersectIter,
    <MultiTerms<T> as Terms>::IntersectIter,
  >;

  fn iterator(&self) -> Result<Self::TermsEnum> {
    match self {
      Self::A(terms) => terms
        .iterator()
        .map(TermsEnumWithUnsupportedSecondAttributes2::WithAttributes),
      Self::B(terms) => terms
        .iterator()
        .map(TermsEnumWithUnsupportedSecondAttributes2::WithoutAttributes),
    }
  }

  fn intersect(
    &self,
    compiled: &CompiledAutomaton,
    start_term: Option<&BytesRef<Vec<u8>>>,
  ) -> Result<Self::IntersectIter> {
    match self {
      Self::A(terms) => terms
        .intersect(compiled, start_term)
        .map(TermsEnumWithUnsupportedSecondAttributes2::WithAttributes),
      Self::B(terms) => terms
        .intersect(compiled, start_term)
        .map(TermsEnumWithUnsupportedSecondAttributes2::WithoutAttributes),
    }
  }

  fn size(&self) -> Result<i64> {
    match self {
      Self::A(terms) => terms.size(),
      Self::B(terms) => terms.size(),
    }
  }

  fn get_sum_total_term_freq(&self) -> Result<i64> {
    match self {
      Self::A(terms) => terms.get_sum_total_term_freq(),
      Self::B(terms) => terms.get_sum_total_term_freq(),
    }
  }

  fn get_sum_doc_freq(&self) -> Result<i64> {
    match self {
      Self::A(terms) => terms.get_sum_doc_freq(),
      Self::B(terms) => terms.get_sum_doc_freq(),
    }
  }

  fn get_doc_count(&self) -> Result<i32> {
    match self {
      Self::A(terms) => terms.get_doc_count(),
      Self::B(terms) => terms.get_doc_count(),
    }
  }

  fn has_freqs(&self) -> bool {
    match self {
      Self::A(terms) => terms.has_freqs(),
      Self::B(terms) => terms.has_freqs(),
    }
  }

  fn has_offsets(&self) -> bool {
    match self {
      Self::A(terms) => terms.has_offsets(),
      Self::B(terms) => terms.has_offsets(),
    }
  }

  fn has_positions(&self) -> bool {
    match self {
      Self::A(terms) => terms.has_positions(),
      Self::B(terms) => terms.has_positions(),
    }
  }

  fn has_payloads(&self) -> bool {
    match self {
      Self::A(terms) => terms.has_payloads(),
      Self::B(terms) => terms.has_payloads(),
    }
  }

  fn get_min(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
    match self {
      Self::A(terms) => terms.get_min(),
      Self::B(terms) => terms.get_min(),
    }
  }

  fn get_max(&self) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
    match self {
      Self::A(terms) => terms.get_max(),
      Self::B(terms) => terms.get_max(),
    }
  }

  fn get_stats(&self) -> Result<String> {
    match self {
      Self::A(terms) => terms.get_stats(),
      Self::B(terms) => terms.get_stats(),
    }
  }
}

pub type TermsType<IR> =
  MultiTermsType<<IRCLeafReader<IndexReaderContextType<IR>> as LeafReader>::Terms>;
pub type TermsPostingType<IR> = <<TermsType<IR> as Terms>::TermsEnum as TermsEnum>::PostingsEnum;
/// This method may return `None` if the field does not exist or if it has no terms.
pub fn get_terms<IR>(reader: IR, field: &str) -> Result<Option<TermsType<IR>>>
where
  IR: IndexReader,
{
  let max_doc = reader.max_doc()?;
  let reader = reader.get_context()?;
  let leaves = reader.leaves()?;

  if leaves.len() == 1 {
    return match leaves[0].reader().terms(field)? {
      Some(terms) => Ok(Some(MultiTermsType::A(terms))),
      None => return Ok(None),
    };
  }

  let mut terms_per_leaf = Vec::with_capacity(leaves.len());
  let mut slice_per_leaf = Vec::with_capacity(leaves.len());

  for (leaf_idx, ctx) in leaves.iter().enumerate() {
    if let Some(sub_terms) = ctx.reader().terms(field)? {
      terms_per_leaf.push(sub_terms);
      slice_per_leaf.push(ReaderSlice::new(ctx.doc_base, max_doc, leaf_idx));
    }
  }

  if terms_per_leaf.is_empty() {
    Ok(None)
  } else {
    Ok(Some(MultiTermsType::B(MultiTerms::new(
      terms_per_leaf,
      slice_per_leaf,
    )?)))
  }
}
/// Returns [`PostingsEnum`](crate::core::index::postings_enum::PostingsEnum) for the specified field and term.
///
/// This returns `None` if the field or term does not exist, or if positions were not indexed.
///
/// See [`get_term_postings_enum_with_flag`] to control the requested postings features.
pub fn get_term_postings_enum<IR>(
  reader: IR,
  field: &str,
  term: &BytesRef<Vec<u8>>,
) -> Result<Option<TermsPostingType<IR>>>
where
  IR: IndexReader,
{
  get_term_postings_enum_with_flag(reader, field, term, ALL as i32)
}

/// Returns [`PostingsEnum`](crate::core::index::postings_enum::PostingsEnum) for the specified field and term, with control over whether freqs,
/// positions, offsets or payloads are required.
///
/// This returns `None` if the field or term does not exist.
/// See [`TermsEnum::postings`].
pub fn get_term_postings_enum_with_flag<IR>(
  reader: IR,
  field: &str,
  term: &BytesRef<Vec<u8>>,
  flags: i32,
) -> Result<Option<TermsPostingType<IR>>>
where
  IR: IndexReader,
{
  if let Some(terms) = get_terms(reader, field)? {
    let mut terms_enum = terms.iterator()?;
    if terms_enum.seek_exact(term)? {
      return Ok(Some(terms_enum.postings_with_flags(None, flags)?));
    }
  }
  Ok(None)
}