19
19
20
20
package org .elasticsearch .cluster .metadata ;
21
21
22
+ import com .google .common .collect .Maps ;
22
23
import com .google .inject .Inject ;
23
24
import org .elasticsearch .ElasticSearchException ;
24
25
import org .elasticsearch .cluster .ClusterService ;
30
31
import org .elasticsearch .cluster .routing .IndexRoutingTable ;
31
32
import org .elasticsearch .cluster .routing .RoutingTable ;
32
33
import org .elasticsearch .cluster .routing .strategy .ShardsRoutingStrategy ;
34
+ import org .elasticsearch .env .Environment ;
33
35
import org .elasticsearch .index .Index ;
34
36
import org .elasticsearch .index .mapper .DocumentMapper ;
35
37
import org .elasticsearch .index .mapper .InvalidTypeNameException ;
44
46
import org .elasticsearch .util .TimeValue ;
45
47
import org .elasticsearch .util .Tuple ;
46
48
import org .elasticsearch .util .component .AbstractComponent ;
49
+ import org .elasticsearch .util .io .Streams ;
47
50
import org .elasticsearch .util .settings .ImmutableSettings ;
48
51
import org .elasticsearch .util .settings .Settings ;
49
52
53
+ import java .io .File ;
54
+ import java .io .FileReader ;
55
+ import java .io .IOException ;
50
56
import java .util .List ;
51
57
import java .util .Map ;
52
58
import java .util .Set ;
66
72
*/
67
73
public class MetaDataService extends AbstractComponent {
68
74
75
+ private final Environment environment ;
69
76
70
77
private final ClusterService clusterService ;
71
78
@@ -79,10 +86,11 @@ public class MetaDataService extends AbstractComponent {
79
86
80
87
private final NodeMappingCreatedAction nodeMappingCreatedAction ;
81
88
82
- @ Inject public MetaDataService (Settings settings , ClusterService clusterService , IndicesService indicesService , ShardsRoutingStrategy shardsRoutingStrategy ,
89
+ @ Inject public MetaDataService (Settings settings , Environment environment , ClusterService clusterService , IndicesService indicesService , ShardsRoutingStrategy shardsRoutingStrategy ,
83
90
NodeIndexCreatedAction nodeIndexCreatedAction , NodeIndexDeletedAction nodeIndexDeletedAction ,
84
91
NodeMappingCreatedAction nodeMappingCreatedAction ) {
85
92
super (settings );
93
+ this .environment = environment ;
86
94
this .clusterService = clusterService ;
87
95
this .indicesService = indicesService ;
88
96
this .shardsRoutingStrategy = shardsRoutingStrategy ;
@@ -130,7 +138,7 @@ public synchronized IndicesAliasesResult indicesAliases(final List<AliasAction>
130
138
return new IndicesAliasesResult ();
131
139
}
132
140
133
- public synchronized CreateIndexResult createIndex (final String index , final Settings indexSettings , final Map <String , String > mappings , TimeValue timeout ) throws IndexAlreadyExistsException {
141
+ public synchronized CreateIndexResult createIndex (final String index , final Settings indexSettings , Map <String , String > mappings , TimeValue timeout ) throws IndexAlreadyExistsException {
134
142
ClusterState clusterState = clusterService .state ();
135
143
136
144
if (clusterState .routingTable ().hasIndex (index )) {
@@ -161,6 +169,30 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
161
169
throw new InvalidIndexNameException (new Index (index ), index , "an alias with the same name already exists" );
162
170
}
163
171
172
+ // add to the mappings files that exists within the config/mappings location
173
+ if (mappings == null ) {
174
+ mappings = Maps .newHashMap ();
175
+ } else {
176
+ mappings = Maps .newHashMap (mappings );
177
+ }
178
+ File mappingsDir = new File (environment .configFile (), "mappings" );
179
+ if (mappingsDir .exists () && mappingsDir .isDirectory ()) {
180
+ File [] mappingsFiles = mappingsDir .listFiles ();
181
+ for (File mappingFile : mappingsFiles ) {
182
+ String fileNameNoSuffix = mappingFile .getName ().substring (0 , mappingFile .getName ().lastIndexOf ('.' ));
183
+ if (mappings .containsKey (fileNameNoSuffix )) {
184
+ // if we have the mapping defined, ignore it
185
+ continue ;
186
+ }
187
+ try {
188
+ mappings .put (fileNameNoSuffix , Streams .copyToString (new FileReader (mappingFile )));
189
+ } catch (IOException e ) {
190
+ logger .warn ("Failed to read mapping [" + fileNameNoSuffix + "] from location [" + mappingFile + "], ignoring..." , e );
191
+ }
192
+ }
193
+ }
194
+ final Map <String , String > fMappings = mappings ;
195
+
164
196
final CountDownLatch latch = new CountDownLatch (clusterService .state ().nodes ().size ());
165
197
NodeIndexCreatedAction .Listener nodeCreatedListener = new NodeIndexCreatedAction .Listener () {
166
198
@ Override public void onNodeIndexCreated (String mIndex , String nodeId ) {
@@ -186,10 +218,8 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
186
218
Settings actualIndexSettings = indexSettingsBuilder .build ();
187
219
188
220
IndexMetaData .Builder indexMetaData = newIndexMetaDataBuilder (index ).settings (actualIndexSettings );
189
- if (mappings != null ) {
190
- for (Map .Entry <String , String > entry : mappings .entrySet ()) {
191
- indexMetaData .putMapping (entry .getKey (), entry .getValue ());
192
- }
221
+ for (Map .Entry <String , String > entry : fMappings .entrySet ()) {
222
+ indexMetaData .putMapping (entry .getKey (), entry .getValue ());
193
223
}
194
224
MetaData newMetaData = newMetaDataBuilder ()
195
225
.metaData (currentState .metaData ())
@@ -200,7 +230,7 @@ public synchronized CreateIndexResult createIndex(final String index, final Sett
200
230
.initializeEmpty (newMetaData .index (index ));
201
231
routingTableBuilder .add (indexRoutingBuilder );
202
232
203
- logger .info ("Creating Index [{}], shards [{}]/[{}]" , new Object []{index , indexMetaData .numberOfShards (), indexMetaData .numberOfReplicas ()});
233
+ logger .info ("Creating Index [{}], shards [{}]/[{}], mappings {} " , new Object []{index , indexMetaData .numberOfShards (), indexMetaData .numberOfReplicas (), fMappings . keySet ()});
204
234
RoutingTable newRoutingTable = shardsRoutingStrategy .reroute (newClusterStateBuilder ().state (currentState ).routingTable (routingTableBuilder ).metaData (newMetaData ).build ());
205
235
return newClusterStateBuilder ().state (currentState ).routingTable (newRoutingTable ).metaData (newMetaData ).build ();
206
236
}
0 commit comments