mod field;
mod index;
pub use field::{Field, kind};
pub use index::{Drift, Index, Indexes, MappingReport, Version, Versioned, Written};
#[cfg(feature = "derive")]
pub use oxsearch_derive::Document;
use oxsearch_core::shapes::IndexMap;
use oxsearch_mapping::{
BooleanProperty, ByteNumberProperty, DoubleNumberProperty, FloatNumberProperty,
IntegerNumberProperty, LongNumberProperty, Property, ShortNumberProperty, TextProperty,
TypeMapping, UnsignedLongNumberProperty,
};
use serde::Serialize;
use serde::de::DeserializeOwned;
pub trait Document: Serialize + DeserializeOwned {
const INDEX: Option<&'static str>;
fn properties() -> IndexMap<String, Property>;
fn mapping() -> TypeMapping {
TypeMapping::new().properties(Self::properties())
}
fn id(&self) -> Option<String> {
None
}
fn routing(&self) -> Option<String> {
None
}
}
pub trait Mapped {
type Kind;
fn property() -> Property;
}
macro_rules! mapped {
($kind:ident: $($ty:ty => $property:expr),* $(,)?) => {
$(
impl Mapped for $ty {
type Kind = kind::$kind;
fn property() -> Property {
$property
}
}
)*
};
}
mapped!(Text: String => Property::Text(TextProperty::new()));
mapped!(Boolean: bool => Property::Boolean(BooleanProperty::new()));
mapped!(Number:
i8 => Property::ByteNumber(ByteNumberProperty::new()),
i16 => Property::ShortNumber(ShortNumberProperty::new()),
i32 => Property::IntegerNumber(IntegerNumberProperty::new()),
i64 => Property::LongNumber(LongNumberProperty::new()),
u8 => Property::ShortNumber(ShortNumberProperty::new()),
u16 => Property::IntegerNumber(IntegerNumberProperty::new()),
u32 => Property::LongNumber(LongNumberProperty::new()),
u64 => Property::UnsignedLongNumber(UnsignedLongNumberProperty::new()),
f32 => Property::FloatNumber(FloatNumberProperty::new()),
f64 => Property::DoubleNumber(DoubleNumberProperty::new()),
);
macro_rules! many {
($($ty:ty),* $(,)?) => {
$(
impl<T: Mapped> Mapped for $ty {
type Kind = T::Kind;
fn property() -> Property {
T::property()
}
}
)*
};
}
many!(
Option<T>,
Vec<T>,
Box<T>,
std::collections::VecDeque<T>,
std::collections::BTreeSet<T>,
std::collections::HashSet<T>,
);
impl<T: Mapped, const N: usize> Mapped for [T; N] {
type Kind = T::Kind;
fn property() -> Property {
T::property()
}
}
#[doc(hidden)]
pub mod __private {
pub use oxsearch_core::shapes::IndexMap;
pub use oxsearch_mapping::{
DateProperty, DenseVectorElementType, DenseVectorProperty, DenseVectorSimilarity,
KeywordProperty, ObjectProperty, Property, SemanticTextProperty, SparseVectorProperty,
TextProperty,
};
pub use super::field::kind;
pub use super::{Document, Field, Mapped};
pub fn text(value: &impl ToString) -> String {
value.to_string()
}
}