Commit 58952c37 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Improve logging & exception handling of elastic functions.

parent dde8a912
Pipeline #21865 passed with stages
in 5 minutes and 17 seconds
...@@ -145,16 +145,26 @@ class ElasticSearchWrapper( ...@@ -145,16 +145,26 @@ class ElasticSearchWrapper(
fun getInstitutionName(identifier: String): FacetContainer { fun getInstitutionName(identifier: String): FacetContainer {
return try { return try {
log.info("Attempting to retrieve institution record.") log.debug("Attempting to retrieve institution record.")
val request = GetRequest(institutionIndex, identifier) val request = GetRequest(institutionIndex, identifier)
val response = client.get(request, RequestOptions.DEFAULT) val response = client.get(request, RequestOptions.DEFAULT)
if (response.isExists) { if (response.isExists) {
log.info("Successfully retrieved institution name.") val map = response.sourceAsMap["name"]
FacetContainer( if (map == null) {
LanguageContainer.fromMap(response.sourceAsMap.getValue("name")), log.error("Found institution for id $identifier in index $institutionIndex, but could not extract name.")
identifier, FacetContainer(
emptyList() LanguageContainer.EMPTY,
) identifier,
emptyList()
)
} else {
log.debug("Retrieval of institution names was successful.")
FacetContainer(
LanguageContainer.fromMap(map),
identifier,
emptyList()
)
}
} else { } else {
log.error("Could not find institution $identifier in index $institutionIndex.") log.error("Could not find institution $identifier in index $institutionIndex.")
FacetContainer( FacetContainer(
...@@ -163,8 +173,6 @@ class ElasticSearchWrapper( ...@@ -163,8 +173,6 @@ class ElasticSearchWrapper(
emptyList() emptyList()
) )
} }
} catch (ex: ElasticsearchException) { } catch (ex: ElasticsearchException) {
log.error(ex.detailedMessage) log.error(ex.detailedMessage)
FacetContainer( FacetContainer(
...@@ -173,19 +181,22 @@ class ElasticSearchWrapper( ...@@ -173,19 +181,22 @@ class ElasticSearchWrapper(
emptyList() emptyList()
) )
} }
} }
fun getRecordSetName(identifier: String): LanguageContainer { fun getRecordSetName(identifier: String): LanguageContainer {
return try { return try {
log.info("Attempting to retrieve record set document from $recordSetIndex.") log.debug("Attempting to retrieve record set document from $recordSetIndex.")
val request = GetRequest(recordSetIndex, identifier) val request = GetRequest(recordSetIndex, identifier)
val response = client.get(request, RequestOptions.DEFAULT) val response = client.get(request, RequestOptions.DEFAULT)
if (response.isExists) { if (response.isExists) {
val map = response.sourceAsMap.getValue("name") val map = response.sourceAsMap["name"]
log.info("Successfully retrieved record set names: $map.") if (map == null) {
LanguageContainer.fromMap(map) log.error("Found record set for id $identifier in $recordSetIndex, but could not retrieve names in field 'name'.")
LanguageContainer.EMPTY
} else {
log.debug("Retrieval of record set names was successful.")
LanguageContainer.fromMap(map)
}
} else { } else {
log.error("Could not find record set $identifier in index $recordSetIndex.") log.error("Could not find record set $identifier in index $recordSetIndex.")
LanguageContainer.EMPTY LanguageContainer.EMPTY
......
...@@ -41,21 +41,36 @@ data class LanguageContainer( ...@@ -41,21 +41,36 @@ data class LanguageContainer(
return LanguageContainer(listOf(placeholder), listOf(placeholder), listOf(placeholder), listOf(placeholder)) return LanguageContainer(listOf(placeholder), listOf(placeholder), listOf(placeholder), listOf(placeholder))
} }
fun fromMap(map: Any): LanguageContainer { fun fromMap(map: Any?): LanguageContainer {
return when (map) { return when (map) {
is Map<*, *> -> { is Map<*, *> -> {
LanguageContainer( val languageContainer = LanguageContainer(
map["de"] as List<String>, extractMapItem(map as Map<String, Any?>, "de"),
map["fr"] as List<String>, extractMapItem(map, "fr"),
map["it"] as List<String>, extractMapItem(map, "it"),
map["un"] as List<String> extractMapItem(map, "unt")
) )
languageContainer
} }
else -> EMPTY else -> EMPTY
} }
} }
private fun extractMapItem(map: Map<String, Any?>, language: String): List<String> {
return when (val item = map[language]) {
is String -> listOf(item)
is List<*> ->
try {
item as List<String>
} catch (ex: ClassCastException) {
emptyList<String>()
}
else -> emptyList()
}
}
const val valueJsonLDFieldName = "@value" const val valueJsonLDFieldName = "@value"
const val languageJsonLDFieldName = "@language" const val languageJsonLDFieldName = "@language"
} }
fun add(value: Any?): LanguageContainer { fun add(value: Any?): LanguageContainer {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment