/** * Initialize the index in the Elasticsearch cluster with the current settings. */ @PostConstruct public void initializeIndex() { if (!this.client.admin().indices().prepareExists(index).get().isExists()) { this.client.admin().indices().prepareCreate(index).setSource(settings).get(); } }
private void createIndex(final String type, final String indexName) { // 加载配置文件 final String indexConfig = indexConfigLoader.load(type); // 创建索引 try { client.admin().indices().prepareCreate(indexName).setSource(indexConfig).execute().actionGet(); } catch (IndexAlreadyExistsException ex) { log.error("conflict while create index: {}, it's not serious.", indexName); } } }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no specific settings * @throws Exception if the elasticsearch API call is failing */ @Deprecated private static void createIndexWithSettingsInElasticsearch(Client client, String index, String settings) throws Exception { logger.trace("createIndex([{}])", index); assert client != null; assert index != null; CreateIndexRequestBuilder cirb = client.admin().indices().prepareCreate(index); // If there are settings for this index, we use it. If not, using Elasticsearch defaults. if (settings != null) { logger.trace("Found settings for index [{}]: [{}]", index, settings); cirb.setSource(settings, XContentType.JSON); } CreateIndexResponse createIndexResponse = cirb.execute().actionGet(); if (!createIndexResponse.isAcknowledged()) { logger.warn("Could not create index [{}]", index); throw new Exception("Could not create index ["+index+"]."); } logger.trace("/createIndex([{}])", index); }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no specific settings * @throws Exception if the elasticsearch API call is failing */ @Deprecated private static void createIndexWithSettingsInElasticsearch(Client client, String index, String settings) throws Exception { logger.trace("createIndex([{}])", index); assert client != null; assert index != null; CreateIndexRequestBuilder cirb = client.admin().indices().prepareCreate(index); // If there are settings for this index, we use it. If not, using Elasticsearch defaults. if (settings != null) { logger.trace("Found settings for index [{}]: [{}]", index, settings); cirb.setSource(settings, XContentType.JSON); } CreateIndexResponse createIndexResponse = cirb.execute().actionGet(); if (!createIndexResponse.isAcknowledged()) { logger.warn("Could not create index [{}]", index); throw new Exception("Could not create index ["+index+"]."); } logger.trace("/createIndex([{}])", index); }
@VisibleForTesting protected String createIndex() { final String indexName = this.configuration.getAlias() + "_" + System.currentTimeMillis(); CreateIndexRequestBuilder prepareCreate = this.client.admin().indices() .prepareCreate(indexName); if (this.configuration.getSource() != null) { try { prepareCreate .setSource(ByteStreams.toByteArray(this.configuration.getSource().getInputStream())); } catch (IOException e) { logger.error("Can't read index {} configuration file {}", indexName, this.configuration.getSource(), e); } } CreateIndexResponse createIndexResponse = prepareCreate.get(); if (!createIndexResponse.isAcknowledged()) { throw new IllegalStateException("Error creating index for Lotus BO"); } return indexName; }
.setSource(configuration) .execute().actionGet(); } catch (Exception e) {
.setSource(source) .execute() .actionGet();
.indices() .prepareCreate(index) .setSource(json) .execute() .actionGet();
public static void createIndexELServer( String index, String jsonMappingsFile ) throws Exception { // Obtain data to configure & populate the server. String mappingsContent = getFileAsString(jsonMappingsFile); CreateIndexRequest indexRequest = new CreateIndexRequestBuilder( client, CreateIndexAction.INSTANCE ) .setIndex( index) .setSource( mappingsContent ) .request(); CreateIndexResponse indexResponse = client.admin().indices().create( indexRequest ).actionGet(); if ( !indexResponse.isAcknowledged() ) { throw new RuntimeException( "Error creating index [" + index + "]" ); } }