diff --git a/publish/src/app.rs b/publish/src/app.rs index b20be90..2729ee4 100644 --- a/publish/src/app.rs +++ b/publish/src/app.rs @@ -1,7 +1,7 @@ use crate::rdf::ontology; use crate::rdf::ontology::Ontology; use crate::rdf::term_helper::{TermHelper, TermHelperMut}; -use gl_search::{Schema, SearchDocument, SearchIndex}; +use gl_search::{doc, Schema, SearchDocument, SearchIndex}; use http::StatusCode; use iced::alignment::Horizontal; use iced::widget::button::Style; @@ -23,6 +23,8 @@ use oxigraph::model::vocab::rdf; use oxigraph::model::{BaseDirection, Dataset, NamedNode, Quad, Term, TermRef}; use tracing::{debug, error}; +const ANNOTATED_IRI_TYPE: u64 = 0; + #[derive(Debug, Clone)] pub(crate) enum Message { None, @@ -104,11 +106,22 @@ impl Publisher { .build() .expect("Failed to build search index"); - index.remove_all_annotated_iris(); + index.remove_all_of_type(ANNOTATED_IRI_TYPE); ontology.for_each_annotated_iri(|iri, label, comment| { - index - .add_annotated_iri(iri, label, comment) - .expect("Unable to add annotated IRI to search index"); + let mut document = doc!( + Schema::type_field() => ANNOTATED_IRI_TYPE, + Schema::iri_field() => iri, + ); + + if let Some(label) = label { + document.add_text(Schema::field("label"), label.to_lowercase()); + } + + if let Some(comment) = comment { + document.add_text(Schema::field("comment"), comment.to_lowercase()); + } + + index.add(document).expect("Unable to add annotated IRI to search index"); }); index.commit().expect("Unable to commit ontology to index"); @@ -309,7 +322,7 @@ impl Publisher { if let Some(search_state) = &mut self.search_state { search_state.results = self .index - .query(7, new_query.as_str(), Schema::default_fields()) + .query(0, new_query.as_str(), Schema::all_fields()) .expect("Error encountered while querying index") .iter() .map(NamedNode::new_unchecked) diff --git a/search/src/index.rs b/search/src/index.rs index 22de669..6134aa9 100644 --- a/search/src/index.rs +++ b/search/src/index.rs @@ -7,7 +7,7 @@ use tantivy::directory::{ManagedDirectory, MmapDirectory}; use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery}; use tantivy::schema::{Field, IndexRecordOption, Value}; use tantivy::tokenizer::{NgramTokenizer, TokenizerManager}; -use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term, doc}; +use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term}; #[derive(Default)] pub struct SearchIndexBuilder { @@ -62,40 +62,16 @@ impl SearchIndex { SearchIndexBuilder::default() } - pub fn remove_all_annotated_iris(&mut self) { - self.writer - .delete_term(Term::from_field_u64(Schema::type_field(), 0u64)); - self.writer.commit().unwrap(); - } - - pub fn add_annotated_iri( - &self, - iri: &str, - label: Option<&str>, - comment: Option<&str>, - ) -> crate::Result<()> { - let mut document = doc!( - Schema::type_field() => 0u64, - Schema::iri_field() => iri, - ); - - if let Some(label) = label { - document.add_text(Schema::label_field(), label.to_lowercase()); - } - - if let Some(comment) = comment { - document.add_text(Schema::comment_field(), comment); - } - - self.writer.add_document(document)?; - Ok(()) - } - pub fn add<'a>(&self, document: TantivyDocument) -> crate::Result<()> { self.writer.add_document(document)?; Ok(()) } + pub fn remove_all_of_type(&mut self, type_: u64) { + self.writer + .delete_term(Term::from_field_u64(Schema::type_field(), type_)); + } + pub fn commit(&mut self) -> crate::Result<()> { self.writer.commit()?; Ok(()) diff --git a/search/src/lib.rs b/search/src/lib.rs index f5e2089..2cc46b8 100644 --- a/search/src/lib.rs +++ b/search/src/lib.rs @@ -3,6 +3,7 @@ mod index; mod schema; pub use tantivy::TantivyDocument as SearchDocument; +pub use tantivy::doc; pub use error::{Result, SearchError}; pub use index::{SearchIndex, SearchIndexBuilder}; diff --git a/search/src/schema.rs b/search/src/schema.rs index 5518eea..a5d8530 100644 --- a/search/src/schema.rs +++ b/search/src/schema.rs @@ -53,25 +53,15 @@ impl Schema { Self::schema().get_field("iri").unwrap() } - pub fn label_field() -> Field { - Self::schema().get_field("label").unwrap() + pub fn field(name: &str) -> Field { + Schema::schema() + .get_field(name) + .expect("Field not found in schema") } - pub fn comment_field() -> Field { - Self::schema().get_field("comment").unwrap() - } - - pub fn ontology_fields() -> Vec { - vec![Self::label_field(), Self::comment_field()] - } - - pub fn default_fields() -> Vec { - 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(), - ] + pub fn all_fields() -> Vec { + Self::schema().fields() + .map(|(field, _)| field) + .collect() } }