Commit 8e6ce5bb authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Add some documentation

parent 2cf53d1e
......@@ -4,6 +4,7 @@ import java.util.Properties
import org.apache.http.HttpHost
import org.apache.logging.log4j.LogManager
import org.elasticsearch.ElasticsearchException
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest
import org.elasticsearch.client.RequestOptions
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient
......@@ -11,14 +12,32 @@ import org.elasticsearch.client.core.CountRequest
import org.elasticsearch.client.indices.GetIndexRequest
import org.elasticsearch.index.query.QueryBuilders
* This class facilitates a connection to the Elasticsearch cluster and offers convenience functions to retrieve
* the necessary data.
class ElasticSearchWrapper(settings: Properties) {
private val log = LogManager.getLogger("ElasticSearchWrapper")
private val host = settings.getProperty(KEYS.SettingsProps.elasticHost)
private val port = settings.getProperty(KEYS.SettingsProps.elasticPort).toInt()
private val documentsIndex = settings.getProperty(KEYS.SettingsProps.elasticIndex)
private val client = connect()
* Establishes a connection to the client and ensures, that the index is present. The index may be an alias
* or an index directly.
* If the connection fails or the index does not exists, no client is created. When this happens all
* functions will be returning default values instead of executing the query.
* The service needs to be restarted to establish a new connection. It is reported within the logs that
* there is no successful connection.
* This makes it easier to test this service, and ensures that it keeps running even if elasticsearch is not
* available.
* TODO: In the future find a better solution.
private fun connect(): RestHighLevelClient? {
return try {
val c = RestHighLevelClient(
......@@ -26,8 +45,10 @@ class ElasticSearchWrapper(settings: Properties) {
HttpHost(host, port)
if (!c.indices().exists(GetIndexRequest(documentsIndex), RequestOptions.DEFAULT)) {
log.error("Could not find the index defined in the configuration: $documentsIndex.")
if (!c.indices().exists(GetIndexRequest(documentsIndex), RequestOptions.DEFAULT) ||
!c.indices().existsAlias(GetAliasesRequest(documentsIndex), RequestOptions.DEFAULT)) {
log.error("Could not find the index or alias defined in the configuration: $documentsIndex.")
} else {"Successfully connected to index $documentsIndex. Ready to query.")
......@@ -39,6 +60,9 @@ class ElasticSearchWrapper(settings: Properties) {
* Counts the number of documents attached to a specific record set.
fun countNumberOfDocuments(recordSetIdentifier: String): Int {
return client.let {
if (it == null) {
Supports Markdown
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