From bb0d4ae841991fd06a0f5e15f30f01b3c6d39876 Mon Sep 17 00:00:00 2001 From: Benedikt Heinrichs Date: Wed, 26 Jan 2022 15:01:57 +0100 Subject: [PATCH 1/2] Fix: Handle empty AP (coscine/issues#1931) --- src/SemanticSearch/RdfClient.cs | 24 ++++++++++++------ src/SemanticSearch/RdfSearchMapper.cs | 36 +++++++++++++++++++-------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/SemanticSearch/RdfClient.cs b/src/SemanticSearch/RdfClient.cs index 71be2ca..49c1d94 100644 --- a/src/SemanticSearch/RdfClient.cs +++ b/src/SemanticSearch/RdfClient.cs @@ -13,7 +13,7 @@ namespace Coscine.SemanticSearch /// /// Provides all necessary queries to the RDF database to create a mapping of metadata graphs into a document. /// - public class RdfClient + public class RdfClient { public const string LABEL_LITERAL_RULE = "instance"; public const string LABEL_ADDITIONAL_RULE = "graph"; @@ -62,6 +62,10 @@ namespace Coscine.SemanticSearch }} LIMIT 1"; _queryString.SetUri("uri", new Uri(graphName)); var results = _connector.QueryWithResultSet(_queryString); + if (results.Count == 0) + { + return null; + } return results.First().Value("applicationProfile").ToString(); } @@ -192,7 +196,7 @@ namespace Coscine.SemanticSearch // Expected correct URI format for Projects: https://purl.org/coscine/projects/{ProjectGUID} var projectRoleModel = new ProjectRoleModel(); - return projectRoleModel.GetAllWhere(p => p.UserId.Equals(Guid.Parse(user)) && p.Project.Deleted == false).Select(p => $"https://purl.org/coscine/projects/{p.ProjectId}").ToList(); + return projectRoleModel.GetAllWhere(p => p.UserId.Equals(Guid.Parse(user)) && p.Project.Deleted == false).Select(p => $"https://purl.org/coscine/projects/{p.ProjectId}").ToList(); } /// @@ -329,7 +333,7 @@ namespace Coscine.SemanticSearch }} ORDER BY ?order"; _queryString.SetUri("uri", new Uri(graphName)); var results = _connector.QueryWithResultSet(_queryString); - + return results.ToDictionary( x => x.Value("class").ToString(), x => @@ -466,13 +470,13 @@ namespace Coscine.SemanticSearch foreach (var graph in documents.Keys) { jObjects.Add(graph, CreateFields(documents[graph], profile, indexMapper)); - } - } + } + } else if (documents.Count() > 0) { jObjects.Add(graphName, CreateFields(documents[graphName], profile, indexMapper)); } - + return jObjects; } @@ -503,11 +507,17 @@ namespace Coscine.SemanticSearch try { jObject.Merge(_dataTypeParser.Parse(label, triple.Object, indexMapper, profile)); - } catch (NotIndexableException e) + } + catch (NotIndexableException e) { Console.WriteLine($"Property {property} could not be indexed. Reason: {e.Reason}"); continue; } + catch (FormatException e) + { + Console.WriteLine($"Property {property} could not be indexed. Reason: {e.Message}"); + continue; + } } return jObject; } diff --git a/src/SemanticSearch/RdfSearchMapper.cs b/src/SemanticSearch/RdfSearchMapper.cs index 2d10e94..38d5ff4 100644 --- a/src/SemanticSearch/RdfSearchMapper.cs +++ b/src/SemanticSearch/RdfSearchMapper.cs @@ -100,18 +100,28 @@ namespace Coscine.SemanticSearch { _indexMapper = new ElasticsearchIndexMapper(_rdfClient, await _searchClient.GetMappingAsync()); var applicationProfileId = _rdfClient.GetApplicationProfileOfMetadata(graphName); - SpecificApplicationProfile profile = new SpecificApplicationProfile(_rdfClient, applicationProfileId); + // Null should mean that no document exists to the graph + if (applicationProfileId == null) + { + // Graph may only be deleted from the RDF database after the mapping has been deleted in the search engine! + // first the graph is only marked as deleted because additional rules have to be executed because they have effects on other graphs and still need the data of the graph to be deleted + _rdfClient.MarkGraphAsDeleted(graphName); + } + else + { + SpecificApplicationProfile profile = new SpecificApplicationProfile(_rdfClient, applicationProfileId); - // Graph may only be deleted from the RDF database after the mapping has been deleted in the search engine! - // first the graph is only marked as deleted because additional rules have to be executed because they have effects on other graphs and still need the data of the graph to be deleted - _rdfClient.MarkGraphAsDeleted(graphName); + // Graph may only be deleted from the RDF database after the mapping has been deleted in the search engine! + // first the graph is only marked as deleted because additional rules have to be executed because they have effects on other graphs and still need the data of the graph to be deleted + _rdfClient.MarkGraphAsDeleted(graphName); - // construct additional triples for specific application profile - var documents = _rdfClient.CreateFieldsFromAdditionalRule(graphName, profile, _indexMapper, true); - // only the changes of the other documents are considered because the graph will be deleted - documents.Remove(graphName); + // construct additional triples for specific application profile + var documents = _rdfClient.CreateFieldsFromAdditionalRule(graphName, profile, _indexMapper, true); + // only the changes of the other documents are considered because the graph will be deleted + documents.Remove(graphName); - await _searchClient.DeleteDocumentAsync(graphName, documents); + await _searchClient.DeleteDocumentAsync(graphName, documents); + } } /************** INITIAL INDEX ************/ @@ -125,7 +135,7 @@ namespace Coscine.SemanticSearch _indexMapper = new ElasticsearchIndexMapper(_rdfClient); var content = _indexMapper.CreateIndex(ElasticsearchIndexMapper.DEFAULT_ALIAS_NAME); - await _searchClient.CreateIndexAsync(content, GetCurrentIndex()); + await _searchClient.CreateIndexAsync(content, GetCurrentIndex()); _indexMapper.ReplaceMapping(await _searchClient.GetMappingAsync()); @@ -160,7 +170,7 @@ namespace Coscine.SemanticSearch var oldIndex = GetCurrentIndex(); _version++; // increase version _rdfClient.SetCurrentIndexVersion(_version); - var newIndex = GetCurrentIndex(); + var newIndex = GetCurrentIndex(); _searchClient.ChangeIndex(newIndex); _indexMapper.ReplaceMapping(await _searchClient.GetMappingAsync()); @@ -194,6 +204,10 @@ namespace Coscine.SemanticSearch private IDictionary CreateDocument(string graphName, bool changeOtherDocs) { var applicationProfileId = _rdfClient.GetApplicationProfileOfMetadata(graphName); + if (applicationProfileId == null) + { + return new Dictionary { { graphName, new JObject() } }; + } SpecificApplicationProfile profile = new SpecificApplicationProfile(_rdfClient, applicationProfileId); // create properties -- GitLab From 482868a3c38bfd6ead08f4468c3baf675f1b922f Mon Sep 17 00:00:00 2001 From: CoscineBot Date: Wed, 26 Jan 2022 14:03:52 +0000 Subject: [PATCH 2/2] Chore: 1.2.1 # [1.2.1](https://git.rwth-aachen.de/coscine/backend/scripts/semanticsearch/compare/v1.2.0...v1.2.1) (2022-01-26) ## Fix * Handle empty AP (coscine/issues#1931) * Handle empty AP (coscine/issues#1931) --- src/SemanticSearch/SemanticSearch.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SemanticSearch/SemanticSearch.csproj b/src/SemanticSearch/SemanticSearch.csproj index 4e0564f..03b9f48 100644 --- a/src/SemanticSearch/SemanticSearch.csproj +++ b/src/SemanticSearch/SemanticSearch.csproj @@ -5,7 +5,7 @@ true Coscine.SemanticSearch Coscine.SemanticSearch - 1.2.0 + 1.2.1 RWTH Aachen University -- GitLab