-
Notifications
You must be signed in to change notification settings - Fork 7
/
IndexService.java
202 lines (185 loc) · 6.58 KB
/
IndexService.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.mzz.esdemo.service;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.*;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* The type Index service.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html">elasticsearch indices
*
* @author Zero
*/
@Service
@RequiredArgsConstructor
public class IndexService {
private static final Set<String> IGNORE_SETTING_NAMES = Sets.newHashSet("index.creation_date",
"index.version.created", "index.provided_name", "index.uuid");
private final IndicesClient indicesClient;
/**
* Exists.
*
* @param indices the indices
* @return the boolean
*/
@SneakyThrows
public boolean exists(String... indices) {
return indicesClient.exists(new GetIndexRequest(indices), RequestOptions.DEFAULT);
}
/**
* Gets index.
*
* @param indices the indices
* @return the index
*/
@SneakyThrows
public GetIndexResponse getIndex(String... indices) {
return indicesClient.get(new GetIndexRequest(indices), RequestOptions.DEFAULT);
}
/**
* Create index.
*
* @param index the index
* @return the create index response
*/
@SneakyThrows
public CreateIndexResponse createIndex(String index) {
return indicesClient.create(new CreateIndexRequest(index), RequestOptions.DEFAULT);
}
/**
* Put settings.
*
* @param index the index
* @param settings the settings
* @return the acknowledged response
*/
@SneakyThrows
public AcknowledgedResponse putSettings(String index, Settings settings) {
return indicesClient.putSettings(new UpdateSettingsRequest(index)
.settings(settings), RequestOptions.DEFAULT);
}
/**
* Gets mappings.
*
* @param indices the indices
* @return the mappings
*/
@SneakyThrows
public Map<String, MappingMetadata> getMappings(String... indices) {
return indicesClient.getMapping(new GetMappingsRequest()
.indices(indices), RequestOptions.DEFAULT)
.mappings();
}
/**
* Gets single mappings.
*
* @param index the index
* @return the single mappings
*/
@SneakyThrows
public Map<String, Object> getSingleMappings(String index) {
return getMappings(index).get(index).getSourceAsMap();
}
/**
* Put mappings.
*
* @param index the index
* @param mappingSource the mapping source
* @return the acknowledged response
*/
@SneakyThrows
public AcknowledgedResponse putMappings(String index, Map<String, ?> mappingSource) {
return indicesClient.putMapping(new PutMappingRequest(index)
.source(mappingSource), RequestOptions.DEFAULT);
}
/**
* Clone mappings.
*
* @param sourceIndex the source index
* @param targetIndex the target index
* @return the acknowledged response
*/
@SneakyThrows
public AcknowledgedResponse cloneMappings(String sourceIndex, String targetIndex) {
Map<String, Object> mappings = getSingleMappings(sourceIndex);
return putMappings(targetIndex, mappings);
}
/**
* Clone index without data.
*
* @param sourceIndex the source index
* @param targetIndex the target index
* @return the create index response
*/
@SneakyThrows
public CreateIndexResponse cloneIndexWithoutData(String sourceIndex, String targetIndex) {
if (exists(targetIndex)) {
throw new ElasticsearchException("sourceIndex[{}] already existed", sourceIndex);
}
GetIndexResponse getIndexResponse = getIndex(sourceIndex);
List<AliasMetadata> aliasMetadata = getIndexResponse.getAliases().get(sourceIndex);
List<Alias> aliases = aliasMetadata.stream()
.map(AliasMetadata::getAlias)
.map(Alias::new)
.collect(Collectors.toList());
Settings settings = getIndexResponse.getSettings().get(sourceIndex);
Settings filterSettings = settings.filter(name -> !IGNORE_SETTING_NAMES.contains(name));
CreateIndexRequest createIndexRequest = new CreateIndexRequest(targetIndex)
.aliases(aliases)
.settings(filterSettings)
.mapping(getIndexResponse.getMappings().get(sourceIndex).sourceAsMap());
return indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
}
/**
* Clone index.
* To clone an index, the index must be marked as read-only and have a cluster health status of green.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clone-index.html">
*
* @param sourceIndex the source index
* @param targetIndex the target index
* @return the resize response
*/
@SneakyThrows
public ResizeResponse cloneIndex(String sourceIndex, String targetIndex) {
return indicesClient.clone(new ResizeRequest(targetIndex, sourceIndex), RequestOptions.DEFAULT);
}
/**
* Delete index.
*
* @param index the index
* @return the acknowledged response
*/
@SneakyThrows
public AcknowledgedResponse deleteIndex(String index) {
if (!exists(index)) {
return new AcknowledgedResponse(true);
}
return indicesClient.delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
}
/**
* Refresh.
*
* @param index the index
* @return the refresh response
*/
@SneakyThrows
public RefreshResponse refresh(String index) {
return indicesClient.refresh(new RefreshRequest(index), RequestOptions.DEFAULT);
}
}