Skip to content

Commit cecc385

Browse files
committed
[NAE-1909] Elasticsearch index per URI node
- implement
1 parent 65d9220 commit cecc385

File tree

4 files changed

+29
-41
lines changed

4 files changed

+29
-41
lines changed

src/main/groovy/com/netgrif/application/engine/startup/ElasticsearchRunner.groovy

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,27 @@ class ElasticsearchRunner extends AbstractOrderedCommandLineRunner {
4848
void run(String... args) throws Exception {
4949
if (drop) {
5050
log.info("Dropping Elasticsearch database [${url}:${port}/${clusterName}]");
51-
if (!template.indexExists(caseIndex)) {
51+
if (template.indexExists(caseIndex)) {
5252
template.deleteIndex(ElasticCase.class)
5353
}
54-
if (!template.indexExists(taskIndex)) {
54+
if (template.indexExists(taskIndex)) {
5555
template.deleteIndex(ElasticTask.class)
5656
}
57-
if (!template.indexExists(uriProperties.index)) {
57+
try {
58+
template.getAllDynamicIndexes().forEach(indexName -> {
59+
if (template.indexExists(indexName)) {
60+
log.info("Deleting dynamic index {}", indexName);
61+
template.deleteIndex(indexName);
62+
} else {
63+
log.warn("Index {} does not exist, skipping deletion.", indexName);
64+
}
65+
})
66+
} catch (Exception e){
67+
log.warn("Index {} does not exist, skipping deletion.", e.message);
68+
}
69+
if (template.indexExists(uriProperties.index)) {
5870
template.deleteIndex(UriNode.class)
5971
}
60-
template.getAllDynamicIndexes().forEach(indexName -> {
61-
if (template.indexExists(indexName)) {
62-
log.info("Deleting dynamic index {}", indexName);
63-
template.deleteIndex(indexName);
64-
} else {
65-
log.warn("Index {} does not exist, skipping deletion.", indexName);
66-
}
67-
});
6872
template.evictAllCaches();
6973
}
7074

src/main/java/com/netgrif/application/engine/configuration/ElasticServiceConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
)
2424
public class ElasticServiceConfiguration {
2525

26-
@Autowired
27-
private ElasticCaseRepository caseRepository;
28-
2926
@Autowired
3027
private ElasticTaskRepository taskRepository;
3128

@@ -54,7 +51,7 @@ public Executor reindexingTaskTaskExecutor() {
5451
@Bean
5552
@Primary
5653
public IElasticCaseService elasticCaseService() {
57-
return new ElasticCaseService(caseRepository, elasticsearchTemplate, executor());
54+
return new ElasticCaseService(elasticsearchTemplate, executor());
5855
}
5956

6057
@Bean
@@ -65,7 +62,7 @@ public IElasticTaskService elasticTaskService() {
6562

6663
@Bean
6764
public IElasticCaseService reindexingTaskElasticCaseService() {
68-
return new ElasticCaseService(caseRepository, elasticsearchTemplate, reindexingTaskCaseExecutor());
65+
return new ElasticCaseService(elasticsearchTemplate, reindexingTaskCaseExecutor());
6966
}
7067

7168

src/main/java/com/netgrif/application/engine/elastic/service/ElasticCaseService.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,13 @@ public class ElasticCaseService extends ElasticViewPermissionService implements
4545

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

48-
private ElasticCaseRepository repository;
49-
5048
private IWorkflowService workflowService;
5149

5250
private Executor executors;
5351

54-
@Value("${spring.data.elasticsearch.index.case}")
55-
private String caseIndex;
56-
5752
@Autowired
5853
private ElasticsearchRestTemplate template;
5954

60-
6155
@Autowired
6256
private IElasticIndexService indexService;
6357

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

7670
@Autowired
77-
public ElasticCaseService(ElasticCaseRepository repository, ElasticsearchRestTemplate template, Executor executors) {
78-
this.repository = repository;
71+
public ElasticCaseService(ElasticsearchRestTemplate template, Executor executors) {
7972
this.template = template;
8073
this.executors = executors;
8174
}

src/main/java/com/netgrif/application/engine/elastic/service/ElasticIndexService.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.cache.CacheManager;
2424
import org.springframework.cache.annotation.Cacheable;
25-
import org.springframework.cache.annotation.EnableCaching;
2625
import org.springframework.context.ApplicationContext;
2726
import org.springframework.data.annotation.Id;
2827
import org.springframework.data.elasticsearch.annotations.Setting;
@@ -64,9 +63,6 @@ public class ElasticIndexService implements IElasticIndexService {
6463
@Autowired
6564
private ApplicationContext context;
6665

67-
@Autowired
68-
private ElasticsearchRestTemplate elasticsearchTemplate;
69-
7066
@Autowired
7167
private ElasticsearchOperations operations;
7268

@@ -170,7 +166,7 @@ public String getIndexByMenuItemId(String menuItemId) {
170166
@Override
171167
public boolean indexExists(String indexName) {
172168
try {
173-
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).exists();
169+
return template.indexOps(IndexCoordinates.of(indexName)).exists();
174170
} catch (Exception e) {
175171
log.error("indexExists:", e);
176172
return false;
@@ -180,7 +176,7 @@ public boolean indexExists(String indexName) {
180176
@Override
181177
public <T> String index(Class<T> clazz, T source, String... placeholders) {
182178
String indexName = getIndexName(clazz, placeholders);
183-
return elasticsearchTemplate.index(new IndexQueryBuilder().withId(getIdFromSource(source))
179+
return template.index(new IndexQueryBuilder().withId(getIdFromSource(source))
184180
.withObject(source).build(), IndexCoordinates.of(indexName));
185181
}
186182

@@ -266,7 +262,7 @@ public boolean deleteIndex(Class<?> clazz, String... placeholders) {
266262
String indexName = getIndexName(clazz, placeholders);
267263
if (this.indexExists(indexName)) {
268264
log.warn("Index: " + indexName + " has been deleted!");
269-
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).delete();
265+
return template.indexOps(IndexCoordinates.of(indexName)).delete();
270266
} else {
271267
log.warn("Index: " + indexName + " not found!");
272268
}
@@ -281,7 +277,7 @@ public boolean openIndex(Class<?> clazz, String... placeholders) {
281277
try {
282278
String indexName = getIndexName(clazz, placeholders);
283279
OpenIndexRequest request = new OpenIndexRequest(indexName);
284-
OpenIndexResponse execute = elasticsearchTemplate.execute(client -> client.indices().open(request, RequestOptions.DEFAULT));
280+
OpenIndexResponse execute = template.execute(client -> client.indices().open(request, RequestOptions.DEFAULT));
285281
boolean acknowledged = execute.isAcknowledged();
286282
if (acknowledged) {
287283
log.info("Open index {} success", indexName);
@@ -300,7 +296,7 @@ public boolean closeIndex(Class<?> clazz, String... placeholders) {
300296
try {
301297
String indexName = getIndexName(clazz, placeholders);
302298
CloseIndexRequest request = new CloseIndexRequest(indexName);
303-
CloseIndexResponse execute = elasticsearchTemplate.execute(client -> client.indices().close(request, RequestOptions.DEFAULT));
299+
CloseIndexResponse execute = template.execute(client -> client.indices().close(request, RequestOptions.DEFAULT));
304300
boolean acknowledged = execute.isAcknowledged();
305301
if (acknowledged) {
306302
log.info("Close index {} success", indexName);
@@ -318,7 +314,7 @@ public boolean closeIndex(Class<?> clazz, String... placeholders) {
318314
public SearchHits<?> search(Query query, Class<?> clazz, String... placeholders) {
319315
try {
320316
String indexName = getIndexName(clazz, placeholders);
321-
return elasticsearchTemplate.search(query, clazz, IndexCoordinates.of(indexName));
317+
return template.search(query, clazz, IndexCoordinates.of(indexName));
322318
} catch (Exception e) {
323319
log.error("scrollFirst:", e);
324320
}
@@ -346,7 +342,7 @@ public boolean putMapping(Class<?> clazz, String... placeholders) {
346342
try {
347343
String indexName = getIndexName(clazz, placeholders);
348344
Document mapping = operations.indexOps(clazz).createMapping();
349-
return elasticsearchTemplate.indexOps(IndexCoordinates.of(indexName)).putMapping(mapping);
345+
return template.indexOps(IndexCoordinates.of(indexName)).putMapping(mapping);
350346
} catch (Exception e) {
351347
log.error("deleteIndex:", e);
352348
return false;
@@ -359,7 +355,7 @@ public boolean putTemplate(String name, String source) {
359355
try {
360356
PutIndexTemplateRequest builder = new PutIndexTemplateRequest(name);
361357
builder.source(source, XContentType.JSON);
362-
AcknowledgedResponse execute = elasticsearchTemplate.execute(client -> client.indices().putTemplate(builder, RequestOptions.DEFAULT));
358+
AcknowledgedResponse execute = template.execute(client -> client.indices().putTemplate(builder, RequestOptions.DEFAULT));
363359
return execute.isAcknowledged();
364360
} catch (Exception e) {
365361
log.error("putTemplate:", e);
@@ -371,7 +367,7 @@ public boolean putTemplate(String name, String source) {
371367
public SearchScrollHits<?> scrollFirst(Query query, Class<?> clazz, String... placeholders) {
372368
String indexName = getIndexName(clazz, placeholders);
373369
try {
374-
return elasticsearchTemplate.searchScrollStart(60000, query, clazz, IndexCoordinates.of(indexName));
370+
return template.searchScrollStart(60000, query, clazz, IndexCoordinates.of(indexName));
375371
} catch (Exception e) {
376372
log.error("scrollFirst: ", e);
377373
}
@@ -382,7 +378,7 @@ public SearchScrollHits<?> scrollFirst(Query query, Class<?> clazz, String... pl
382378
public SearchScrollHits<?> scroll(String scrollId, Class<?> clazz, String... placeholders) {
383379
String indexName = getIndexName(clazz, placeholders);
384380
try {
385-
return elasticsearchTemplate.searchScrollContinue(scrollId, 60000, clazz, IndexCoordinates.of(indexName));
381+
return template.searchScrollContinue(scrollId, 60000, clazz, IndexCoordinates.of(indexName));
386382
} catch (Exception e) {
387383
log.error("scrollFirst:", e);
388384
}
@@ -461,6 +457,4 @@ private String getIndexName(Class<?> clazz, String... placeholders) {
461457
}
462458
return indexName;
463459
}
464-
465-
}
466-
460+
}

0 commit comments

Comments
 (0)