Skip to content

Commit 702f32b

Browse files
Add endpoint to recreate the index
Now free from manual `curl` commands!
1 parent 7b19a1a commit 702f32b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/main/java/org/gridsuite/directory/server/SupervisionController.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
import io.swagger.v3.oas.annotations.tags.Tag;
1313
import org.gridsuite.directory.server.dto.ElementAttributes;
1414
import org.gridsuite.directory.server.services.SupervisionService;
15+
import org.springframework.http.MediaType;
1516
import org.springframework.http.ResponseEntity;
1617
import org.springframework.web.bind.annotation.*;
1718

1819
import java.util.List;
20+
import java.util.Optional;
1921
import java.util.UUID;
2022

2123
/**
2224
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
2325
*/
2426
@RestController
25-
@RequestMapping(value = "/" + DirectoryApi.API_VERSION + "/supervision")
27+
@RequestMapping(value = "/" + DirectoryApi.API_VERSION + "/supervision", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
2628
@Tag(name = "directory-server - Supervision")
2729
public class SupervisionController {
2830
private final SupervisionService service;
@@ -47,4 +49,17 @@ public ResponseEntity<Void> deleteElements(@RequestParam("ids") List<UUID> eleme
4749
service.deleteElementsByIds(elementsUuid);
4850
return ResponseEntity.ok().build();
4951
}
52+
53+
@PostMapping(value = "/elements/recreate-index", produces = MediaType.TEXT_PLAIN_VALUE)
54+
@Operation(summary = "Recreate the index then reindex data")
55+
@ApiResponse(responseCode = "200", description = "Success of the index recreation & reindexing of elements")
56+
@ApiResponse(responseCode = "500", description = "An error happen while recreating the index.\nAn manual intervention is needing as no details of the error is available.")
57+
public ResponseEntity<Optional<String>> deleteElements() {
58+
if (service.recreateIndexDirectoryElementInfos()) {
59+
return ResponseEntity.ok().build();
60+
} else {
61+
return ResponseEntity.internalServerError().contentType(MediaType.TEXT_PLAIN)
62+
.body(Optional.of("An error happen while re-creating the index. As no details is available an manual intervention is required."));
63+
}
64+
}
5065
}

src/main/java/org/gridsuite/directory/server/services/SupervisionService.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.gridsuite.directory.server.services;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.gridsuite.directory.server.dto.ElementAttributes;
5+
import org.gridsuite.directory.server.dto.elasticsearch.DirectoryElementInfos;
46
import org.gridsuite.directory.server.repository.DirectoryElementEntity;
57
import org.gridsuite.directory.server.repository.DirectoryElementRepository;
8+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
9+
import org.springframework.data.elasticsearch.core.IndexOperations;
610
import org.springframework.stereotype.Service;
711

812
import java.util.List;
@@ -11,13 +15,17 @@
1115
import static org.gridsuite.directory.server.dto.ElementAttributes.toElementAttributes;
1216

1317
@Service
18+
@Slf4j
1419
public class SupervisionService {
1520
private final DirectoryElementRepository directoryElementRepository;
1621
private final DirectoryRepositoryService repositoryService;
22+
private final ElasticsearchTemplate esTemplate;
1723

18-
public SupervisionService(DirectoryRepositoryService repositoryService, DirectoryElementRepository directoryElementRepository) {
24+
public SupervisionService(DirectoryRepositoryService repositoryService, DirectoryElementRepository directoryElementRepository,
25+
ElasticsearchTemplate esTemplate) {
1926
this.repositoryService = repositoryService;
2027
this.directoryElementRepository = directoryElementRepository;
28+
this.esTemplate = esTemplate;
2129
}
2230

2331
public List<ElementAttributes> getStashedElementsAttributes() {
@@ -35,4 +43,28 @@ public void deleteElementsByIds(List<UUID> uuids) {
3543
public List<DirectoryElementEntity> getStashedElements() {
3644
return directoryElementRepository.findAllByStashed(true);
3745
}
46+
47+
public boolean recreateIndexDirectoryElementInfos() {
48+
final IndexOperations idxDirectoryElementInfos = esTemplate.indexOps(DirectoryElementInfos.class);
49+
final String idxDirectoryElementInfosName = idxDirectoryElementInfos.getIndexCoordinates().getIndexName();
50+
log.warn("Recreating ElasticSearch index {}", idxDirectoryElementInfosName);
51+
if (idxDirectoryElementInfos.exists()) {
52+
log.info("Index {} found, delete it.", idxDirectoryElementInfosName);
53+
if (idxDirectoryElementInfos.delete()) {
54+
log.info("Successfully delete index {}", idxDirectoryElementInfosName);
55+
} else {
56+
log.error("A problem seems to happen when deleting index {}...", idxDirectoryElementInfosName);
57+
return false;
58+
}
59+
}
60+
if (idxDirectoryElementInfos.createWithMapping()) {
61+
log.info("Index {} successfully recreated!", idxDirectoryElementInfosName);
62+
} else {
63+
log.info("An error happen while re-creating index {}...", idxDirectoryElementInfosName);
64+
return false;
65+
}
66+
log.info("Re-indexing all elements of {}", idxDirectoryElementInfosName);
67+
repositoryService.reindexAllElements();
68+
return true;
69+
}
3870
}

0 commit comments

Comments
 (0)