private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException { synchronized (CREATE_MAPPING_LOCK) { IndicesExists indicesExists = new IndicesExists.Builder(indexName).build(); boolean indexExists = jestClient.execute(indicesExists).isSucceeded(); if (!indexExists) { CreateIndex createIndex = new CreateIndex.Builder(indexName).build(); JestResult result = jestClient.execute(createIndex); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create index: %s", result.getErrorMessage())); } else { log.info("Created index {}", indexName); } URL url = ElasticWriter.class.getResource("/elastic-mapping.json"); String mapping = Resources.toString(url, Charsets.UTF_8); PutMapping putMapping = new PutMapping.Builder(indexName, typeName,mapping).build(); result = jestClient.execute(putMapping); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage())); } else { log.info("Created mapping for index {}", indexName); } } } }
public boolean create(String indexName, IndexSet indexSet, Map<String, Object> customSettings) { final Map<String, Object> settings = new HashMap<>(); settings.put("number_of_shards", indexSet.getConfig().shards()); settings.put("number_of_replicas", indexSet.getConfig().replicas()); settings.putAll(customSettings); final CreateIndex request = new CreateIndex.Builder(indexName) .settings(settings) .build(); // Make sure our index template exists before creating an index! ensureIndexTemplate(indexSet); final JestResult jestResult; try { jestResult = jestClient.execute(request); } catch (IOException e) { throw new ElasticsearchException("Couldn't create index " + indexName, e); } final boolean succeeded = jestResult.isSucceeded(); if (succeeded) { auditEventSender.success(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName)); } else { LOG.warn("Couldn't create index {}. Error: {}", indexName, jestResult.getErrorMessage()); auditEventSender.failure(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName)); } return succeeded; }
+ "}"; try { final JestResult result = jest.execute(new CreateIndex.Builder(index).settings(mappings).build()); if (result.isSucceeded()) { log.info("Correctly configured mapping.");
.loadFromSource(settingsAsString).build().getAsMap(); CreateIndex createIndex = new CreateIndex.Builder(index) .settings(settingsAsString) .build(); createIndex = new CreateIndex.Builder(index) .settings(settingsAsMap) .build();
log.info("create index " + indexName); rst = jestClient.execute( new CreateIndex.Builder(indexName) .settings(settings) .setParameter("master_timeout", "5m") .build() );
private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException { synchronized (CREATE_MAPPING_LOCK) { IndicesExists indicesExists = new IndicesExists.Builder(indexName).build(); boolean indexExists = jestClient.execute(indicesExists).isSucceeded(); if (!indexExists) { CreateIndex createIndex = new CreateIndex.Builder(indexName).build(); JestResult result = jestClient.execute(createIndex); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create index: %s", result.getErrorMessage())); } else { log.info("Created index {}", indexName); } URL url = ElasticWriter.class.getResource("/elastic-mapping.json"); String mapping = Resources.toString(url, Charsets.UTF_8); PutMapping putMapping = new PutMapping.Builder(indexName, typeName,mapping).build(); result = jestClient.execute(putMapping); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage())); } else { log.info("Created mapping for index {}", indexName); } } } }
public boolean init(String elasticHost, String elasticPort, String elasticIndex, String logType, Path processResultMappingFile) { this.elasticHost = elasticHost; this.elasticPort = elasticPort; this.elasticIndex = elasticIndex; this.logType = logType; try { final JestClient client = getElasticSearchClient(); boolean indexExists = client.execute(new IndicesExists.Builder(elasticIndex).build()).isSucceeded(); if (!indexExists){ log.info("Creating elasticsearch index."); client.execute(new CreateIndex.Builder(elasticIndex).build()); } if (StringUtils.isNotBlank(logType) && (processResultMappingFile != null)) { log.info("Updating type mapping."); final String mappingJson = new String(ByteStreams.toByteArray(Files.newInputStream(processResultMappingFile))); client.execute(new PutMapping.Builder(elasticIndex, logType, mappingJson).build()); } log.info("Established elasticsearch connection to host '{}:{}', index '{}'.", elasticHost, elasticPort, elasticIndex); elasticClient = client; } catch (IOException e) { log.error("Error creating base index on ElasticSearch: {}", e.getMessage(), e); elasticClient = null; } return true; }
public boolean create(String indexName, IndexSet indexSet, Map<String, Object> customSettings) { final Map<String, Object> settings = new HashMap<>(); settings.put("number_of_shards", indexSet.getConfig().shards()); settings.put("number_of_replicas", indexSet.getConfig().replicas()); settings.putAll(customSettings); final CreateIndex request = new CreateIndex.Builder(indexName) .settings(settings) .build(); // Make sure our index template exists before creating an index! ensureIndexTemplate(indexSet); final JestResult jestResult; try { jestResult = jestClient.execute(request); } catch (IOException e) { throw new ElasticsearchException("Couldn't create index " + indexName, e); } final boolean succeeded = jestResult.isSucceeded(); if (succeeded) { auditEventSender.success(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName)); } else { LOG.warn("Couldn't create index {}. Error: {}", indexName, jestResult.getErrorMessage()); auditEventSender.failure(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName)); } return succeeded; }
/** * The create index function will take as arguments the index name, document type, and mapping and will create a new index. */ @Override public final void createIndex(String indexName, String documentType, String mapping, String settings, String alias) { LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={}.", indexName, documentType); CreateIndex createIndex = new CreateIndex.Builder(indexName).settings(settings).build(); PutMapping putMapping = new PutMapping.Builder(indexName, documentType, mapping).build(); ModifyAliases modifyAliases = new ModifyAliases.Builder(new AddAliasMapping.Builder(indexName, alias).build()).build(); JestResult jestResult = jestClientHelper.execute(createIndex); LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded()); jestResult = jestClientHelper.execute(putMapping); LOGGER .info("Creating Elasticsearch index put mappings, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded()); jestResult = jestClientHelper.execute(modifyAliases); LOGGER.info("Creating Elasticsearch index alias, indexName={}, alias={}", indexName, alias, jestResult.isSucceeded()); // If there are failures log them if (!jestResult.isSucceeded()) { LOGGER.error("Error in index creation= {}", jestResult.getErrorMessage()); } }
public void validateIndex(String indexName) { try { IndicesExists indicesExistsRequest = new IndicesExists.Builder(indexName).build(); logger.debug("created indexExistsRequests: {}", indicesExistsRequest); JestResult existsResult = client.execute(indicesExistsRequest); logger.debug("indexExistsRequests result: {}", existsResult); if (!existsResult.isSucceeded()) { Settings settings = Settings.builder() // .put("index.analysis.analyzer.default.type", "keyword") .put("index.store.type", "mmapfs") .build(); CreateIndex createIndexRequest = new CreateIndex.Builder(indexName).settings(settings).build(); execute(createIndexRequest); //TODO: Make this work. Using the above "keyword" configuration in the meantime. PutMapping putMapping = new PutMapping.Builder(indexName, "_default_", STRING_NOT_ANALYZED).build(); execute(putMapping); logger.info("created index with settings: {}, indexName: {}, putMapping: {}", settings, indexName, putMapping); } } catch (IOException e) { logger.error("failed to connect to elastic cluster", e); } }
@Override public void sendPing() throws Exception { JestClient client = this.jestClientManager.getClient(); // Step 1: Create index (if already exists then it'll just be ignored) client.execute(new CreateIndex.Builder(JestClientManager.INDEX).build()); // Step 2: Create a mapping so that we can search distribution versions containing hyphens (otherwise they // are removed by the default tokenizer/analyzer). If mapping already exists then it'll just be ignored. PutMapping putMapping = new PutMapping.Builder(JestClientManager.INDEX, JestClientManager.TYPE, constructJSONMapping()).build(); client.execute(putMapping); // Step 3: Index the data Index index = new Index.Builder(constructIndexJSON()) .index(JestClientManager.INDEX) .type(JestClientManager.TYPE) .build(); JestResult result = client.execute(index); if (!result.isSucceeded()) { throw new Exception(result.getErrorMessage()); } }
/** * Creates an index. * @param indexName * @throws Exception */ @SuppressWarnings("nls") protected void createIndex(JestClient client, String indexName, String settingsName) throws Exception { URL settings = AbstractClientFactory.class.getResource(settingsName); String source = IOUtils.toString(settings); JestResult response = client.execute(new CreateIndex.Builder(indexName).settings(source).build()); if (!response.isSucceeded()) { // When running in e.g. Wildfly, the Gateway exists as two separate WARs - the API and the // runtime Gateway itself. They both create a registry and thus they both try to initialize // the ES index if it doesn't exist. A race condition could result in both WARs trying to // create the index. So a result of "IndexAlreadyExistsException" should be ignored. if (!indexAlreadyExistsException(response)) { throw new Exception("Failed to create index: '" + indexName + "' Reason: " + response.getErrorMessage()); } } }
/** * The create index function will take as arguments the index name, document type, and mapping and will create a new index. */ @Override public final void createIndex(String indexName, String documentType, String mapping, String settings, String alias) { LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={}.", indexName, documentType); CreateIndex createIndex = new CreateIndex.Builder(indexName).settings(settings).build(); PutMapping putMapping = new PutMapping.Builder(indexName, documentType, mapping).build(); ModifyAliases modifyAliases = new ModifyAliases.Builder(new AddAliasMapping.Builder(indexName, alias).build()).build(); JestResult jestResult = jestClientHelper.execute(createIndex); LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded()); jestResult = jestClientHelper.execute(putMapping); LOGGER .info("Creating Elasticsearch index put mappings, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded()); jestResult = jestClientHelper.execute(modifyAliases); LOGGER.info("Creating Elasticsearch index alias, indexName={}, alias={}", indexName, alias, jestResult.isSucceeded()); // If there are failures log them if (!jestResult.isSucceeded()) { LOGGER.error("Error in index creation= {}", jestResult.getErrorMessage()); } }
private void initJestClient() throws IOException { JestClientFactory factory = new JestClientFactory(); // Custom feature converter is required Gson gson = new GsonBuilder() // .registerTypeAdapter(Feature.class, new FeatureConverter()) // .registerTypeAdapter(Property.class, new PropertyConverter()) // .create(); factory.setHttpClientConfig(new HttpClientConfig.Builder(mapUrl()) // .multiThreaded(true) // .gson(gson) // .build()); jestClient = factory.getObject(); boolean indexExists = jestClient.execute(new IndicesExists.Builder(indexName).build()).isSucceeded(); if (indexExists) { jestClient.execute(new DeleteIndex.Builder(indexName).build()); } jestClient.execute(new CreateIndex.Builder(indexName).build()); }
/** * Creates an index. * @param indexName * @throws Exception */ @SuppressWarnings("nls") protected void createIndex(JestClient client, String indexName, String settingsName) throws Exception { URL settings = AbstractClientFactory.class.getResource(settingsName); String source = IOUtils.toString(settings); JestResult response = client.execute(new CreateIndex.Builder(indexName).settings(source).build()); if (!response.isSucceeded()) { // When running in e.g. Wildfly, the Gateway exists as two separate WARs - the API and the // runtime Gateway itself. They both create a registry and thus they both try to initialize // the ES index if it doesn't exist. A race condition could result in both WARs trying to // create the index. So a result of "IndexAlreadyExistsException" should be ignored. if (!indexAlreadyExistsException(response)) { throw new Exception("Failed to create index: '" + indexName + "' Reason: " + response.getErrorMessage()); } } }
@SuppressWarnings("unchecked") private void createIndex(List<Map<String, Object>> properties, Map<String, Object> indexSettings) throws IOException { for (Map<String, Object> property : properties) { if (property.containsKey(INDEX_ELEMENT)) { final Map<String, String> indexInformation = (Map<String, String>) property.get(INDEX_ELEMENT); final String indexName = indexInformation.get(INDEX_NAME_ELEMENT); if (indexName == null) { throw new IllegalArgumentException("Missing index name element in " + indexInformation); } final CreateIndex request = new CreateIndex.Builder(indexName) .settings(indexSettings) .build(); final JestResult result = client.execute(request); if (!result.isSucceeded()) { throw new IllegalStateException("Error while creating index " + indexName + ": " + result.getErrorMessage()); } } } }
@Override public boolean createIndex(String indexName) { return executeWithAcknowledge(new CreateIndex.Builder(indexName).build()); }
private void createIndexIfNotYetExisting() { if ( clientReference.get().executeRequest( new IndicesExists.Builder( actualIndexName ).build(), 404 ).getResponseCode() == 200 ) { return; } clientReference.get().executeRequest( new CreateIndex.Builder( actualIndexName ).build() ); }
private void createIndex() { CreateIndex createIndex = new CreateIndex.Builder( actualIndexName ) .build(); clientReference.get().executeRequest( createIndex ); waitForIndexCreation(); }
@Override public boolean createIndex(String indexName, Object settings) { CreateIndex.Builder createIndexBuilder = new CreateIndex.Builder(indexName); if (settings instanceof String) { createIndexBuilder.payload(String.valueOf(settings)); } else if (settings instanceof Map) { createIndexBuilder.payload((Map)settings); } return executeWithAcknowledge(createIndexBuilder.build()); }