1414 * #L%
1515 */
1616
17+ import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .util .concurrent .atomic .AtomicBoolean ;
20+ import java .util .logging .Level ;
1721import java .util .logging .Logger ;
1822
1923import javax .servlet .ServletContextEvent ;
2024import javax .servlet .ServletContextListener ;
2125
26+ import co .elastic .clients .elasticsearch .indices .CreateIndexRequest ;
27+ import co .elastic .clients .elasticsearch .indices .CreateIndexResponse ;
28+ import co .elastic .clients .elasticsearch .indices .ExistsRequest ;
29+ import co .elastic .clients .transport .endpoints .BooleanResponse ;
2230import org .apache .http .HttpHost ;
2331import org .elasticsearch .client .RestClient ;
2432import org .springframework .beans .factory .annotation .Value ;
@@ -46,27 +54,23 @@ public class ElasticConfig implements ServletContextListener {
4654
4755 private ElasticsearchClient searchClient ;
4856 private ElasticsearchClient indexClient ;
57+ private static final AtomicBoolean esInitialized = new AtomicBoolean ();
4958
5059 @ Value ("${elasticsearch.cluster.name:elasticsearch}" )
5160 private String clusterName ;
5261 @ Value ("${elasticsearch.network.host:localhost}" )
5362 private String host ;
5463 @ Value ("${elasticsearch.http.port:9200}" )
5564 private int port ;
65+ @ Value ("${elasticsearch.create.indices:true}" )
66+ private String createIndices ;
5667
5768 @ Value ("${elasticsearch.tag.index:cf_tags}" )
5869 private String ES_TAG_INDEX ;
59- @ Value ("${elasticsearch.tag.type:cf_tag}" )
60- private String ES_TAG_TYPE ;
6170 @ Value ("${elasticsearch.property.index:cf_properties}" )
6271 private String ES_PROPERTY_INDEX ;
63- @ Value ("${elasticsearch.property.type:cf_property}" )
64- private String ES_PROPERTY_TYPE ;
6572 @ Value ("${elasticsearch.channel.index:channelfinder}" )
6673 private String ES_CHANNEL_INDEX ;
67- @ Value ("${elasticsearch.channel.type:cf_channel}" )
68- private String ES_CHANNEL_TYPE ;
69-
7074 @ Value ("${elasticsearch.query.size}" )
7175 private String ES_QUERY_SIZE ;
7276
@@ -81,6 +85,10 @@ public ElasticsearchClient getSearchClient() {
8185
8286 searchClient = new ElasticsearchClient (transport );
8387 }
88+ esInitialized .set (!Boolean .parseBoolean (createIndices ));
89+ if (esInitialized .compareAndSet (false , true )) {
90+ elasticIndexValidation (searchClient );
91+ }
8492 return searchClient ;
8593 }
8694
@@ -94,6 +102,10 @@ public ElasticsearchClient getIndexClient() {
94102 ElasticsearchTransport transport = new RestClientTransport (httpClient , new JacksonJsonpMapper ());
95103 indexClient = new ElasticsearchClient (transport );
96104 }
105+ esInitialized .set (!Boolean .parseBoolean (createIndices ));
106+ if (esInitialized .compareAndSet (false , true )) {
107+ elasticIndexValidation (indexClient );
108+ }
97109 return indexClient ;
98110 }
99111
@@ -111,4 +123,52 @@ public void contextDestroyed(ServletContextEvent sce) {
111123 indexClient .shutdown ();
112124 }
113125
126+
127+ /**
128+ * Create the olog indices and templates if they don't exist
129+ * @param client
130+ */
131+ void elasticIndexValidation (ElasticsearchClient client ) {
132+ // ChannelFinder Index
133+ try (InputStream is = ElasticConfig .class .getResourceAsStream ("/channel_mapping.json" )) {
134+ BooleanResponse exits = client .indices ().exists (ExistsRequest .of (e -> e .index (ES_CHANNEL_INDEX )));
135+ if (!exits .value ()) {
136+
137+ CreateIndexResponse result = client .indices ().create (
138+ CreateIndexRequest .of (
139+ c -> c .index (ES_CHANNEL_INDEX ).withJson (is )));
140+ log .info ("Created index: " + ES_CHANNEL_INDEX + " : acknowledged " + result .acknowledged ());
141+ }
142+ } catch (IOException e ) {
143+ log .log (Level .WARNING , "Failed to create index " + ES_CHANNEL_INDEX , e );
144+ }
145+
146+ // ChannelFinder tag Index
147+ try (InputStream is = ElasticConfig .class .getResourceAsStream ("/tag_mapping.json" )) {
148+ BooleanResponse exits = client .indices ().exists (ExistsRequest .of (e -> e .index (ES_TAG_INDEX )));
149+ if (!exits .value ()) {
150+
151+ CreateIndexResponse result = client .indices ().create (
152+ CreateIndexRequest .of (
153+ c -> c .index (ES_TAG_INDEX ).withJson (is )));
154+ log .info ("Created index: " + ES_TAG_INDEX + " : acknowledged " + result .acknowledged ());
155+ }
156+ } catch (IOException e ) {
157+ log .log (Level .WARNING , "Failed to create index " + ES_TAG_INDEX , e );
158+ }
159+
160+ // ChannelFinder property Index
161+ try (InputStream is = ElasticConfig .class .getResourceAsStream ("/properties_mapping.json" )) {
162+ BooleanResponse exits = client .indices ().exists (ExistsRequest .of (e -> e .index (ES_PROPERTY_INDEX )));
163+ if (!exits .value ()) {
164+
165+ CreateIndexResponse result = client .indices ().create (
166+ CreateIndexRequest .of (
167+ c -> c .index (ES_PROPERTY_INDEX ).withJson (is )));
168+ log .info ("Created index: " + ES_PROPERTY_INDEX + " : acknowledged " + result .acknowledged ());
169+ }
170+ } catch (IOException e ) {
171+ log .log (Level .WARNING , "Failed to create index " + ES_PROPERTY_INDEX , e );
172+ }
173+ }
114174}
0 commit comments