@@ -10,37 +10,34 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
10
10
import org.elasticsearch.action.bulk.BulkRequest
11
11
import org.elasticsearch.action.bulk.BulkResponse
12
12
import org.elasticsearch.action.index.IndexRequest
13
- import org.elasticsearch.client.ResponseException
14
- import org.elasticsearch.client.RestClientBuilder
15
- import org.elasticsearch.client.RestHighLevelClient
13
+ import org.elasticsearch.client.*
16
14
import org.elasticsearch.common.xcontent.XContentType
17
15
import org.slf4j.LoggerFactory
18
16
import org.springframework.beans.factory.annotation.Autowired
19
17
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
20
18
import org.springframework.stereotype.Service
21
19
import java.io.IOException
22
- import kotlin.streams .toList
20
+ import java.util.stream.Collectors .toList
23
21
24
22
/* *
25
23
* Elasticsearch client implementation.
26
24
* <br></br><br></br>
27
25
* Note that in cases where parameters are used as part of an index name, the value(s) are converted to lower case before being used.
28
26
*/
29
- @ConditionalOnProperty(prefix = " elasticsearch" , name = [" usemock" ], havingValue = " false" , matchIfMissing = true )
30
27
@Service
31
28
class ElasticsearchIndexClient @Autowired
32
- constructor (client : RestClientBuilder ,
33
- private val objectMapper: ObjectMapper ) : RestHighLevelClient (client ) {
29
+ constructor (elasticClientBuilder : RestClientBuilder ,
30
+ private val objectMapper: ObjectMapper ) : RestHighLevelClient (elasticClientBuilder ) {
34
31
35
32
val isHealthy: Boolean
36
33
@Throws(IOException ::class )
37
- get() = super .ping()
34
+ get() = super .ping(RequestOptions . DEFAULT )
38
35
39
36
@Throws(IOException ::class )
40
37
fun createIndex (index : String , settings : String ) {
41
38
42
39
val lowerCaseIndex = index.toLowerCase()
43
- indices().create(CreateIndexRequest (lowerCaseIndex).source(settings, XContentType .JSON ))
40
+ indices().create(CreateIndexRequest (lowerCaseIndex).source(settings, XContentType .JSON ), RequestOptions . DEFAULT )
44
41
45
42
}
46
43
@@ -50,7 +47,7 @@ constructor(client: RestClientBuilder,
50
47
val lowerCaseIndices = indices.map { it.toLowerCase() }.toTypedArray()
51
48
52
49
if (lowerCaseIndices.isNotEmpty()) {
53
- indices().delete(DeleteIndexRequest (* lowerCaseIndices))
50
+ indices().delete(DeleteIndexRequest (* lowerCaseIndices), RequestOptions . DEFAULT )
54
51
}
55
52
}
56
53
@@ -59,7 +56,7 @@ constructor(client: RestClientBuilder,
59
56
60
57
val lowerCaseIndex = index.toLowerCase()
61
58
try {
62
- lowLevelClient.performRequest(" GET" , " /$lowerCaseIndex " )
59
+ lowLevelClient.performRequest(Request ( " GET" , " /$lowerCaseIndex " ) )
63
60
return true
64
61
} catch (e: ResponseException ) {
65
62
LOG .debug(" Exception while calling indexExists" + e.message)
@@ -79,12 +76,10 @@ constructor(client: RestClientBuilder,
79
76
" { \" add\" : { \" index\" : \" " + lowerCaseAlias + indexDatestamp + " \" , \" alias\" : \" " + lowerCaseAlias + " \" } }\n " +
80
77
" ]\n " +
81
78
" }"
82
- lowLevelClient.performRequest(
83
- " POST" ,
84
- " /_aliases" ,
85
- emptyMap(),
86
- NStringEntity (jsonString, ContentType .APPLICATION_JSON )
87
- )
79
+
80
+ lowLevelClient.performRequest(Request (" POST" , " /_aliases" ).apply {
81
+ this .entity = NStringEntity (jsonString, ContentType .APPLICATION_JSON )
82
+ })
88
83
89
84
}
90
85
@@ -106,8 +101,8 @@ constructor(client: RestClientBuilder,
106
101
fun fetchIndexDocCount (index : String ): Int {
107
102
108
103
val lowerCaseIndex = index.toLowerCase()
109
- lowLevelClient.performRequest(" POST" , " /$lowerCaseIndex /_refresh" )
110
- val response = lowLevelClient.performRequest(" GET" , " /_cat/indices/$lowerCaseIndex " )
104
+ lowLevelClient.performRequest(Request ( " POST" , " /$lowerCaseIndex /_refresh" ) )
105
+ val response = lowLevelClient.performRequest(Request ( " GET" , " /_cat/indices/$lowerCaseIndex " ) )
111
106
val line = EntityUtils .toString(response.entity)
112
107
return Integer .parseInt(line.split(" " .toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[6 ])
113
108
@@ -117,15 +112,15 @@ constructor(client: RestClientBuilder,
117
112
fun fetchAllIndicesStartingWith (name : String ): List <String > {
118
113
119
114
val lowerCaseName = name.toLowerCase()
120
- val response = lowLevelClient.performRequest(" GET" , " /_cat/indices/$lowerCaseName *" )
115
+ val response = lowLevelClient.performRequest(Request ( " GET" , " /_cat/indices/$lowerCaseName *" ) )
121
116
122
117
return response.entity.content.bufferedReader().lines().filter {
123
118
! it.trim().isEmpty()
124
119
}.map {
125
120
it.split(" \\ s+" .toRegex())[2 ]
126
121
}.filter {
127
122
it.startsWith(lowerCaseName) // Extra sanity check
128
- }.toList()
123
+ }.collect( toList() )
129
124
}
130
125
131
126
companion object {
0 commit comments