use crate::json::ShouldSkip;
use std::collections::HashMap;
use serde::ser::{Serializer};
use serde::Serialize;
#[derive(Debug, Clone)]
pub enum Encoders {
Default,
HTML,
}
impl Serialize for Encoders {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Encoders::Default => "default",
Encoders::HTML => "html",
}
.serialize(serializer)
}
}
#[derive(Debug, Clone)]
pub enum SettingTypes {
Plain,
FVH,
Postings,
}
impl Serialize for SettingTypes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
SettingTypes::Plain => "plain",
SettingTypes::FVH => "fvh",
SettingTypes::Postings => "postings",
}
.serialize(serializer)
}
}
#[derive(Debug, Clone)]
pub enum IndexOptions {
Offsets,
}
impl Serialize for IndexOptions {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
IndexOptions::Offsets => "offsets",
}
.serialize(serializer)
}
}
#[derive(Debug, Clone)]
pub enum TermVector {
WithPositionsOffsets,
BoundaryChars,
BoundaryMaxScan,
}
impl Serialize for TermVector {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
TermVector::WithPositionsOffsets => "with_positions_offsets",
TermVector::BoundaryChars => "boundary_chars",
TermVector::BoundaryMaxScan => "boundary_max_scan",
}
.serialize(serializer)
}
}
#[derive(Debug, Serialize, Clone)]
pub struct Setting {
#[serde(rename = "type")]
pub setting_type: Option<SettingTypes>,
#[cfg(not(feature = "es5"))]
pub index_options: Option<IndexOptions>,
#[cfg(not(feature = "es5"))]
pub term_vector: Option<TermVector>,
pub force_source: bool,
pub fragment_size: u32,
pub number_of_fragments: u32,
pub no_match_size: u32,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
pub matched_fields: Option<Vec<String>>,
}
impl Default for Setting {
fn default() -> Self {
Setting {
setting_type: None,
force_source: false,
fragment_size: 150,
number_of_fragments: 5,
no_match_size: 0,
matched_fields: None,
#[cfg(not(feature = "es5"))]
index_options: None,
#[cfg(not(feature = "es5"))]
term_vector: None,
}
}
}
impl Setting {
pub fn new() -> Self {
Default::default()
}
pub fn with_type(&mut self, setting_type: SettingTypes) -> &mut Setting {
self.setting_type = Some(setting_type);
self
}
#[cfg(not(feature = "es5"))]
pub fn with_index_options(&mut self, index_options: IndexOptions) -> &mut Setting {
self.index_options = Some(index_options);
self
}
#[cfg(not(feature = "es5"))]
pub fn with_term_vector(&mut self, term_vector: TermVector) -> &mut Setting {
self.term_vector = Some(term_vector);
self
}
pub fn with_force_source(&mut self, force_source: bool) -> &mut Setting {
self.force_source = force_source;
self
}
pub fn with_fragment_size(&mut self, fragment_size: u32) -> &mut Setting {
self.fragment_size = fragment_size;
self
}
pub fn with_number_of_fragments(&mut self, number_of_fragments: u32) -> &mut Setting {
self.number_of_fragments = number_of_fragments;
self
}
pub fn with_no_match_size(&mut self, no_match_size: u32) -> &mut Setting {
self.no_match_size = no_match_size;
self
}
pub fn with_matched_fields(&mut self, matched_fields: Vec<String>) -> &mut Setting {
self.matched_fields = Some(matched_fields);
self
}
}
#[derive(Debug, Default, Serialize, Clone)]
pub struct Highlight {
pub fields: HashMap<String, Setting>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
pub pre_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
pub post_tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
pub encoder: Option<Encoders>,
}
impl Highlight {
pub fn new() -> Highlight {
Highlight {
fields: HashMap::new(),
pre_tags: None,
post_tags: None,
encoder: None,
}
}
pub fn with_encoder(&mut self, encoder: Encoders) -> &mut Highlight {
self.encoder = Some(encoder);
self
}
pub fn with_pre_tags(&mut self, pre_tags: Vec<String>) -> &mut Highlight {
self.pre_tags = Some(pre_tags);
self
}
pub fn with_post_tags(&mut self, post_tags: Vec<String>) -> &mut Highlight {
self.post_tags = Some(post_tags);
self
}
pub fn add_setting(&mut self, name: String, setting: Setting) {
self.fields.insert(name, setting);
}
}
pub type HighlightResult = HashMap<String, Vec<String>>;