Skip to content

Commit

Permalink
#3293: added endpoint for individual recreation of elasticsearch indices
Browse files Browse the repository at this point in the history
  • Loading branch information
moellerth committed Apr 16, 2024
1 parent 68ce6f8 commit 245f3ad
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -135,11 +136,21 @@ public ResponseEntity<String> search(@RequestBody(required = false) String body,
produces = MediaType.APPLICATION_JSON_VALUE)
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<?> recreateAllElasticsearchIndices() throws URISyntaxException {
log.debug("REST request to recreate all elasticsearch indices.");
elasticsearchAdminService.recreateAllIndices();
return ResponseEntity.ok().build();
}

/**
* Recreate selected elasticsearch indices.
*/
@RequestMapping(value = "/api/search/recreateByName", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<?> recreateSelectedElasticsearchIndices(@RequestParam List<ElasticsearchType> indices) throws URISyntaxException {
elasticsearchAdminService.recreateIndices(indices);
return ResponseEntity.ok().build();
}

/**
* Trigger processing of elasticsearch updates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

Expand Down Expand Up @@ -70,6 +71,7 @@ public class ElasticsearchAdminService {
*/
@Async
public CompletableFuture<Object> recreateAllIndices() {
log.info("Recreating all elasticsearch indices ...");
try {
for (ElasticsearchType type : ElasticsearchType.values()) {
recreateIndex(type);
Expand All @@ -85,6 +87,60 @@ public CompletableFuture<Object> recreateAllIndices() {
this.enqueueAllAnalysisPackages();
this.enqueueAllDataAcquisitionProjects();
updateQueueService.processAllQueueItems();
log.info("Finished recreating all indices.");
} catch (Exception e) {
log.error("Error during recreation of indices:", e);
}
return CompletableFuture.completedFuture(new Object());
}

/**
* Recreate the specified indices and their mappings. Asynchronous cause it might take a while.
*/
@Async
public CompletableFuture<Object> recreateIndices(List<ElasticsearchType> indices) {
log.info("Recreating the following indices: " + indices.toString() + " ...");
try {
for (ElasticsearchType index : indices) {
recreateIndex(index);
switch (index) {
case data_packages:
this.enqueueAllDataPackages();
break;
case variables:
this.enqueueAllVariables();
break;
case surveys:
this.enqueueAllSurveys();
break;
case data_sets:
this.enqueueAllDataSets();
break;
case questions:
this.enqueueAllQuestions();
break;
case related_publications:
this.enqueueAllRelatedPublications();
break;
case instruments:
this.enqueueAllInstruments();
break;
case concepts:
this.enqueueAllConcepts();
break;
case analysis_packages:
this.enqueueAllAnalysisPackages();
break;
case data_acquisition_projects:
this.enqueueAllDataAcquisitionProjects();
break;
default:
log.error("Enqueueing objects for index with name'" + index + "' has not been implemented yet.");
}
}
log.info("Enqueueing all objects ...");
updateQueueService.processAllQueueItems();
log.info("Finished recreating indices.");
} catch (Exception e) {
log.error("Error during recreation of indices:", e);
}
Expand Down

0 comments on commit 245f3ad

Please sign in to comment.