ElasticsearchController.java
/*
* Copyright 2022 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.api.admin.v1;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.component.elastic.ElasticReindex;
import org.gringlobal.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
@RestController("elasticSearchApi1")
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@RequestMapping(ElasticsearchController.API_URL)
@Api(tags = { "elasticv1" })
@Slf4j
public class ElasticsearchController {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv1_BASE + "/admin/elastic";
@Autowired(required = false)
private ElasticsearchService elasticsearchService;
@Resource
private BlockingQueue<ElasticReindex> elasticReindexQueue;
@Autowired
private TaskExecutor taskExecutor;
@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
public IndexResponse getIndexResponse() throws IOException {
IndexResponse indexResponse = new IndexResponse();
var listIndicesResponse = elasticsearchService.listIndices();
Map<String, List<AliasMetaData>> aliases = new HashMap<>();
if (listIndicesResponse != null) {
aliases = listIndicesResponse.getAliases();
}
indexResponse.indexes = aliases;
indexResponse.reindexTypes = createReindexTypesMap();
indexResponse.updateQueueSize = elasticReindexQueue.size();
return indexResponse;
}
@PostMapping(value = "/reindex")
public void reindexElasticContent(@RequestParam(value = "type") String type) {
if (elasticReindexQueue.size() > 0) {
throw new RuntimeException("Reindex queue not empty or operation is locked! Unable to run new indexing.");
}
if (type.equals("All")) {
taskExecutor.execute(() -> {
try {
log.warn("Reindexing EVERYTHING!!!");
elasticsearchService.reindexAll();
} catch (Throwable e) {
log.error("Error executing reindexAll", e);
}
});
} else {
taskExecutor.execute(() -> {
try {
log.warn("Reindexing {}", type);
elasticsearchService.reindex(Class.forName(type));
} catch (Throwable e) {
log.error("Error executing reindex of " + type, e);
}
});
}
}
@PostMapping(value = "/realias")
public void moveAlias(@RequestParam(name = "aliasName") String aliasName, @RequestParam(name = "indexName") String indexName) {
elasticsearchService.realias(aliasName, null, indexName);
}
@PostMapping(value = "/delete-alias/{name}")
public void deleteAlias(@PathVariable(name = "name") String aliasName) {
elasticsearchService.deleteAlias(aliasName);
}
@PostMapping(value = "/delete-index/{name}")
public void deleteIndex(@PathVariable(name = "name") String indexName) {
elasticsearchService.deleteIndex(indexName);
}
public static class IndexResponse {
public Map<String, List<AliasMetaData>> indexes;
public Map<String, String> reindexTypes;
public int updateQueueSize;
}
private Map<String, String> createReindexTypesMap() {
Map<String, String> reindexTypesMap = new HashMap<>();
for (Class<?> clazz : elasticsearchService.getIndexedEntities()) {
reindexTypesMap.put(clazz.getSimpleName(), clazz.getName());
}
return reindexTypesMap;
}
}