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
/*
 * 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::analysis::analyzer::AnalyzerEnum;
use crate::core::analysis::standard::standard_analyzer::StandardAnalyzer;
use crate::core::codecs::Codecs;
use crate::core::index::flush_policy::FlushPolicyEnum;
use crate::core::index::index_deletion_policy::IndexDeletionPolicyEnum;
use crate::core::index::index_writer::IndexReaderWarmerEnum;
use crate::core::index::index_writer_event_listener::IndexWriterEventListenerEnum;
use crate::core::index::live_index_writer_config::{
  LeafSorter, LiveIndexWriterConfig, LiveIndexWriterConfigBase,
};
use crate::core::index::merge_policy::MergePolicyEnum;
use crate::core::index::merge_scheduler::MergeSchedulerEnum;
use crate::core::search::sort::Sort;
use crate::core::search::sort_field::SortFiledBase;
use crate::core::store::directory::Directory;
use crate::core::util::LATEST;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::info_stream::InfoStreamMT;
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::sync::Arc;
use std::sync::atomic::Ordering;

/// Holds all configuration that is used to create an [`IndexWriter`](crate::core::index::index_writer::IndexWriter).
///
/// Once an [`IndexWriter`](crate::core::index::index_writer::IndexWriter) has been created with this object, changes to this
/// object will not affect that writer instance. For live changes, use the
/// [`LiveIndexWriterConfig`] returned from the writer configuration API.
///
/// Norm computation is selected per write operation with methods such as
/// [`IndexWriter::add_document_with_similarity`](crate::core::index::index_writer::IndexWriter::add_document_with_similarity).
/// The default write methods use the shared BM25 similarity.
///
/// All setter methods return [`IndexWriterConfig`] to allow settings to be
/// chained conveniently.
pub struct IndexWriterConfig<D>
where
  D: Directory,
{
  pub(crate) base: LiveIndexWriterConfigBase<D>,
}

impl<D> IndexWriterConfig<D>
where
  D: Directory,
{
  /// Creates a new config using the default analyzer.
  ///
  /// By default, [`TieredMergePolicy`](crate::core::index::tiered_merge_policy::TieredMergePolicy)
  /// is used for merging. This merge policy is free to select non-contiguous
  /// merges, which means doc IDs may not remain monotonic over time. If this is
  /// a problem, switch to a log-style merge policy.
  pub fn new() -> Result<Self> {
    Self::with_analyzer(StandardAnalyzer::new())
  }

  /// Creates a new config with the provided analyzer.
  ///
  /// By default, [`TieredMergePolicy`](crate::core::index::tiered_merge_policy::TieredMergePolicy)
  /// is used for merging. This merge policy is free to select non-contiguous
  /// merges, which means doc IDs may not remain monotonic over time. If this is
  /// a problem, switch to a log-style merge policy.
  pub fn with_analyzer<T>(analyzer: T) -> Result<Self>
  where
    T: Into<AnalyzerEnum>,
  {
    Ok(Self {
      base: LiveIndexWriterConfigBase::with_analyzer(analyzer)?,
    })
  }

  /// Sets if calls to [`IndexWriter::close`](crate::core::index::index_writer::IndexWriter::close) should first commit before closing.
  ///
  /// Use `true` to match the behavior of Lucene 4.x.
  pub fn set_commit_on_close(&mut self, commit_on_close: bool) -> &mut Self {
    self.base.commit_on_close = commit_on_close;
    self
  }

  /// Expert: sets the amount of time to wait for full-flush merges during
  /// commit or getting a reader from the writer.
  ///
  /// If this time is reached, commit proceeds based on segments merged up to
  /// that point. The merges are not aborted and will still run to completion
  /// independent of the commit or get-reader call, like natural segment merges.
  ///
  /// Set to `0` to disable merging on full flush. If a serial merge scheduler is
  /// used and a non-zero timeout is configured, full-flush merges always wait
  /// for the merge to finish without honoring the configured timeout.
  pub fn set_max_full_flush_merge_wait_millis(
    &mut self,
    max_full_flush_merge_wait_millis: i64,
  ) -> &mut Self {
    self.base.max_full_flush_merge_wait_millis = max_full_flush_merge_wait_millis;
    self
  }

  /// Set the [`Codec`](crate::core::codecs::Codec).
  ///
  /// Only takes effect when [`IndexWriter`](crate::core::index::index_writer::IndexWriter) is first created.
  pub fn set_codec<T>(&mut self, codec: T) -> &mut Self
  where
    T: Into<Codecs>,
  {
    self.base.codec = codec.into();
    self
  }

  /// Specifies [`OpenMode`] of the index.
  ///
  /// Only takes effect when IndexWriter is first created.
  pub fn set_open_mode(&mut self, open_mode: OpenMode) -> &mut Self {
    self.base.open_mode = open_mode;
    self
  }

  /// Expert: sets the compatibility version to use for this index.
  ///
  /// If the index is created, it will use the given major version for
  /// compatibility. It is sometimes useful to set the previous major version for
  /// compatibility because adding indexes only accepts indexes written with the
  /// same major version as the current index. If the index already exists, this
  /// value is ignored. The default value is the major version of the latest
  /// version.
  ///
  /// NOTE: Changing the creation version reduces backward compatibility
  /// guarantees.
  pub fn set_index_created_version_major(
    &mut self,
    index_created_version_major: i32,
  ) -> Result<&mut Self> {
    if index_created_version_major > LATEST.major {
      return Err(LuceneError::illegal_argument(format!(
        "indexCreatedVersionMajor may not be in the future: current major version is {}, but got: {}",
        LATEST.major, index_created_version_major
      )));
    }

    if index_created_version_major < LATEST.major - 1 {
      return Err(LuceneError::illegal_argument(format!(
        "indexCreatedVersionMajor may not be less than the minimum supported version: {}, but got: {}",
        LATEST.major - 1,
        index_created_version_major
      )));
    }

    self.base.created_version_major = index_created_version_major;
    Ok(self)
  }

  /// Expert: allows an optional [`IndexDeletionPolicyEnum`] implementation to
  /// be specified.
  ///
  /// This controls when prior commits are deleted from the index. The default
  /// policy is keep-only-last-commit, which removes all prior commits as soon as
  /// a new commit is done. A custom policy can keep previous point-in-time
  /// commits alive for some time, allowing readers to refresh to the new commit
  /// without having the old commit deleted underneath them.
  ///
  /// This is necessary on filesystems that do not support delete-on-last-close
  /// semantics, which point-in-time search normally relies on.
  ///
  /// Only takes effect when [`IndexWriter`](crate::core::index::index_writer::IndexWriter) is first created.
  pub fn set_index_deletion_policy<T>(&mut self, deletion_policy: T) -> &mut Self
  where
    T: Into<IndexDeletionPolicyEnum<D>>,
  {
    self.base.index_deletion_policy = deletion_policy.into();
    self
  }

  /// Sets the [`Sort`] order to use for all flushed and merged segments.
  pub fn set_index_sort<T>(&mut self, sort: T) -> Result<&mut Self>
  where
    T: Into<Arc<Sort>>,
  {
    let sort = sort.into();
    for sort_field in sort.get_sort() {
      if sort_field.get_index_sorter()?.is_none() {
        return Err(LuceneError::illegal_argument(format!(
          "Cannot sort index with sort field {}",
          sort_field
        )));
      }
    }
    let index_sort_fields: HashSet<String> = sort
      .get_sort()
      .iter()
      .filter_map(|f| f.get_field())
      .map(str::to_string)
      .collect();
    self.base.index_sort_fields = index_sort_fields;
    self.base.index_sort = Some(sort);
    Ok(self)
  }

  /// Expert: sets the merge scheduler used by this writer.
  ///
  /// Only takes effect when [`IndexWriter`](crate::core::index::index_writer::IndexWriter) is first created.
  pub fn set_merge_scheduler<T>(&mut self, merge_scheduler: T) -> &mut Self
  where
    T: Into<MergeSchedulerEnum>,
  {
    let v = merge_scheduler.into();
    self.base.merge_scheduler = v;
    self
  }

  /// Sets the soft deletes field.
  ///
  /// A soft delete field is a doc-values field that marks a document as
  /// soft-deleted if the document has at least one value in that field. A
  /// soft-deleted document is treated as if it has been hard-deleted through the
  /// [`IndexWriter`](crate::core::index::index_writer::IndexWriter) API. Merges reclaim soft-deleted as well as hard-deleted
  /// documents, and index readers obtained from the writer reflect all deleted
  /// documents in their live docs.
  ///
  /// Soft deletes allow documents to be retained across merges if the merge
  /// policy modifies the live docs of a merge reader.
  ///
  /// There is currently no API support to undelete a soft-deleted document; it
  /// must be re-indexed.
  ///
  /// The default is `None`, which disables soft deletes. If soft deletes are
  /// enabled, documents can still be hard-deleted. Hard-deleted documents are
  /// not considered soft-deleted even if they have a value in the soft deletes
  /// field.
  pub fn set_soft_deletes_field<T>(&mut self, soft_deletes_field: T) -> &mut Self
  where
    T: Into<String>,
  {
    let v = soft_deletes_field.into();
    self.base.soft_deletes_field = Some(v);
    self
  }

  /// Set event listener to record key events in [`IndexWriter`](crate::core::index::index_writer::IndexWriter).
  pub fn set_index_writer_event_listener<T>(&mut self, event_listener: T) -> &mut Self
  where
    T: Into<IndexWriterEventListenerEnum>,
  {
    self.base.event_listener = event_listener.into();
    self
  }

  /// Set the merged segment warmer.
  ///
  /// Takes effect on the next merge.
  pub fn set_merged_segment_warmer(
    &mut self,
    merge_segment_warmer: Option<IndexReaderWarmerEnum<D>>,
  ) -> &mut Self {
    self.base.merged_segment_warmer = merge_segment_warmer;
    self
  }

  /// Returns the current merged segment warmer.
  pub fn get_merged_segment_warmer(&self) -> Option<&IndexReaderWarmerEnum<D>> {
    self.base.merged_segment_warmer.as_ref()
  }

  /// Sets the parent document field.
  ///
  /// If this optional property is set, [`IndexWriter`](crate::core::index::index_writer::IndexWriter) adds an internal field to
  /// every root document added to the index writer. A document is considered a
  /// parent document if it is the last document in a document block indexed via
  /// block document APIs, and individual documents added via single-document
  /// methods are also considered parent documents.
  ///
  /// This property is optional for indexes that do not use document blocks in
  /// combination with index sorting. In order to maintain the API guarantee that
  /// document order within a block is not altered by [`IndexWriter`](crate::core::index::index_writer::IndexWriter), a marker for
  /// parent documents is required.
  pub fn set_parent_field<T>(&mut self, parent_field: T) -> &mut Self
  where
    T: Into<String>,
  {
    let v = parent_field.into();
    self.base.parent_field = Some(v);
    self
  }

  /// Sets whether [`IndexWriter`](crate::core::index::index_writer::IndexWriter) should pool readers without requiring a
  /// near-real-time reader to have been opened from the writer.
  ///
  /// If set to `false`, [`IndexWriter`](crate::core::index::index_writer::IndexWriter) will still pool readers once a reader is
  /// opened from the writer.
  ///
  /// Only takes effect when [`IndexWriter`](crate::core::index::index_writer::IndexWriter) is first created.
  pub fn set_reader_pooling(&mut self, reader_pooling: bool) -> &mut Self {
    self.base.reader_pooling = reader_pooling;
    self
  }
  /// Set the comparator for sorting leaf readers. A [`DirectoryReader`](crate::core::index::directory_reader::DirectoryReader) opened
  /// from an [`IndexWriter`](crate::core::index::index_writer::IndexWriter) with this configuration will have its leaf readers
  /// sorted with the provided leaf sorter.
  pub fn set_leaf_sorter(&mut self, leaf_sorter: Option<LeafSorter<D>>) -> &mut Self {
    self.base.leaf_sorter = leaf_sorter;
    self
  }

  /// Returns the comparator for sorting leaf readers, or `None` if no leaf
  /// sorter is set.
  pub fn get_leaf_sorter(&self) -> Option<&LeafSorter<D>> {
    self.base.leaf_sorter.as_ref()
  }
}

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

impl<D> LiveIndexWriterConfig for IndexWriterConfig<D>
where
  D: Directory,
{
  type Directory = D;

  fn get_analyzer(&self) -> &AnalyzerEnum {
    &self.base.analyzer
  }

  fn get_merge_scheduler(&self) -> &MergeSchedulerEnum {
    &self.base.merge_scheduler
  }

  fn get_codec(&self) -> &Codecs {
    &self.base.codec
  }

  fn get_index_sort(&self) -> Option<&Arc<Sort>> {
    self.base.index_sort.as_ref()
  }

  fn get_index_sort_fields(&self) -> &HashSet<String> {
    &self.base.index_sort_fields
  }

  fn get_use_compound_file(&self) -> bool {
    self.base.use_compound_file
  }

  fn get_soft_deletes_field(&self) -> Option<&String> {
    self.base.soft_deletes_field.as_ref()
  }

  fn get_info_stream(&self) -> InfoStreamMT {
    self.base.info_stream.clone()
  }

  fn get_parent_field(&self) -> Option<&String> {
    self.base.parent_field.as_ref()
  }

  fn get_merge_policy(&self) -> &MergePolicyEnum<D> {
    &self.base.merge_policy
  }
  fn get_merge_policy_mut(&mut self) -> &mut MergePolicyEnum<D> {
    &mut self.base.merge_policy
  }
  fn get_flush_policy(&self) -> &FlushPolicyEnum {
    self.base.get_flush_policy()
  }

  fn get_ram_buffer_size_mb(&self) -> f64 {
    self.base.ram_buffer_size_mb
  }

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

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

  fn get_check_pending_flush_on_update(&self) -> bool {
    self
      .base
      .check_pending_flush_on_update
      .load(Ordering::SeqCst)
  }

  fn set_check_pending_flush_update(&self, check_pending_flush_on_update: bool) -> &Self {
    self
      .base
      .check_pending_flush_on_update
      .store(check_pending_flush_on_update, Ordering::SeqCst);
    self
  }

  fn get_index_deletion_policy(&self) -> &IndexDeletionPolicyEnum<D> {
    &self.base.index_deletion_policy
  }

  fn get_max_full_flush_merge_wait_millis(&self) -> i64 {
    self.base.max_full_flush_merge_wait_millis
  }

  fn get_index_writer_event_listener(&self) -> &IndexWriterEventListenerEnum {
    &self.base.event_listener
  }

  fn get_merged_segment_warmer(&self) -> Option<&IndexReaderWarmerEnum<D>> {
    self.base.merged_segment_warmer.as_ref()
  }

  fn get_commit_on_close(&self) -> bool {
    self.base.commit_on_close
  }

  fn get_open_mode(&self) -> &OpenMode {
    &self.base.open_mode
  }

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

  fn get_reader_pooling(&self) -> bool {
    self.base.reader_pooling
  }

  fn get_base_mut(&mut self) -> &mut LiveIndexWriterConfigBase<D> {
    &mut self.base
  }
}

/// Specifies the open mode for [`IndexWriter`](crate::core::index::index_writer::IndexWriter).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenMode {
  /// Creates a new index or overwrites an existing one.
  Create,

  /// Opens an existing index.
  Append,

  /// Creates a new index if one does not exist, otherwise it opens the index
  /// and documents will be appended.
  CreateOrAppend,
}

/// Denotes a flush trigger is disabled.
pub const DISABLE_AUTO_FLUSH: i32 = -1;

/// Disabled by default (because IndexWriter flushes by RAM usage by default).
pub const DEFAULT_MAX_BUFFERED_DELETE_TERMS: i32 = DISABLE_AUTO_FLUSH;

/// Disabled by default (because IndexWriter flushes by RAM usage by default).
pub const DEFAULT_MAX_BUFFERED_DOCS: i32 = DISABLE_AUTO_FLUSH;

/// Default value is 16 MB (which means flush when buffered docs consume approximately 16 MB RAM).
pub const DEFAULT_RAM_BUFFER_SIZE_MB: f64 = 16.0;

/// Default setting (true) for `set_reader_pooling`.
///
/// We changed this default to true with concurrent deletes/updates (LUCENE-7868),
/// because we will otherwise need to open and close segment readers more frequently.
/// False is still supported, but will have worse performance since readers will
/// be forced to aggressively move all state to disk.
pub const DEFAULT_READER_POOLING: bool = true;

/// Default value is 1945. Change using `set_ram_per_thread_hard_limit_mb`.
pub const DEFAULT_RAM_PER_THREAD_HARD_LIMIT_MB: i32 = 1945;

/// Default value for compound file system for newly written segments (set to `true`).
/// For batch indexing with very large ram buffers use `false`.
pub const DEFAULT_USE_COMPOUND_FILE_SYSTEM: bool = true;

/// Default value for whether calls to [`IndexWriter::close`](crate::core::index::index_writer::IndexWriter::close) include a commit.
pub const DEFAULT_COMMIT_ON_CLOSE: bool = true;

/// Default value for time to wait for merges on commit or getReader (when using a
/// [`MergePolicy`](crate::core::index::merge_policy::MergePolicy) that implements [`MergePolicy::find_full_flush_merges`](crate::core::index::merge_policy::MergePolicy::find_full_flush_merges)).
pub const DEFAULT_MAX_FULL_FLUSH_MERGE_WAIT_MILLIS: i64 = 500;