use std::collections::HashMap;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use crate::analysis::analyzer::analyzer::Analyzer;
use crate::lexical::index::config::InvertedIndexConfig;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum LexicalIndexConfig {
Inverted(InvertedIndexConfig),
}
impl Default for LexicalIndexConfig {
fn default() -> Self {
LexicalIndexConfig::Inverted(InvertedIndexConfig::default())
}
}
impl LexicalIndexConfig {
pub fn builder() -> LexicalIndexConfigBuilder {
LexicalIndexConfigBuilder::new()
}
pub fn index_type_name(&self) -> &str {
match self {
LexicalIndexConfig::Inverted(_) => "Inverted",
}
}
pub fn analyzer(&self) -> &Arc<dyn Analyzer> {
match self {
LexicalIndexConfig::Inverted(config) => &config.analyzer,
}
}
}
pub struct LexicalIndexConfigBuilder {
analyzer: Option<Arc<dyn Analyzer>>,
max_docs_per_segment: Option<u64>,
write_buffer_size: Option<usize>,
compress_stored_fields: Option<bool>,
store_term_vectors: Option<bool>,
merge_factor: Option<u32>,
max_segments: Option<u32>,
default_fields: Vec<String>,
fields: HashMap<String, FieldOption>,
}
use crate::lexical::core::field::FieldOption;
impl Default for LexicalIndexConfigBuilder {
fn default() -> Self {
Self::new()
}
}
impl LexicalIndexConfigBuilder {
pub fn new() -> Self {
Self {
analyzer: None,
max_docs_per_segment: None,
write_buffer_size: None,
compress_stored_fields: None,
store_term_vectors: None,
merge_factor: None,
max_segments: None,
default_fields: Vec::new(),
fields: HashMap::new(),
}
}
pub fn analyzer(mut self, analyzer: Arc<dyn Analyzer>) -> Self {
self.analyzer = Some(analyzer);
self
}
pub fn max_docs_per_segment(mut self, max_docs: u64) -> Self {
self.max_docs_per_segment = Some(max_docs);
self
}
pub fn write_buffer_size(mut self, size: usize) -> Self {
self.write_buffer_size = Some(size);
self
}
pub fn compress_stored_fields(mut self, compress: bool) -> Self {
self.compress_stored_fields = Some(compress);
self
}
pub fn store_term_vectors(mut self, store: bool) -> Self {
self.store_term_vectors = Some(store);
self
}
pub fn merge_factor(mut self, factor: u32) -> Self {
self.merge_factor = Some(factor);
self
}
pub fn max_segments(mut self, max: u32) -> Self {
self.max_segments = Some(max);
self
}
pub fn default_field(mut self, field: impl Into<String>) -> Self {
let field = field.into();
if !self.default_fields.contains(&field) {
self.default_fields.push(field);
}
self
}
pub fn default_fields(mut self, fields: Vec<String>) -> Self {
self.default_fields = fields;
self
}
pub fn fields(mut self, fields: HashMap<String, FieldOption>) -> Self {
self.fields = fields;
self
}
pub fn add_field(mut self, name: impl Into<String>, option: FieldOption) -> Self {
self.fields.insert(name.into(), option);
self
}
pub fn build(self) -> LexicalIndexConfig {
let mut config = InvertedIndexConfig::default();
if let Some(analyzer) = self.analyzer {
config.analyzer = analyzer;
}
if let Some(max_docs) = self.max_docs_per_segment {
config.max_docs_per_segment = max_docs;
}
if let Some(size) = self.write_buffer_size {
config.write_buffer_size = size;
}
if let Some(compress) = self.compress_stored_fields {
config.compress_stored_fields = compress;
}
if let Some(store) = self.store_term_vectors {
config.store_term_vectors = store;
}
if let Some(factor) = self.merge_factor {
config.merge_factor = factor;
}
if let Some(max) = self.max_segments {
config.max_segments = max;
}
if !self.default_fields.is_empty() {
config.default_fields = self.default_fields;
}
if !self.fields.is_empty() {
config.fields = self.fields;
}
LexicalIndexConfig::Inverted(config)
}
}