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 ...@@ -4,6 +4,7 @@ import java.util.Properties
import org.apache.http.HttpHost import org.apache.http.HttpHost
import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.LogManager
import org.elasticsearch.ElasticsearchException import org.elasticsearch.ElasticsearchException
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest
import org.elasticsearch.client.RequestOptions import org.elasticsearch.client.RequestOptions
import org.elasticsearch.client.RestClient import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient import org.elasticsearch.client.RestHighLevelClient
...@@ -11,14 +12,32 @@ import org.elasticsearch.client.core.CountRequest ...@@ -11,14 +12,32 @@ import org.elasticsearch.client.core.CountRequest
import org.elasticsearch.client.indices.GetIndexRequest import org.elasticsearch.client.indices.GetIndexRequest
import org.elasticsearch.index.query.QueryBuilders 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) { class ElasticSearchWrapper(settings: Properties) {
private val log = LogManager.getLogger("ElasticSearchWrapper") private val log = LogManager.getLogger("ElasticSearchWrapper")
private val host = settings.getProperty(KEYS.SettingsProps.elasticHost) private val host = settings.getProperty(KEYS.SettingsProps.elasticHost)
private val port = settings.getProperty(KEYS.SettingsProps.elasticPort).toInt() private val port = settings.getProperty(KEYS.SettingsProps.elasticPort).toInt()
private val documentsIndex = settings.getProperty(KEYS.SettingsProps.elasticIndex) private val documentsIndex = settings.getProperty(KEYS.SettingsProps.elasticIndex)
private val client = connect() 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? { private fun connect(): RestHighLevelClient? {
return try { return try {
val c = RestHighLevelClient( val c = RestHighLevelClient(
...@@ -26,8 +45,10 @@ class ElasticSearchWrapper(settings: Properties) { ...@@ -26,8 +45,10 @@ class ElasticSearchWrapper(settings: Properties) {
HttpHost(host, port) 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.")
null null
} else { } else {
log.info("Successfully connected to index $documentsIndex. Ready to query.") log.info("Successfully connected to index $documentsIndex. Ready to query.")
...@@ -39,6 +60,9 @@ class ElasticSearchWrapper(settings: Properties) { ...@@ -39,6 +60,9 @@ class ElasticSearchWrapper(settings: Properties) {
} }
} }
/**
* Counts the number of documents attached to a specific record set.
*/
fun countNumberOfDocuments(recordSetIdentifier: String): Int { fun countNumberOfDocuments(recordSetIdentifier: String): Int {
return client.let { return client.let {
if (it == null) { if (it == null) {
......
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