18
18
import static org .springframework .util .StringUtils .*;
19
19
20
20
import java .util .HashMap ;
21
+ import java .util .List ;
21
22
import java .util .Map ;
22
23
23
24
import org .elasticsearch .action .admin .indices .settings .get .GetSettingsResponse ;
25
+ import org .elasticsearch .cluster .metadata .AliasMetaData ;
24
26
import org .elasticsearch .common .collect .MapBuilder ;
25
27
import org .elasticsearch .common .settings .Settings ;
26
28
import org .slf4j .Logger ;
27
29
import org .slf4j .LoggerFactory ;
30
+ import org .springframework .dao .InvalidDataAccessApiUsageException ;
28
31
import org .springframework .data .elasticsearch .ElasticsearchException ;
29
32
import org .springframework .data .elasticsearch .annotations .Mapping ;
30
33
import org .springframework .data .elasticsearch .annotations .Setting ;
31
34
import org .springframework .data .elasticsearch .core .convert .ElasticsearchConverter ;
35
+ import org .springframework .data .elasticsearch .core .document .Document ;
32
36
import org .springframework .data .elasticsearch .core .index .MappingBuilder ;
33
37
import org .springframework .data .elasticsearch .core .mapping .ElasticsearchPersistentEntity ;
34
38
import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
39
+ import org .springframework .data .elasticsearch .core .query .AliasQuery ;
40
+ import org .springframework .lang .Nullable ;
35
41
import org .springframework .util .StringUtils ;
36
42
37
43
/**
@@ -48,88 +54,145 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
48
54
protected final ElasticsearchConverter elasticsearchConverter ;
49
55
protected final RequestFactory requestFactory ;
50
56
51
- public AbstractDefaultIndexOperations (ElasticsearchConverter elasticsearchConverter ) {
57
+ @ Nullable protected final Class <?> boundClass ;
58
+ protected final IndexCoordinates boundIndex ;
59
+
60
+ public AbstractDefaultIndexOperations (ElasticsearchConverter elasticsearchConverter , Class <?> boundClass ) {
52
61
this .elasticsearchConverter = elasticsearchConverter ;
53
62
requestFactory = new RequestFactory (elasticsearchConverter );
63
+
64
+ this .boundClass = boundClass ;
65
+ this .boundIndex = getIndexCoordinatesFor (boundClass );
54
66
}
55
67
56
- // region IndexOperations
57
- @ Override
58
- public boolean createIndex (String indexName ) {
59
- return createIndex (indexName , null );
68
+ public AbstractDefaultIndexOperations (ElasticsearchConverter elasticsearchConverter , IndexCoordinates boundIndex ) {
69
+ this .elasticsearchConverter = elasticsearchConverter ;
70
+ requestFactory = new RequestFactory (elasticsearchConverter );
71
+
72
+ this .boundClass = null ;
73
+ this .boundIndex = boundIndex ;
60
74
}
61
75
76
+ protected Class <?> checkForBoundClass () {
77
+ if (boundClass == null ) {
78
+ throw new InvalidDataAccessApiUsageException ("IndexOperations are not bound" );
79
+ }
80
+ return boundClass ;
81
+ }
82
+
83
+ // region IndexOperations
84
+
62
85
@ Override
63
- public boolean createIndex (Class <?> clazz ) {
86
+ public boolean create () {
87
+
88
+ if (boundClass != null ) {
89
+ Class <?> clazz = boundClass ;
90
+ String indexName = boundIndex .getIndexName ();
64
91
65
- String indexName = getRequiredPersistentEntity (clazz ).getIndexCoordinates ().getIndexName ();
66
- if (clazz .isAnnotationPresent (Setting .class )) {
67
- String settingPath = clazz .getAnnotation (Setting .class ).settingPath ();
92
+ if (clazz .isAnnotationPresent (Setting .class )) {
93
+ String settingPath = clazz .getAnnotation (Setting .class ).settingPath ();
68
94
69
- if (hasText (settingPath )) {
70
- String settings = ResourceUtil .readFileFromClasspath (settingPath );
95
+ if (hasText (settingPath )) {
96
+ String settings = ResourceUtil .readFileFromClasspath (settingPath );
71
97
72
- if (hasText (settings )) {
73
- return createIndex (indexName , settings );
98
+ if (hasText (settings )) {
99
+ return doCreate (indexName , Document .parse (settings ));
100
+ }
101
+ } else {
102
+ LOGGER .info ("settingPath in @Setting has to be defined. Using default instead." );
74
103
}
75
- } else {
76
- LOGGER .info ("settingPath in @Setting has to be defined. Using default instead." );
77
104
}
105
+ return doCreate (indexName , getDefaultSettings (getRequiredPersistentEntity (clazz )));
78
106
}
79
- return createIndex (indexName , getDefaultSettings (getRequiredPersistentEntity (clazz )));
107
+ return doCreate (boundIndex .getIndexName (), null );
108
+ }
109
+
110
+ @ Override
111
+ public boolean create (Document settings ) {
112
+ return doCreate (boundIndex .getIndexName (), settings );
80
113
}
81
114
115
+ protected abstract boolean doCreate (String indexName , @ Nullable Document settings );
116
+
82
117
@ Override
83
- public boolean createIndex ( Class <?> clazz , Object settings ) {
84
- return createIndex ( getRequiredPersistentEntity ( clazz ). getIndexCoordinates (). getIndexName (), settings );
118
+ public boolean delete ( ) {
119
+ return doDelete ( boundIndex . getIndexName ());
85
120
}
86
121
122
+ protected abstract boolean doDelete (String indexName );
123
+
87
124
@ Override
88
- public boolean deleteIndex ( Class <?> clazz ) {
89
- return deleteIndex ( getRequiredPersistentEntity ( clazz ). getIndexCoordinates () .getIndexName ());
125
+ public boolean exists ( ) {
126
+ return doExists ( boundIndex .getIndexName ());
90
127
}
91
128
129
+ protected abstract boolean doExists (String indexName );
130
+
92
131
@ Override
93
- public boolean indexExists ( Class <?> clazz ) {
94
- return indexExists ( getIndexCoordinatesFor ( clazz ). getIndexName () );
132
+ public boolean putMapping ( Document mapping ) {
133
+ return doPutMapping ( boundIndex , mapping );
95
134
}
96
135
136
+ protected abstract boolean doPutMapping (IndexCoordinates index , Document mapping );
137
+
97
138
@ Override
98
- public Map <String , Object > getMapping (Class <?> clazz ) {
99
- return getMapping ( getIndexCoordinatesFor ( clazz ) );
139
+ public Map <String , Object > getMapping () {
140
+ return doGetMapping ( boundIndex );
100
141
}
101
142
143
+ abstract protected Map <String , Object > doGetMapping (IndexCoordinates index );
144
+
102
145
@ Override
103
- public boolean putMapping ( Class <?> clazz ) {
104
- return putMapping ( clazz , buildMapping ( clazz ) );
146
+ public Map < String , Object > getSettings ( ) {
147
+ return getSettings ( false );
105
148
}
106
149
107
150
@ Override
108
- public < T > boolean putMapping ( Class < T > clazz , Object mapping ) {
109
- return putMapping ( getIndexCoordinatesFor ( clazz ), mapping );
151
+ public Map < String , Object > getSettings ( boolean includeDefaults ) {
152
+ return doGetSettings ( boundIndex . getIndexName ( ), includeDefaults );
110
153
}
111
154
155
+ protected abstract Map <String , Object > doGetSettings (String indexName , boolean includeDefaults );
156
+
112
157
@ Override
113
- public boolean putMapping ( IndexCoordinates index , Class <?> clazz ) {
114
- return putMapping ( index , buildMapping ( clazz ) );
158
+ public void refresh ( ) {
159
+ doRefresh ( boundIndex );
115
160
}
116
161
162
+ protected abstract void doRefresh (IndexCoordinates indexCoordinates );
163
+
117
164
@ Override
118
- public Map < String , Object > getSettings ( Class <?> clazz ) {
119
- return getSettings ( clazz , false );
165
+ public boolean addAlias ( AliasQuery query ) {
166
+ return doAddAlias ( query , boundIndex );
120
167
}
121
168
169
+ protected abstract boolean doAddAlias (AliasQuery query , IndexCoordinates index );
170
+
122
171
@ Override
123
- public Map < String , Object > getSettings ( Class <?> clazz , boolean includeDefaults ) {
124
- return getSettings ( getRequiredPersistentEntity ( clazz ). getIndexCoordinates (). getIndexName (), includeDefaults );
172
+ public List < AliasMetaData > queryForAlias ( ) {
173
+ return doQueryForAlias ( boundIndex . getIndexName ());
125
174
}
126
175
176
+ protected abstract List <AliasMetaData > doQueryForAlias (String indexName );
177
+
127
178
@ Override
128
- public void refresh ( Class <?> clazz ) {
129
- refresh ( getIndexCoordinatesFor ( clazz ) );
179
+ public boolean removeAlias ( AliasQuery query ) {
180
+ return doRemoveAlias ( query , boundIndex );
130
181
}
131
182
132
- protected String buildMapping (Class <?> clazz ) {
183
+ protected abstract boolean doRemoveAlias (AliasQuery query , IndexCoordinates index );
184
+
185
+ @ Override
186
+ public Document createMapping () {
187
+ return createMapping (checkForBoundClass ());
188
+ }
189
+
190
+ @ Override
191
+ public Document createMapping (Class <?> clazz ) {
192
+ return buildMapping (clazz );
193
+ }
194
+
195
+ protected Document buildMapping (Class <?> clazz ) {
133
196
134
197
// load mapping specified in Mapping annotation if present
135
198
if (clazz .isAnnotationPresent (Mapping .class )) {
@@ -139,7 +202,7 @@ protected String buildMapping(Class<?> clazz) {
139
202
String mappings = ResourceUtil .readFileFromClasspath (mappingPath );
140
203
141
204
if (!StringUtils .isEmpty (mappings )) {
142
- return mappings ;
205
+ return Document . parse ( mappings ) ;
143
206
}
144
207
} else {
145
208
LOGGER .info ("mappingPath in @Mapping has to be defined. Building mappings using @Field" );
@@ -148,23 +211,27 @@ protected String buildMapping(Class<?> clazz) {
148
211
149
212
// build mapping from field annotations
150
213
try {
151
- return new MappingBuilder (elasticsearchConverter ).buildPropertyMapping (clazz );
214
+ String mapping = new MappingBuilder (elasticsearchConverter ).buildPropertyMapping (clazz );
215
+ return Document .parse (mapping );
152
216
} catch (Exception e ) {
153
217
throw new ElasticsearchException ("Failed to build mapping for " + clazz .getSimpleName (), e );
154
218
}
155
219
}
156
220
// endregion
157
221
158
222
// region Helper functions
159
- private <T > Map getDefaultSettings (ElasticsearchPersistentEntity <T > persistentEntity ) {
223
+ private <T > Document getDefaultSettings (ElasticsearchPersistentEntity <T > persistentEntity ) {
160
224
161
- if (persistentEntity .isUseServerConfiguration ())
162
- return new HashMap ();
225
+ if (persistentEntity .isUseServerConfiguration ()) {
226
+ return Document .create ();
227
+ }
163
228
164
- return new MapBuilder <String , String >().put ("index.number_of_shards" , String .valueOf (persistentEntity .getShards ()))
229
+ Map <String , String > map = new MapBuilder <String , String >()
230
+ .put ("index.number_of_shards" , String .valueOf (persistentEntity .getShards ()))
165
231
.put ("index.number_of_replicas" , String .valueOf (persistentEntity .getReplicas ()))
166
232
.put ("index.refresh_interval" , persistentEntity .getRefreshInterval ())
167
233
.put ("index.store.type" , persistentEntity .getIndexStoreType ()).map ();
234
+ return Document .from (map );
168
235
}
169
236
170
237
ElasticsearchPersistentEntity <?> getRequiredPersistentEntity (Class <?> clazz ) {
0 commit comments