.
This commit is contained in:
+19
-6
@@ -1,7 +1,7 @@
|
|||||||
use crate::rdf::ontology;
|
use crate::rdf::ontology;
|
||||||
use crate::rdf::ontology::Ontology;
|
use crate::rdf::ontology::Ontology;
|
||||||
use crate::rdf::term_helper::{TermHelper, TermHelperMut};
|
use crate::rdf::term_helper::{TermHelper, TermHelperMut};
|
||||||
use gl_search::{Schema, SearchDocument, SearchIndex};
|
use gl_search::{doc, Schema, SearchDocument, SearchIndex};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use iced::alignment::Horizontal;
|
use iced::alignment::Horizontal;
|
||||||
use iced::widget::button::Style;
|
use iced::widget::button::Style;
|
||||||
@@ -23,6 +23,8 @@ use oxigraph::model::vocab::rdf;
|
|||||||
use oxigraph::model::{BaseDirection, Dataset, NamedNode, Quad, Term, TermRef};
|
use oxigraph::model::{BaseDirection, Dataset, NamedNode, Quad, Term, TermRef};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
|
const ANNOTATED_IRI_TYPE: u64 = 0;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) enum Message {
|
pub(crate) enum Message {
|
||||||
None,
|
None,
|
||||||
@@ -104,11 +106,22 @@ impl Publisher {
|
|||||||
.build()
|
.build()
|
||||||
.expect("Failed to build search index");
|
.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| {
|
ontology.for_each_annotated_iri(|iri, label, comment| {
|
||||||
index
|
let mut document = doc!(
|
||||||
.add_annotated_iri(iri, label, comment)
|
Schema::type_field() => ANNOTATED_IRI_TYPE,
|
||||||
.expect("Unable to add annotated IRI to search index");
|
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");
|
index.commit().expect("Unable to commit ontology to index");
|
||||||
|
|
||||||
@@ -309,7 +322,7 @@ impl Publisher {
|
|||||||
if let Some(search_state) = &mut self.search_state {
|
if let Some(search_state) = &mut self.search_state {
|
||||||
search_state.results = self
|
search_state.results = self
|
||||||
.index
|
.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")
|
.expect("Error encountered while querying index")
|
||||||
.iter()
|
.iter()
|
||||||
.map(NamedNode::new_unchecked)
|
.map(NamedNode::new_unchecked)
|
||||||
|
|||||||
+6
-30
@@ -7,7 +7,7 @@ use tantivy::directory::{ManagedDirectory, MmapDirectory};
|
|||||||
use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery};
|
use tantivy::query::{BooleanQuery, Occur, QueryParser, TermQuery};
|
||||||
use tantivy::schema::{Field, IndexRecordOption, Value};
|
use tantivy::schema::{Field, IndexRecordOption, Value};
|
||||||
use tantivy::tokenizer::{NgramTokenizer, TokenizerManager};
|
use tantivy::tokenizer::{NgramTokenizer, TokenizerManager};
|
||||||
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term, doc};
|
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument, Term};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct SearchIndexBuilder {
|
pub struct SearchIndexBuilder {
|
||||||
@@ -62,40 +62,16 @@ impl SearchIndex {
|
|||||||
SearchIndexBuilder::default()
|
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<()> {
|
pub fn add<'a>(&self, document: TantivyDocument) -> crate::Result<()> {
|
||||||
self.writer.add_document(document)?;
|
self.writer.add_document(document)?;
|
||||||
Ok(())
|
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<()> {
|
pub fn commit(&mut self) -> crate::Result<()> {
|
||||||
self.writer.commit()?;
|
self.writer.commit()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ mod index;
|
|||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
pub use tantivy::TantivyDocument as SearchDocument;
|
pub use tantivy::TantivyDocument as SearchDocument;
|
||||||
|
pub use tantivy::doc;
|
||||||
|
|
||||||
pub use error::{Result, SearchError};
|
pub use error::{Result, SearchError};
|
||||||
pub use index::{SearchIndex, SearchIndexBuilder};
|
pub use index::{SearchIndex, SearchIndexBuilder};
|
||||||
|
|||||||
+8
-18
@@ -53,25 +53,15 @@ impl Schema {
|
|||||||
Self::schema().get_field("iri").unwrap()
|
Self::schema().get_field("iri").unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn label_field() -> Field {
|
pub fn field(name: &str) -> Field {
|
||||||
Self::schema().get_field("label").unwrap()
|
Schema::schema()
|
||||||
|
.get_field(name)
|
||||||
|
.expect("Field not found in schema")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment_field() -> Field {
|
pub fn all_fields() -> Vec<Field> {
|
||||||
Self::schema().get_field("comment").unwrap()
|
Self::schema().fields()
|
||||||
}
|
.map(|(field, _)| field)
|
||||||
|
.collect()
|
||||||
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(),
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user