78 lines
2.7 KiB
Rust
78 lines
2.7 KiB
Rust
use std::sync::OnceLock;
|
|||
|
|
use tantivy::schema;
|
||
|
|
use tantivy::schema::{
|
||
|
|
Field, IndexRecordOption, Schema as TantivySchema, TextFieldIndexing, TextOptions,
|
||
|
|
};
|
||
|
|
|
||
|
|
static SCHEMA: OnceLock<TantivySchema> = OnceLock::new();
|
||
|
|
|
||
|
|
pub struct Schema;
|
||
|
|
|
||
|
|
impl Schema {
|
||
|
|
pub fn schema() -> &'static TantivySchema {
|
||
|
|
SCHEMA.get_or_init(|| {
|
||
|
|
let ngram_32 = TextOptions::default().set_indexing_options(
|
||
|
|
TextFieldIndexing::default()
|
||
|
|
.set_index_option(IndexRecordOption::WithFreqsAndPositions)
|
||
|
|
.set_tokenizer("ngram_32"),
|
||
|
|
);
|
||
|
|
|
||
|
|
let en_stem = TextOptions::default().set_indexing_options(
|
||
|
|
TextFieldIndexing::default()
|
||
|
|
.set_index_option(IndexRecordOption::WithFreqsAndPositions)
|
||
|
|
.set_tokenizer("en_stem"),
|
||
|
|
);
|
||
|
|
|
||
|
|
let mut schema_builder = TantivySchema::builder();
|
||
|
|
schema_builder.add_u64_field("type", schema::FAST | schema::INDEXED);
|
||
|
|
schema_builder.add_text_field("iri", schema::STORED | schema::STRING);
|
||
|
|
schema_builder.add_text_field("label", ngram_32.clone());
|
||
|
|
schema_builder.add_text_field("comment", en_stem.clone());
|
||
|
|
|
||
|
|
schema_builder.add_text_field("given name", ngram_32.clone());
|
||
|
|
schema_builder.add_text_field("surname", ngram_32);
|
||
|
|
|
||
|
|
schema_builder.add_text_field("title", en_stem.clone());
|
||
|
|
schema_builder.add_text_field("description", en_stem.clone());
|
||
|
|
schema_builder.add_text_field("content", en_stem);
|
||
|
|
|
||
|
|
schema_builder.add_u64_field("page", schema::STORED);
|
||
|
|
schema_builder.add_u64_field("book", schema::STORED);
|
||
|
|
schema_builder.add_u64_field("chapter", schema::STORED);
|
||
|
|
schema_builder.add_u64_field("verse", schema::STORED);
|
||
|
|
|
||
|
|
schema_builder.build()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn type_field() -> Field {
|
||
|
|
Self::schema().get_field("type").unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn iri_field() -> Field {
|
||
|
|
Self::schema().get_field("iri").unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn label_field() -> Field {
|
||
|
|
Self::schema().get_field("label").unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn comment_field() -> Field {
|
||
|
|
Self::schema().get_field("comment").unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn ontology_fields() -> Vec<Field> {
|
||
|
|
vec![Self::label_field(), Self::comment_field()]
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn default_fields() -> Vec<Field> {
|
||
|
|
vec![
|
||
|
|
Self::schema().get_field("given name").unwrap(),
|
||
|
|
Self::schema().get_field("surname").unwrap(),
|
||
|
|
Self::schema().get_field("title").unwrap(),
|
||
|
|
Self::schema().get_field("description").unwrap(),
|
||
|
|
Self::schema().get_field("content").unwrap(),
|
||
|
|
]
|
||
|
|
}
|
||
|
|
}
|