In order to mitigate against the brute force attacks against Gitlab accounts, we are moving to all edu-ID Logins. We would like to remind you to link your account with your edu-id. Login will be possible only by edu-ID after November 30, 2021. Here you can find the instructions for linking your account.

If you don't have a SWITCH edu-ID, you can create one with this guide here

kind regards

ElasticSearchWrapper.kt 3.51 KB
Newer Older
1
2
3
4
5
6
package org.memobase.helpers

import java.util.Properties
import org.apache.http.HttpHost
import org.apache.logging.log4j.LogManager
import org.elasticsearch.ElasticsearchException
Jonas Waeber's avatar
Jonas Waeber committed
7
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest
8
9
10
11
12
13
14
import org.elasticsearch.client.RequestOptions
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient
import org.elasticsearch.client.core.CountRequest
import org.elasticsearch.client.indices.GetIndexRequest
import org.elasticsearch.index.query.QueryBuilders

Jonas Waeber's avatar
Jonas Waeber committed
15
16
17
18
/**
 * This class facilitates a connection to the Elasticsearch cluster and offers convenience functions to retrieve
 * the necessary data.
 */
19
20
21
22
23
24
25
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()

Jonas Waeber's avatar
Jonas Waeber committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    /**
     * 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.
     */
41
42
43
44
45
46
47
    private fun connect(): RestHighLevelClient? {
        return try {
            val c = RestHighLevelClient(
                    RestClient.builder(
                            HttpHost(host, port)
                    ))

Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
51

            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.")
52
53
54
55
56
57
58
59
60
61
62
                null
            } else {
                log.info("Successfully connected to index $documentsIndex. Ready to query.")
                c
            }
        } catch (ex: ElasticsearchException) {
            log.error(ex.detailedMessage)
            null
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
63
64
65
    /**
     * Counts the number of documents attached to a specific record set.
     */
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    fun countNumberOfDocuments(recordSetIdentifier: String): Int {
        return client.let {
            if (it == null) {
                log.error("Not connected to an index. Count is at zero! Restart service to retry connection.")
                0
            } else {
                val request = CountRequest(documentsIndex)
                request.query(
                        QueryBuilders.termQuery(
                                "recordSet.facet", recordSetIdentifier
                        )
                )
                try {
                    val response = it.count(request, RequestOptions.DEFAULT
                    )
                    response.count.toInt()
                } catch (ex: ElasticsearchException) {
                    log.error(ex.detailedMessage)
                    0
                }
            }
        }
    }
}