6
6
package org .elasticsearch .xpack .core .ml .annotations ;
7
7
8
8
import org .elasticsearch .ResourceAlreadyExistsException ;
9
+ import org .elasticsearch .Version ;
9
10
import org .elasticsearch .action .ActionListener ;
10
11
import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
11
12
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
15
16
import org .elasticsearch .cluster .ClusterState ;
16
17
import org .elasticsearch .cluster .metadata .AliasOrIndex ;
17
18
import org .elasticsearch .cluster .metadata .IndexMetaData ;
18
- import org .elasticsearch .cluster .routing .UnassignedInfo ;
19
19
import org .elasticsearch .common .settings .Settings ;
20
- import org .elasticsearch .common .unit .TimeValue ;
21
- import org .elasticsearch .common .xcontent .XContentBuilder ;
22
- import org .elasticsearch .xpack .core .ml .MachineLearningField ;
23
- import org .elasticsearch .xpack .core .ml .job .config .Job ;
24
- import org .elasticsearch .xpack .core .ml .job .persistence .ElasticsearchMappings ;
20
+ import org .elasticsearch .common .xcontent .XContentType ;
25
21
import org .elasticsearch .xpack .core .ml .utils .ExceptionsHelper ;
22
+ import org .elasticsearch .xpack .core .template .TemplateUtils ;
26
23
27
- import java .io .IOException ;
28
24
import java .util .SortedMap ;
29
25
30
- import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
31
26
import static org .elasticsearch .index .mapper .MapperService .SINGLE_MAPPING_NAME ;
32
27
import static org .elasticsearch .xpack .core .ClientHelper .ML_ORIGIN ;
33
28
import static org .elasticsearch .xpack .core .ClientHelper .executeAsyncWithOrigin ;
@@ -40,6 +35,8 @@ public class AnnotationIndex {
40
35
public static final String INDEX_NAME = ".ml-annotations-6" ;
41
36
public static final String INDEX_PATTERN = ".ml-annotations*" ;
42
37
38
+ private static final String MAPPINGS_VERSION_VARIABLE = "xpack.ml.version" ;
39
+
43
40
/**
44
41
* Create the .ml-annotations index with correct mappings if it does not already
45
42
* exist. This index is read and written by the UI results views, so needs to
@@ -64,39 +61,26 @@ public static void createAnnotationsIndexIfNecessary(Settings settings, Client c
64
61
// Create the annotations index if it doesn't exist already.
65
62
if (mlLookup .containsKey (INDEX_NAME ) == false ) {
66
63
67
- final TimeValue delayedNodeTimeOutSetting ;
68
- // Whether we are using native process is a good way to detect whether we are in dev / test mode:
69
- if (MachineLearningField .AUTODETECT_PROCESS .get (settings )) {
70
- delayedNodeTimeOutSetting = UnassignedInfo .INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING .get (settings );
71
- } else {
72
- delayedNodeTimeOutSetting = TimeValue .ZERO ;
73
- }
74
-
75
64
CreateIndexRequest createIndexRequest = new CreateIndexRequest (INDEX_NAME );
76
- try (XContentBuilder annotationsMapping = AnnotationIndex .annotationsMapping ()) {
77
- createIndexRequest .mapping (SINGLE_MAPPING_NAME , annotationsMapping );
78
- createIndexRequest .settings (Settings .builder ()
79
- .put (IndexMetaData .SETTING_AUTO_EXPAND_REPLICAS , "0-1" )
80
- .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , "1" )
81
- .put (UnassignedInfo .INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING .getKey (), delayedNodeTimeOutSetting ));
65
+ createIndexRequest .mapping (SINGLE_MAPPING_NAME , annotationsMapping (), XContentType .JSON );
66
+ createIndexRequest .settings (Settings .builder ()
67
+ .put (IndexMetaData .SETTING_AUTO_EXPAND_REPLICAS , "0-1" )
68
+ .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , "1" ));
82
69
83
- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), ML_ORIGIN , createIndexRequest ,
84
- ActionListener .<CreateIndexResponse >wrap (
85
- r -> createAliasListener .onResponse (r .isAcknowledged ()),
86
- e -> {
87
- // Possible that the index was created while the request was executing,
88
- // so we need to handle that possibility
89
- if (ExceptionsHelper .unwrapCause (e ) instanceof ResourceAlreadyExistsException ) {
90
- // Create the alias
91
- createAliasListener .onResponse (true );
92
- } else {
93
- finalListener .onFailure (e );
94
- }
70
+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), ML_ORIGIN , createIndexRequest ,
71
+ ActionListener .<CreateIndexResponse >wrap (
72
+ r -> createAliasListener .onResponse (r .isAcknowledged ()),
73
+ e -> {
74
+ // Possible that the index was created while the request was executing,
75
+ // so we need to handle that possibility
76
+ if (ExceptionsHelper .unwrapCause (e ) instanceof ResourceAlreadyExistsException ) {
77
+ // Create the alias
78
+ createAliasListener .onResponse (true );
79
+ } else {
80
+ finalListener .onFailure (e );
95
81
}
96
- ), client .admin ().indices ()::create );
97
- } catch (IOException e ) {
98
- finalListener .onFailure (e );
99
- }
82
+ }
83
+ ), client .admin ().indices ()::create );
100
84
return ;
101
85
}
102
86
@@ -111,42 +95,8 @@ public static void createAnnotationsIndexIfNecessary(Settings settings, Client c
111
95
finalListener .onResponse (false );
112
96
}
113
97
114
- public static XContentBuilder annotationsMapping () throws IOException {
115
- XContentBuilder builder = jsonBuilder ()
116
- .startObject ()
117
- .startObject (SINGLE_MAPPING_NAME );
118
- ElasticsearchMappings .addMetaInformation (builder );
119
- builder .startObject (ElasticsearchMappings .PROPERTIES )
120
- .startObject (Annotation .ANNOTATION .getPreferredName ())
121
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .TEXT )
122
- .endObject ()
123
- .startObject (Annotation .CREATE_TIME .getPreferredName ())
124
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .DATE )
125
- .endObject ()
126
- .startObject (Annotation .CREATE_USERNAME .getPreferredName ())
127
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .KEYWORD )
128
- .endObject ()
129
- .startObject (Annotation .TIMESTAMP .getPreferredName ())
130
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .DATE )
131
- .endObject ()
132
- .startObject (Annotation .END_TIMESTAMP .getPreferredName ())
133
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .DATE )
134
- .endObject ()
135
- .startObject (Job .ID .getPreferredName ())
136
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .KEYWORD )
137
- .endObject ()
138
- .startObject (Annotation .MODIFIED_TIME .getPreferredName ())
139
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .DATE )
140
- .endObject ()
141
- .startObject (Annotation .MODIFIED_USERNAME .getPreferredName ())
142
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .KEYWORD )
143
- .endObject ()
144
- .startObject (Annotation .TYPE .getPreferredName ())
145
- .field (ElasticsearchMappings .TYPE , ElasticsearchMappings .KEYWORD )
146
- .endObject ()
147
- .endObject ()
148
- .endObject ()
149
- .endObject ();
150
- return builder ;
98
+ public static String annotationsMapping () {
99
+ return TemplateUtils .loadTemplate ("/org/elasticsearch/xpack/core/ml/annotations_index_mappings.json" ,
100
+ Version .CURRENT .toString (), MAPPINGS_VERSION_VARIABLE );
151
101
}
152
102
}
0 commit comments