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(
fun getInstitutionName(identifier: String): FacetContainer {
return try {
log.info("Attempting to retrieve institution record.")
log.debug("Attempting to retrieve institution record.")
val request = GetRequest(institutionIndex, identifier)
val response = client.get(request, RequestOptions.DEFAULT)
if (response.isExists) {
log.info("Successfully retrieved institution name.")
FacetContainer(
LanguageContainer.fromMap(response.sourceAsMap.getValue("name")),
identifier,
emptyList()
)
val map = response.sourceAsMap["name"]
if (map == null) {
log.error("Found institution for id $identifier in index $institutionIndex, but could not extract name.")
FacetContainer(
LanguageContainer.EMPTY,
identifier,
emptyList()
)
} else {
log.debug("Retrieval of institution names was successful.")
FacetContainer(
LanguageContainer.fromMap(map),
identifier,
emptyList()
)
}
} else {
log.error("Could not find institution $identifier in index $institutionIndex.")
FacetContainer(
......@@ -163,8 +173,6 @@ class ElasticSearchWrapper(
emptyList()
)
}
} catch (ex: ElasticsearchException) {
log.error(ex.detailedMessage)
FacetContainer(
......@@ -173,19 +181,22 @@ class ElasticSearchWrapper(
emptyList()
)
}
}
fun getRecordSetName(identifier: String): LanguageContainer {
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 response = client.get(request, RequestOptions.DEFAULT)
if (response.isExists) {
val map = response.sourceAsMap.getValue("name")
log.info("Successfully retrieved record set names: $map.")
LanguageContainer.fromMap(map)
val map = response.sourceAsMap["name"]
if (map == null) {
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 {
log.error("Could not find record set $identifier in index $recordSetIndex.")
LanguageContainer.EMPTY
......
......@@ -41,21 +41,36 @@ data class LanguageContainer(
return LanguageContainer(listOf(placeholder), listOf(placeholder), listOf(placeholder), listOf(placeholder))
}
fun fromMap(map: Any): LanguageContainer {
fun fromMap(map: Any?): LanguageContainer {
return when (map) {
is Map<*, *> -> {
LanguageContainer(
map["de"] as List<String>,
map["fr"] as List<String>,
map["it"] as List<String>,
map["un"] as List<String>
)
val languageContainer = LanguageContainer(
extractMapItem(map as Map<String, Any?>, "de"),
extractMapItem(map, "fr"),
extractMapItem(map, "it"),
extractMapItem(map, "unt")
)
languageContainer
}
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 languageJsonLDFieldName = "@language"
}
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