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 2.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.memobase.helpers

import java.util.Properties
import org.apache.http.HttpHost
import org.apache.logging.log4j.LogManager
import org.elasticsearch.ElasticsearchException
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

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()

    private fun connect(): RestHighLevelClient? {
        return try {
            val c = RestHighLevelClient(
                    RestClient.builder(
                            HttpHost(host, port)
                    ))

            if (!c.indices().exists(GetIndexRequest(documentsIndex), RequestOptions.DEFAULT)) {
                log.error("Could not find the index defined in the configuration: $documentsIndex.")
                null
            } else {
                log.info("Successfully connected to index $documentsIndex. Ready to query.")
                c
            }
        } catch (ex: ElasticsearchException) {
            log.error(ex.detailedMessage)
            null
        }
    }

    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
                }
            }
        }
    }
}