Commit d1557e92 authored by Maxym Borodenko's avatar Maxym Borodenko

ES reindexing using BasicFilter

parent 819dabb1
......@@ -16,11 +16,13 @@
package org.genesys2.server.api.v1;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.dataset.Dataset;
......@@ -34,18 +36,22 @@ import org.genesys.filerepository.persistence.RepositoryFolderRepository;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.ScheduledGLISUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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;
......@@ -63,6 +69,8 @@ public class AdminController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/admin";
public static final String ELASTIC_SEARCH_URL = "/elastic";
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(AdminController.class);
......@@ -78,9 +86,6 @@ public class AdminController {
@Autowired
private ImageGalleryService imageGalleryService;
@Autowired
private ElasticsearchService elasticsearch;
@Autowired
private CustomAclService aclService;
......@@ -90,6 +95,12 @@ public class AdminController {
@Autowired
private ScheduledGLISUpdater scheduledGLISUpdater;
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private ElasticsearchService elasticsearchService;
@PostMapping(value = "/ensure-inst-folders")
public Boolean ensureInstituteFolders() throws Exception {
LOG.info("Ensure institute folders");
......@@ -137,10 +148,51 @@ public class AdminController {
return true;
}
@PostMapping(value = "/reindex-es")
public Boolean reindexElasticsearch() {
LOG.info("Reindex Elasticsearch");
elasticsearch.reindexAll();
/**
* This method refreshes data in the currently active index. It is very handy
* when having to refresh part of ES after direct database update.
*
* @param type
*/
@PostMapping(value = ELASTIC_SEARCH_URL + "/reindex/{type}")
public Boolean reindexElasticContent(@PathVariable(required = true) String type) {
if (type.equals("All")) {
taskExecutor.execute(() -> {
try {
elasticsearchService.reindexAll();
} catch (Throwable e) {
LOG.error("Error executing reindexAll", e);
}
});
} else {
taskExecutor.execute(() -> {
try {
elasticsearchService.reindex(Class.forName(type));
} catch (Throwable e) {
LOG.error("Error executing reindex of " + type, e);
}
});
}
return true;
}
/**
* This method refreshes data in the currently active index. It is very handy
* when having to refresh part of ES after direct database update.
*
* @param jsonFilter
* @throws IOException
*/
@PostMapping(value = ELASTIC_SEARCH_URL + "/reindex")
public Boolean reindexElasticFiltered(@RequestParam(value = "filter", required = true) String jsonFilter) throws IOException {
AccessionFilter accessionFilter = new ObjectMapper().readValue(jsonFilter, AccessionFilter.class);
taskExecutor.execute(() -> {
try {
elasticsearchService.reindex(Accession.class, accessionFilter);
} catch (Throwable e) {
LOG.error("Error executing reindex Accession", e);
}
});
return true;
}
......
......@@ -14,7 +14,7 @@
<h3>Elastic reindex</h3>
<form method="post" action="<c:url value="/admin/elastic/action" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<input type="text" name="filter" placeholder="Genesys filter {}" value='{"holder":{"code":["NGA039"]}}' />
<input type="text" name="filter" placeholder="Genesys filter {}" value='{"institute":{"code":["NGA039"]}}' />
<button type="submit" class="btn btn-default" value="accn" name="reindex">Reindex</button>
<button type="submit" class="btn btn-default" value="accn" name="regenerate">Regenerate</button>
</form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment