Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
memoriav
Memobase 2020
services
Elasticsearch Services
Search Doc Service
Commits
8e6ce5bb
Commit
8e6ce5bb
authored
Nov 23, 2020
by
Jonas Waeber
Browse files
Add some documentation
parent
2cf53d1e
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/kotlin/helpers/ElasticSearchWrapper.kt
View file @
8e6ce5bb
...
...
@@ -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."
)
null
}
else
{
log
.
info
(
"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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment