Skip to content

Commit

Permalink
[NAE-1909] Elasticsearch index per URI node
Browse files Browse the repository at this point in the history
- implement
  • Loading branch information
machacjozef committed Aug 1, 2023
1 parent 65d9220 commit cecc385
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ class ElasticsearchRunner extends AbstractOrderedCommandLineRunner {
void run(String... args) throws Exception {
if (drop) {
log.info("Dropping Elasticsearch database [${url}:${port}/${clusterName}]");
if (!template.indexExists(caseIndex)) {
if (template.indexExists(caseIndex)) {
template.deleteIndex(ElasticCase.class)
}
if (!template.indexExists(taskIndex)) {
if (template.indexExists(taskIndex)) {
template.deleteIndex(ElasticTask.class)
}
if (!template.indexExists(uriProperties.index)) {
try {
template.getAllDynamicIndexes().forEach(indexName -> {
if (template.indexExists(indexName)) {
log.info("Deleting dynamic index {}", indexName);
template.deleteIndex(indexName);
} else {
log.warn("Index {} does not exist, skipping deletion.", indexName);
}
})
} catch (Exception e){
log.warn("Index {} does not exist, skipping deletion.", e.message);
}
if (template.indexExists(uriProperties.index)) {
template.deleteIndex(UriNode.class)
}
template.getAllDynamicIndexes().forEach(indexName -> {
if (template.indexExists(indexName)) {
log.info("Deleting dynamic index {}", indexName);
template.deleteIndex(indexName);
} else {
log.warn("Index {} does not exist, skipping deletion.", indexName);
}
});
template.evictAllCaches();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
)
public class ElasticServiceConfiguration {

@Autowired
private ElasticCaseRepository caseRepository;

@Autowired
private ElasticTaskRepository taskRepository;

Expand Down Expand Up @@ -54,7 +51,7 @@ public Executor reindexingTaskTaskExecutor() {
@Bean
@Primary
public IElasticCaseService elasticCaseService() {
return new ElasticCaseService(caseRepository, elasticsearchTemplate, executor());
return new ElasticCaseService(elasticsearchTemplate, executor());
}

@Bean
Expand All @@ -65,7 +62,7 @@ public IElasticTaskService elasticTaskService() {

@Bean
public IElasticCaseService reindexingTaskElasticCaseService() {
return new ElasticCaseService(caseRepository, elasticsearchTemplate, reindexingTaskCaseExecutor());
return new ElasticCaseService(elasticsearchTemplate, reindexingTaskCaseExecutor());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,13 @@ public class ElasticCaseService extends ElasticViewPermissionService implements

private static final Logger log = LoggerFactory.getLogger(ElasticCaseService.class);

private ElasticCaseRepository repository;

private IWorkflowService workflowService;

private Executor executors;

@Value("${spring.data.elasticsearch.index.case}")
private String caseIndex;

@Autowired
private ElasticsearchRestTemplate template;


@Autowired
private IElasticIndexService indexService;

Expand All @@ -74,8 +68,7 @@ public class ElasticCaseService extends ElasticViewPermissionService implements
// private IImpersonationElasticFilterService impersonationElasticFilterService;

@Autowired
public ElasticCaseService(ElasticCaseRepository repository, ElasticsearchRestTemplate template, Executor executors) {
this.repository = repository;
public ElasticCaseService(ElasticsearchRestTemplate template, Executor executors) {
this.template = template;
this.executors = executors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Setting;
Expand Down Expand Up @@ -64,9 +63,6 @@ public class ElasticIndexService implements IElasticIndexService {
@Autowired
private ApplicationContext context;

@Autowired
private ElasticsearchRestTemplate elasticsearchTemplate;

@Autowired
private ElasticsearchOperations operations;

Expand Down Expand Up @@ -170,7 +166,7 @@ public String getIndexByMenuItemId(String menuItemId) {
@Override
public boolean indexExists(String indexName) {
try {
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).exists();
return template.indexOps(IndexCoordinates.of(indexName)).exists();
} catch (Exception e) {
log.error("indexExists:", e);
return false;
Expand All @@ -180,7 +176,7 @@ public boolean indexExists(String indexName) {
@Override
public <T> String index(Class<T> clazz, T source, String... placeholders) {
String indexName = getIndexName(clazz, placeholders);
return elasticsearchTemplate.index(new IndexQueryBuilder().withId(getIdFromSource(source))
return template.index(new IndexQueryBuilder().withId(getIdFromSource(source))
.withObject(source).build(), IndexCoordinates.of(indexName));
}

Expand Down Expand Up @@ -266,7 +262,7 @@ public boolean deleteIndex(Class<?> clazz, String... placeholders) {
String indexName = getIndexName(clazz, placeholders);
if (this.indexExists(indexName)) {
log.warn("Index: " + indexName + " has been deleted!");
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).delete();
return template.indexOps(IndexCoordinates.of(indexName)).delete();
} else {
log.warn("Index: " + indexName + " not found!");
}
Expand All @@ -281,7 +277,7 @@ public boolean openIndex(Class<?> clazz, String... placeholders) {
try {
String indexName = getIndexName(clazz, placeholders);
OpenIndexRequest request = new OpenIndexRequest(indexName);
OpenIndexResponse execute = elasticsearchTemplate.execute(client -> client.indices().open(request, RequestOptions.DEFAULT));
OpenIndexResponse execute = template.execute(client -> client.indices().open(request, RequestOptions.DEFAULT));
boolean acknowledged = execute.isAcknowledged();
if (acknowledged) {
log.info("Open index {} success", indexName);
Expand All @@ -300,7 +296,7 @@ public boolean closeIndex(Class<?> clazz, String... placeholders) {
try {
String indexName = getIndexName(clazz, placeholders);
CloseIndexRequest request = new CloseIndexRequest(indexName);
CloseIndexResponse execute = elasticsearchTemplate.execute(client -> client.indices().close(request, RequestOptions.DEFAULT));
CloseIndexResponse execute = template.execute(client -> client.indices().close(request, RequestOptions.DEFAULT));
boolean acknowledged = execute.isAcknowledged();
if (acknowledged) {
log.info("Close index {} success", indexName);
Expand All @@ -318,7 +314,7 @@ public boolean closeIndex(Class<?> clazz, String... placeholders) {
public SearchHits<?> search(Query query, Class<?> clazz, String... placeholders) {
try {
String indexName = getIndexName(clazz, placeholders);
return elasticsearchTemplate.search(query, clazz, IndexCoordinates.of(indexName));
return template.search(query, clazz, IndexCoordinates.of(indexName));
} catch (Exception e) {
log.error("scrollFirst:", e);
}
Expand Down Expand Up @@ -346,7 +342,7 @@ public boolean putMapping(Class<?> clazz, String... placeholders) {
try {
String indexName = getIndexName(clazz, placeholders);
Document mapping = operations.indexOps(clazz).createMapping();
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).putMapping(mapping);
return template.indexOps(IndexCoordinates.of(indexName)).putMapping(mapping);
} catch (Exception e) {
log.error("deleteIndex:", e);
return false;
Expand All @@ -359,7 +355,7 @@ public boolean putTemplate(String name, String source) {
try {
PutIndexTemplateRequest builder = new PutIndexTemplateRequest(name);
builder.source(source, XContentType.JSON);
AcknowledgedResponse execute = elasticsearchTemplate.execute(client -> client.indices().putTemplate(builder, RequestOptions.DEFAULT));
AcknowledgedResponse execute = template.execute(client -> client.indices().putTemplate(builder, RequestOptions.DEFAULT));
return execute.isAcknowledged();
} catch (Exception e) {
log.error("putTemplate:", e);
Expand All @@ -371,7 +367,7 @@ public boolean putTemplate(String name, String source) {
public SearchScrollHits<?> scrollFirst(Query query, Class<?> clazz, String... placeholders) {
String indexName = getIndexName(clazz, placeholders);
try {
return elasticsearchTemplate.searchScrollStart(60000, query, clazz, IndexCoordinates.of(indexName));
return template.searchScrollStart(60000, query, clazz, IndexCoordinates.of(indexName));
} catch (Exception e) {
log.error("scrollFirst: ", e);
}
Expand All @@ -382,7 +378,7 @@ public SearchScrollHits<?> scrollFirst(Query query, Class<?> clazz, String... pl
public SearchScrollHits<?> scroll(String scrollId, Class<?> clazz, String... placeholders) {
String indexName = getIndexName(clazz, placeholders);
try {
return elasticsearchTemplate.searchScrollContinue(scrollId, 60000, clazz, IndexCoordinates.of(indexName));
return template.searchScrollContinue(scrollId, 60000, clazz, IndexCoordinates.of(indexName));
} catch (Exception e) {
log.error("scrollFirst:", e);
}
Expand Down Expand Up @@ -461,6 +457,4 @@ private String getIndexName(Class<?> clazz, String... placeholders) {
}
return indexName;
}

}

}

0 comments on commit cecc385

Please sign in to comment.