.
This commit is contained in:
+63
-68
@@ -1,7 +1,7 @@
|
||||
use crate::rdf::ontology::{LabeledIri, Ontology};
|
||||
use crate::rdf::term_helper::{TermHelper, TermHelperMut};
|
||||
use crate::rdf::vocab::{gl, rda};
|
||||
use gl_search::{Schema, SearchDocument, SearchIndex, NamedFieldDocument, OwnedValue, doc};
|
||||
use gl_search::{Schema, SearchDocument, SearchIndex, NamedFieldDocument, OwnedValue, doc, IndexWriter};
|
||||
use http::StatusCode;
|
||||
use iced::alignment::Horizontal;
|
||||
use iced::widget::button::Style;
|
||||
@@ -43,6 +43,7 @@ pub(crate) enum Message {
|
||||
HoverRow(QuadKey),
|
||||
UnhoverRow(QuadKey),
|
||||
QueryUpdated(String),
|
||||
SetSearchResults(Vec<NamedFieldDocument>),
|
||||
QueryTypeUpdated(NamedNode),
|
||||
SearchResultClicked(NamedNode),
|
||||
DatatypeUpdated(QuadKey, Option<String>),
|
||||
@@ -88,6 +89,7 @@ pub(crate) struct Publisher {
|
||||
hovered_row: Option<QuadKey>,
|
||||
search_state: Option<SearchState>,
|
||||
index: SearchIndex,
|
||||
index_writer: Option<IndexWriter>,
|
||||
show_overwrite_confirmation: bool,
|
||||
modified: bool,
|
||||
show_new_document_buttons: bool,
|
||||
@@ -108,26 +110,39 @@ impl Publisher {
|
||||
.build()
|
||||
.expect("Failed to build search index");
|
||||
|
||||
if let Some(id) = ontology.catalog_id(&rdf::PROPERTY.into_owned()) { index.remove_all_of_type(id); }
|
||||
if let Some(id) = ontology.catalog_id(&rdfs::CLASS.into_owned()) { index.remove_all_of_type(id); }
|
||||
let writer = index.writer().expect("Failed to create search index writer");
|
||||
|
||||
for (iri, entity) in ontology.entities() {
|
||||
let mut document = doc!(
|
||||
Schema::type_field() => entity.catalog_id,
|
||||
Schema::iri_field() => iri.as_str(),
|
||||
);
|
||||
let classes_to_index = [
|
||||
rdf::PROPERTY,
|
||||
rdfs::CLASS,
|
||||
];
|
||||
|
||||
if let Some(field) = ontology.field_for_property(&rdfs::LABEL.into_owned()) {
|
||||
document.add_text(Schema::field(&field.name), &entity.label);
|
||||
for class in &classes_to_index {
|
||||
let class = class.into_owned();
|
||||
if let Some(id) = ontology.catalog_id(&class) {
|
||||
index.remove_all_of_type(id).expect("Failed to remove documents from search index");
|
||||
for individual in ontology.individuals(&class) {
|
||||
let info = ontology.info(&individual);
|
||||
if info.label.is_some() || info.comment.is_some() {
|
||||
let mut document = doc!(
|
||||
Schema::type_field() => id,
|
||||
Schema::iri_field() => individual.as_str(),
|
||||
);
|
||||
|
||||
if let Some(field) = ontology.field_for_property(&rdfs::LABEL.into_owned()) {
|
||||
document.add_text(Schema::field(&field.name), info.label.unwrap_or_default());
|
||||
}
|
||||
|
||||
if let Some(field) = ontology.field_for_property(&rdfs::COMMENT.into_owned()) {
|
||||
document.add_text(Schema::field(&field.name), info.comment.unwrap_or_default());
|
||||
}
|
||||
|
||||
writer.add_document(document).expect("Failed to add document to search index");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(field) = ontology.field_for_property(&rdfs::COMMENT.into_owned()) {
|
||||
document.add_text(Schema::field(&field.name), entity.comment.clone().unwrap_or(String::from("")));
|
||||
}
|
||||
|
||||
index.add(document).expect("Unable to add annotated IRI to search index");
|
||||
}
|
||||
index.commit().expect("Unable to commit ontology to index");
|
||||
|
||||
index
|
||||
});
|
||||
|
||||
@@ -161,6 +176,7 @@ impl Publisher {
|
||||
hovered_row: None,
|
||||
search_state: None,
|
||||
index,
|
||||
index_writer: None,
|
||||
show_overwrite_confirmation: false,
|
||||
modified: false,
|
||||
show_new_document_buttons: false,
|
||||
@@ -178,6 +194,9 @@ impl Publisher {
|
||||
let client = self.http_client.clone();
|
||||
let root = Url::parse(&self.url_input).expect("Invalid URL");
|
||||
let stream = Traverse::new(client, root, None);
|
||||
|
||||
self.index_writer = Some(self.index.writer().expect("Unable to create search index writer"));
|
||||
|
||||
task = Task::run(stream, |result| match result {
|
||||
Ok(rdf_source) => Message::IndexRdfSource(rdf_source),
|
||||
Err(err) => Message::ShowError(format!("Unable to fetch RDF Source: {err}")),
|
||||
@@ -203,14 +222,14 @@ impl Publisher {
|
||||
}
|
||||
}
|
||||
|
||||
self.index
|
||||
.add(document)
|
||||
.expect("Unable to add document to search index");
|
||||
if let Some(writer) = &self.index_writer {
|
||||
writer.add_document(document).expect("Unable to add document to search index");
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::CommitIndex => {
|
||||
if let Err(err) = self.index.commit() {
|
||||
task = Task::done(Message::ShowError(format!("Unable to commit index: {err}")));
|
||||
if let Some(writer) = &mut self.index_writer {
|
||||
writer.commit().expect("Unable to commit updates to search index");
|
||||
}
|
||||
}
|
||||
Message::WindowClosed(id) => {
|
||||
@@ -349,13 +368,22 @@ impl Publisher {
|
||||
SearchResultClickAction::URLInput => self.ontology.catalog_id(&search_state.type_.iri),
|
||||
};
|
||||
|
||||
search_state.results = self
|
||||
.index
|
||||
.query(catalog_id, new_query.as_str(), Schema::all_fields())
|
||||
.expect("Error encountered while querying index");
|
||||
search_state.query = new_query;
|
||||
search_state.query = new_query.clone();
|
||||
let index = self.index.clone();
|
||||
task = Task::future(async move {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let result = index.query(catalog_id, new_query.as_str(), Schema::all_fields())
|
||||
.expect("Error encountered while querying index");
|
||||
Message::SetSearchResults(result)
|
||||
}).await.unwrap()
|
||||
});
|
||||
};
|
||||
}
|
||||
Message::SetSearchResults(results) => {
|
||||
if let Some(search_state) = &mut self.search_state {
|
||||
search_state.results = results;
|
||||
}
|
||||
}
|
||||
Message::SearchResultClicked(node) => {
|
||||
if let Some(search_state) = &self.search_state {
|
||||
match search_state.action {
|
||||
@@ -542,12 +570,12 @@ impl Publisher {
|
||||
|
||||
let term = TermHelper::new(&triple.object);
|
||||
|
||||
let value_label = "bar"; /*term.value_as_named_node().and_then(|node| {
|
||||
let value_label = term.value_as_named_node().and_then(|node| {
|
||||
self.ontology
|
||||
.info(node)
|
||||
.and_then(|info| info.label.clone())
|
||||
.info(&node.into_owned())
|
||||
.label
|
||||
.map(|label| container(text(label)))
|
||||
});*/
|
||||
});
|
||||
|
||||
let value = term
|
||||
.value_as_named_node()
|
||||
@@ -563,43 +591,10 @@ impl Publisher {
|
||||
})
|
||||
.unwrap_or(Horizontal::Left);
|
||||
|
||||
/*let value_input_base = text_input("Value", value.as_str())
|
||||
.align_x(value_alignment);*/
|
||||
|
||||
/*let value_input = if !state.read_only {
|
||||
let is_named_node = term.is_named_node();
|
||||
value_input_base.on_input(move |input| {
|
||||
let expanded_input = if is_named_node {
|
||||
self.ontology
|
||||
.expand(input.as_str())
|
||||
.map(|node| node.as_str().to_string())
|
||||
.unwrap_or(input)
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
Message::ValueUpdated(key, expanded_input)
|
||||
})
|
||||
} else {
|
||||
value_input_base
|
||||
};*/
|
||||
|
||||
let foo = iri_input(self.ontology.prefixes(), "Object", value.as_str())
|
||||
.on_input(move |value| Message::ValueUpdated(key, value))
|
||||
.on_control_click(Message::OpenQueryWindow(SearchResultClickAction::Object(key)));
|
||||
|
||||
/*let search_launcher = if term.is_named_node() && !state.read_only {
|
||||
Some(container(
|
||||
button(text("\u{1f50e}"))
|
||||
.on_press(Message::OpenQueryWindow(SearchResultClickAction::Object(
|
||||
key,
|
||||
)))
|
||||
.style(button::text),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};*/
|
||||
|
||||
let selected_datatype = term.datatype().map(|node| self.ontology.abbreviate(node));
|
||||
let datatype_selector: Element<Message> = if state.read_only {
|
||||
selected_datatype.map(text).into()
|
||||
@@ -671,11 +666,11 @@ impl Publisher {
|
||||
entities: impl Iterator<Item = NamedNode>,
|
||||
) -> Element<'_, Message> {
|
||||
let buttons = entities.map(|entity| {
|
||||
let label = "baz"; /*self
|
||||
let label = self
|
||||
.ontology
|
||||
.info(entity.as_ref())
|
||||
.and_then(|info| info.label.clone())
|
||||
.unwrap_or(entity.as_str().to_string());*/
|
||||
.info(&entity)
|
||||
.label
|
||||
.unwrap_or(entity.as_str().to_string());
|
||||
button(text(label))
|
||||
.on_press(Message::NewDocument(entity))
|
||||
.into()
|
||||
|
||||
Reference in New Issue
Block a user