Files
tools/search/src/schema.rs
T

58 lines
2.0 KiB
Rust
Raw Normal View History

2026-06-08 19:33:49 -04:00
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(|| {
2026-06-17 20:50:26 -04:00
let stored_ngram32 = TextOptions::default().set_indexing_options(
2026-06-08 19:33:49 -04:00
TextFieldIndexing::default()
.set_index_option(IndexRecordOption::WithFreqsAndPositions)
.set_tokenizer("ngram_32"),
2026-06-17 20:50:26 -04:00
).set_stored();
2026-06-08 19:33:49 -04:00
2026-06-17 20:50:26 -04:00
let stored_en_stem = TextOptions::default().set_indexing_options(
2026-06-08 19:33:49 -04:00
TextFieldIndexing::default()
.set_index_option(IndexRecordOption::WithFreqsAndPositions)
.set_tokenizer("en_stem"),
2026-06-17 20:50:26 -04:00
).set_stored();
2026-06-08 19:33:49 -04:00
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);
2026-07-01 17:42:23 -04:00
schema_builder.add_text_field("label:en", stored_ngram32.clone());
schema_builder.add_text_field("definition:en", stored_en_stem.clone());
2026-06-17 20:50:26 -04:00
/*schema_builder.add_text_field("title", en_stem.clone());
2026-06-08 19:33:49 -04:00
schema_builder.add_text_field("description", en_stem.clone());
2026-06-17 20:50:26 -04:00
schema_builder.add_text_field("content", en_stem);*/
2026-06-08 19:33:49 -04:00
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()
}
2026-07-01 17:42:23 -04:00
pub fn field(name: &str, language: &str) -> Field {
2026-06-09 10:36:13 -04:00
Schema::schema()
2026-07-01 17:42:23 -04:00
.get_field(&format!("{name}:{language}"))
2026-06-09 10:36:13 -04:00
.expect("Field not found in schema")
2026-06-08 19:33:49 -04:00
}
2026-06-09 10:36:13 -04:00
pub fn all_fields() -> Vec<Field> {
2026-06-09 17:20:46 -04:00
Self::schema().fields().map(|(field, _)| field).collect()
2026-06-08 19:33:49 -04:00
}
}