use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project graylog2-server by Graylog2.
the class ClientES7 method templateExists.
@Override
public boolean templateExists(String templateName) {
final GetIndexTemplatesRequest request = new GetIndexTemplatesRequest("*");
final GetIndexTemplatesResponse result = client.execute((c, requestOptions) -> c.indices().getIndexTemplate(request, requestOptions));
return result.getIndexTemplates().stream().anyMatch(indexTemplate -> indexTemplate.name().equals(templateName));
}
use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project spring-data-elasticsearch by spring-projects.
the class RestIndexTemplate method getTemplate.
@Override
public TemplateData getTemplate(GetTemplateRequest getTemplateRequest) {
Assert.notNull(getTemplateRequest, "getTemplateRequest must not be null");
// getIndexTemplate throws an error on non-existing template names
if (!existsTemplate(new ExistsTemplateRequest(getTemplateRequest.getTemplateName()))) {
return null;
}
GetIndexTemplatesRequest getIndexTemplatesRequest = requestFactory.getIndexTemplatesRequest(getTemplateRequest);
GetIndexTemplatesResponse getIndexTemplatesResponse = restTemplate.execute(client -> client.indices().getIndexTemplate(getIndexTemplatesRequest, RequestOptions.DEFAULT));
return ResponseConverter.getTemplateData(getIndexTemplatesResponse, getTemplateRequest.getTemplateName());
}
use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project hopsworks by logicalclocks.
the class PyPiLibraryElasticIndexer method execute.
@Timeout
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void execute(Timer timer) throws ElasticException {
long errorRescheduleTimeout = 600000;
LOGGER.log(Level.INFO, "Running PyPi Indexer");
try {
ClusterHealthResponse clusterHealthResponse = elasticClientCtrl.clusterHealthGet();
GetIndexTemplatesResponse templateResponse = elasticClientCtrl.templateGet(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS);
// If elasticsearch is down or template not in elastic reschedule the timer
if (clusterHealthResponse.getStatus().equals(ClusterHealthStatus.RED)) {
scheduleTimer(errorRescheduleTimeout);
LOGGER.log(Level.INFO, "Elastic currently down, rescheduling indexing for pypi libraries");
return;
} else if (templateResponse.getIndexTemplates().isEmpty()) {
scheduleTimer(errorRescheduleTimeout);
LOGGER.log(Level.INFO, "Elastic template " + Settings.ELASTIC_PYPI_LIBRARIES_ALIAS + " currently missing, " + "rescheduling indexing for pypi libraries");
return;
}
} catch (Exception e) {
scheduleTimer(errorRescheduleTimeout);
LOGGER.log(Level.SEVERE, "Exception occurred trying to index pypi libraries, rescheduling timer", e);
return;
}
String newIndex = Settings.ELASTIC_PYPI_LIBRARIES_INDEX_PATTERN_PREFIX + System.currentTimeMillis();
try {
GetAliasesResponse pypiAlias = elasticClientCtrl.getAliases(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS);
if (!pypiAlias.getAliases().isEmpty()) {
this.isIndexed = true;
}
String[] indicesToDelete = elasticClientCtrl.mngIndicesGetBySimplifiedRegex(Settings.ELASTIC_PYPI_LIBRARIES_INDEX_REGEX);
Element body = Jsoup.connect(settings.getPyPiSimpleEndpoint()).maxBodySize(0).get().body();
Elements elements = body.getElementsByTag("a");
CreateIndexRequest createIndexRequest = new CreateIndexRequest(newIndex);
elasticClientCtrl.mngIndexCreate(createIndexRequest);
final int bulkSize = 100;
int currentBulkSize = 0;
int currentId = 0;
BulkRequest bulkRequest = new BulkRequest();
LOGGER.log(Level.INFO, "Starting to index libraries from pypi simple index");
for (Element library : elements) {
IndexRequest indexRequest = new IndexRequest().index(newIndex).id(String.valueOf(currentId)).source(jsonBuilder().startObject().field("library", library.text()).endObject());
bulkRequest.add(indexRequest);
currentBulkSize += 1;
currentId += 1;
if (currentBulkSize == bulkSize) {
elasticClientCtrl.bulkUpdateDoc(bulkRequest);
bulkRequest = new BulkRequest();
currentBulkSize = 0;
}
}
// Also send last batch
if (bulkRequest.numberOfActions() > 0) {
elasticClientCtrl.bulkUpdateDoc(bulkRequest);
}
if (pypiAlias.getAliases().isEmpty()) {
elasticClientCtrl.createAlias(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS, newIndex);
} else {
String currentSearchIndex = pypiAlias.getAliases().keySet().iterator().next();
elasticClientCtrl.aliasSwitchIndex(Settings.ELASTIC_PYPI_LIBRARIES_ALIAS, currentSearchIndex, newIndex);
}
this.isIndexed = true;
LOGGER.log(Level.INFO, "Finished indexing");
for (String index : indicesToDelete) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest().indices(index);
elasticClientCtrl.mngIndexDelete(deleteIndexRequest);
}
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Indexing pypi libraries failed", ex);
scheduleTimer(errorRescheduleTimeout);
if (elasticClientCtrl.mngIndexExists(newIndex)) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest().indices(newIndex);
elasticClientCtrl.mngIndexDelete(deleteIndexRequest);
}
return;
}
String rawInterval = settings.getPyPiIndexerTimerInterval();
Long intervalValue = settings.getConfTimeValue(rawInterval);
TimeUnit intervalTimeunit = settings.getConfTimeTimeUnit(rawInterval);
scheduleTimer(intervalTimeunit.toMillis(intervalValue));
}
use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project graylog2-server by Graylog2.
the class ClientES7 method existingTemplates.
private String[] existingTemplates() {
final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest();
final GetIndexTemplatesResponse result = client.execute((c, requestOptions) -> c.indices().getIndexTemplate(getIndexTemplatesRequest, requestOptions));
return result.getIndexTemplates().stream().map(IndexTemplateMetadata::name).toArray(String[]::new);
}
use of org.elasticsearch.client.indices.GetIndexTemplatesResponse in project phoebus-olog by Olog.
the class ElasticConfig method elasticIndexValidation.
/**
* Checks for the existence of the elastic indices needed for Olog and creates
* them with the appropriate mapping is they are missing.
*
* @param indexClient the elastic client instance used to validate and create
* olog indices
*/
private synchronized void elasticIndexValidation(RestHighLevelClient indexClient) {
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_TAG_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_TAG_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/tag_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_TAG_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_TAG_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_TAG_INDEX, e);
}
// Create/migrate the logbook index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_LOGBOOK_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_LOGBOOK_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/logbook_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_LOGBOOK_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_LOGBOOK_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_LOGBOOK_INDEX, e);
}
// Create/migrate the property index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_PROPERTY_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_PROPERTY_INDEX);
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/property_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_PROPERTY_TYPE, jsonMap);
indexClient.indices().create(createRequest, RequestOptions.DEFAULT);
logger.info("Successfully created index: " + ES_PROPERTY_INDEX);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_PROPERTY_INDEX, e);
}
// Create/migrate the sequence index
try {
if (!indexClient.indices().exists(new GetIndexRequest().indices(ES_SEQ_INDEX), RequestOptions.DEFAULT)) {
CreateIndexRequest createRequest = new CreateIndexRequest(ES_SEQ_INDEX);
createRequest.settings(Settings.builder().put("index.number_of_shards", 1).put("auto_expand_replicas", "0-all"));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/seq_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
createRequest.mapping(ES_SEQ_TYPE, jsonMap);
logger.info("Successfully created index: " + ES_SEQ_TYPE);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create index " + ES_SEQ_INDEX, e);
}
// create/migrate log template
try {
GetIndexTemplatesResponse templates = indexClient.indices().getIndexTemplate(new GetIndexTemplatesRequest("*"), RequestOptions.DEFAULT);
if (!templates.getIndexTemplates().stream().anyMatch(i -> {
return i.name().equalsIgnoreCase(ES_LOG_INDEX + "_template");
})) {
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(ES_LOG_INDEX + "_template");
templateRequest.patterns(Arrays.asList(ES_LOG_INDEX));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/log_template_mapping.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
templateRequest.mapping(ES_LOG_TYPE, XContentFactory.jsonBuilder().map(jsonMap));
templateRequest.create(true);
indexClient.indices().putTemplate(templateRequest, RequestOptions.DEFAULT);
}
// Get the index templates again...
templates = indexClient.indices().getIndexTemplate(new GetIndexTemplatesRequest("*"), RequestOptions.DEFAULT);
if (templates.getIndexTemplates().stream().anyMatch(i -> {
return i.name().equalsIgnoreCase(ES_LOG_INDEX + "_template") && i.version() == null;
})) {
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(ES_LOG_INDEX + "_template");
templateRequest.patterns(Arrays.asList(ES_LOG_INDEX));
ObjectMapper mapper = new ObjectMapper();
InputStream is = ElasticConfig.class.getResourceAsStream("/log_template_mapping_with_title.json");
Map<String, String> jsonMap = mapper.readValue(is, Map.class);
templateRequest.mapping(ES_LOG_TYPE, XContentFactory.jsonBuilder().map(jsonMap)).version(2);
templateRequest.create(false);
indexClient.indices().putTemplate(templateRequest, RequestOptions.DEFAULT);
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to create template for index " + ES_LOG_TYPE, e);
}
}
Aggregations