Commit 52c6d72b authored by Matija Obreza's avatar Matija Obreza

Refactored ES methods

parent f7e2c1f0
......@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
......@@ -83,8 +84,8 @@ public class SearchController {
QueryBuilder extraFilters = filtersFromDataset(filters);
Map<Class<BasicModel>, List<BasicModel>> hitsByEntity = elasticsearch.search(extraFilters, newHashSet(Crop.class, Partner.class,
AccessionIdentifier.class, Descriptor.class), searchQuery);
Set<Class<? extends BasicModel>> clazzes = newHashSet(Crop.class, Partner.class, Descriptor.class);
Map<Class<? extends BasicModel>, List<? extends BasicModel>> hitsByEntity = elasticsearch.search(extraFilters, searchQuery, clazzes);
Map<String, SearchResults<?>> suggestions = new HashMap<>();
suggestions.put("search.group.crop", SearchResults.from("code", Arrays.asList("crop"), hitsByEntity.get(Crop.class)));
......@@ -93,7 +94,7 @@ public class SearchController {
suggestions.put("search.group.descriptor", SearchResults.from("uuid", Arrays.asList("descriptor.uuid"), hitsByEntity.get(Descriptor.class)));
// Search datasets
suggestions.put("search.matches", SearchResults.from("uuid", Arrays.asList("uuid"), elasticsearch.search(extraFilters, Dataset.class, searchQuery)));
suggestions.put("search.matches", SearchResults.from("uuid", Arrays.asList("uuid"), elasticsearch.search(extraFilters, searchQuery, Dataset.class)));
return suggestions;
}
......
......@@ -336,7 +336,7 @@ public class AccessionController extends ApiBaseController {
@RequestMapping(value = "/search", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<Accession> search(@RequestParam("page") final int page, @RequestParam("query") final String query) throws SearchException {
return elasticService.search(null, Accession.class, StringUtils.defaultIfBlank(query, "*")); //, new PageRequest(page - 1, 20));
return elasticService.search(null, StringUtils.defaultIfBlank(query, "*"), Accession.class); //, new PageRequest(page - 1, 20));
}
// FIXME Not using institute...
......@@ -349,7 +349,7 @@ public class AccessionController extends ApiBaseController {
}
query = StringUtils.defaultIfBlank(query, "*");
// TODO filer by inst
return elasticService.search(null, Accession.class, query); // , new PageRequest(page - 1, 20));
return elasticService.search(null, query, Accession.class); // , new PageRequest(page - 1, 20));
}
@RequestMapping(value = "/{id}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
......
......@@ -16,6 +16,8 @@
package org.genesys2.server.component.elastic;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
......@@ -24,18 +26,23 @@ import java.util.concurrent.BlockingQueue;
import javax.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.service.ElasticsearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.stereotype.Component;
/**
* AspectJ powered listener on repository save and delete operations
* adds indexed entities to re-index queue, handled by {@link ElasticReindexProcessor}.
* AspectJ powered listener on repository save and delete operations adds
* indexed entities to re-index queue, handled by
* {@link ElasticReindexProcessor}.
*/
@Aspect
@Component
......@@ -44,10 +51,13 @@ public class ElasticJPAListener {
private Set<Object> includedClasses;
private Set<Object> ignoredClasses;
@Resource
private BlockingQueue<ElasticReindex> elasticReindexQueue;
@Autowired
private ElasticsearchService elasticsearchService;
/**
* Instantiates a new elastic JPA listener.
*/
......@@ -91,6 +101,35 @@ public class ElasticJPAListener {
}
}
/**
* Delete all
*
* @param joinPoint the join point
*/
@After(value = "execution(* org.springframework.data.jpa.repository.JpaRepository.deleteAll())")
public void afterDeleteAll(final JoinPoint joinPoint) {
try {
LOG.trace("JPA afterDeleteAll: {} {}", joinPoint.toLongString(), joinPoint.getTarget());
Object proxy = joinPoint.getTarget();
if (proxy instanceof Advised) {
Advised x = (Advised) proxy;
for (Class<?> foo : x.getProxiedInterfaces()) {
for (Type generic : foo.getGenericInterfaces()) {
if (generic instanceof ParameterizedType) {
Class<?> paramClass = (Class<?>) ((ParameterizedType) generic).getActualTypeArguments()[0];
if (BasicModel.class.isAssignableFrom(paramClass)) {
LOG.trace("Regenerating ES for {}", paramClass);
elasticsearchService.reindex(paramClass);
}
}
}
}
}
} catch (Throwable e) {
LOG.error(e.getMessage(), e);
}
}
/**
* Flatten collections (if Iterable) and check each object class if indexed
*
......
......@@ -89,8 +89,8 @@ public class ChartsController extends BaseController {
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
// Load all term results
TermResult countryStatistics = elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(
FilterConstants.INSTITUTE_COUNTRY_ISO2), Integer.MAX_VALUE);
TermResult countryStatistics = elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), Integer.MAX_VALUE, AppliedFiltersConverter.convertTerm(
FilterConstants.INSTITUTE_COUNTRY_ISO2));
List<Map<String, Object>> resultList = new ArrayList<>();
......
......@@ -241,7 +241,7 @@ public class ExplorerController extends BaseController implements InitializingBe
AppliedFilters tempFilters = new AppliedFilters(storedTransformedFilters);
final GenesysFilter finalFilter = filter;
tempFilters.removeIf(appliedFilter -> appliedFilter.getKey().equals(finalFilter.getKey()));
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), AppliedFiltersConverter.convertTerm(filter.getKey()), 30).getTerms()) {
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), 30, AppliedFiltersConverter.convertTerm(filter.getKey())).getTerms()) {
options.add(Integer.valueOf(term.getTerm()));
counts.add(term.getCount());
}
......@@ -257,7 +257,7 @@ public class ExplorerController extends BaseController implements InitializingBe
tempFilters = new AppliedFilters(storedTransformedFilters);
final GenesysFilter finalFilter2 = filter;
tempFilters.removeIf(appliedFilter -> appliedFilter.getKey().equals(finalFilter2.getKey()));
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), AppliedFiltersConverter.convertTerm(filter.getKey()), 30).getTerms()) {
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), 30, AppliedFiltersConverter.convertTerm(filter.getKey())).getTerms()) {
options.add(Integer.valueOf(term.getTerm()));
counts.add(term.getCount());
}
......@@ -329,7 +329,7 @@ public class ExplorerController extends BaseController implements InitializingBe
appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
model.addAttribute("appliedFilters", getAppliedFilters(tempFilters));
model.addAttribute("accessionCount", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE), 10).getTotal());
model.addAttribute("accessionCount", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 10, AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE)).getTotal());
overviewInstitutes(model, appliedFilters);
overviewComposition(model, appliedFilters);
overviewAvailability(model, appliedFilters);
......@@ -385,20 +385,20 @@ public class ExplorerController extends BaseController implements InitializingBe
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
Map<String, TermResult> pageData = new HashMap<>();
pageData.put("statsInstCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE), 20));
pageData.put("statsInstCountry", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3), 20));
pageData.put("statsOrgCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3), 20));
pageData.put("statsDonorCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.DONORCODE), 20));
pageData.put("statsMLS", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.MLSSTATUS), 2));
pageData.put("statsAvailable", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.AVAILABLE), 2));
pageData.put("statsHistoric", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.HISTORIC), 2));
pageData.put("statsStorage", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.STORAGE), 30));
pageData.put("statsDuplSite", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.DUPLSITE), 20));
pageData.put("statsSGSV", elasticService.termStatistics(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.SGSV), 2));
pageData.put("statsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS), 20));
pageData.put("statsSpecies", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUSSPECIES), 20));
pageData.put("statsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.CROPS), 30));
pageData.put("statsSampStat", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.SAMPSTAT), 30));
pageData.put("statsInstCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE)));
pageData.put("statsInstCountry", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3)));
pageData.put("statsOrgCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3)));
pageData.put("statsDonorCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.DONORCODE)));
pageData.put("statsMLS", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.MLSSTATUS)));
pageData.put("statsAvailable", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.AVAILABLE)));
pageData.put("statsHistoric", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.HISTORIC)));
pageData.put("statsStorage", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.STORAGE)));
pageData.put("statsDuplSite", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.DUPLSITE)));
pageData.put("statsSGSV", elasticService.termStatistics(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.SGSV)));
pageData.put("statsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS)));
pageData.put("statsSpecies", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUSSPECIES)));
pageData.put("statsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.CROPS)));
pageData.put("statsSampStat", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.SAMPSTAT)));
return pageData;
}
......@@ -422,7 +422,7 @@ public class ExplorerController extends BaseController implements InitializingBe
List<Long> counts = new ArrayList<>();
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
appliedFilters.removeIf(appliedFilter -> appliedFilter.getKey().equals(filter.getKey()));
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(filter.getKey()), 30).getTerms()) {
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(filter.getKey())).getTerms()) {
options.add(Integer.valueOf(term.getTerm()));
counts.add(term.getCount());
}
......@@ -466,7 +466,7 @@ public class ExplorerController extends BaseController implements InitializingBe
if (FilterConstants.HISTORIC.equals(filter.getKey())) {
// Handle this
}
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(filter.getKey()), 30).getTerms()) {
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(filter.getKey())).getTerms()) {
counts.put(term.getTerm(), term.getCount());
}
booleanFilters.put(filter.getKey(), counts);
......@@ -488,7 +488,7 @@ public class ExplorerController extends BaseController implements InitializingBe
AppliedFilters tempFilters = new AppliedFilters(originalFilters);
tempFilters.removeIf(appliedFilter -> appliedFilter.getKey().equals(FilterConstants.CROPS));
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), AppliedFiltersConverter.convertTerm(FilterConstants.CROPS), 30).getTerms()) {
for (Term term : elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(tempFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.CROPS)).getTerms()) {
String cropShortName = term.getTerm();
Crop crop = cropService.getCrop(cropShortName);
Map<String, Long> values = new HashMap<>();
......@@ -620,63 +620,6 @@ public class ExplorerController extends BaseController implements InitializingBe
return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
/**
* Browse all using Elasticsearch
*
* @param model
* @param page
* @return
* @throws IOException
* @throws SearchException
*/
@RequestMapping("/explore-es")
public String viewElasticFiltered(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "filter", required = true, defaultValue = "{}") String jsonFilter) throws IOException, SearchException {
String[] selectedFilters = null;
LOG.debug("Filtering by: {}", jsonFilter);
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
Crop crop = null;
{
String shortName = appliedFilters.getFirstLiteralValue(FilterConstants.CROPS, String.class);
if (shortName != null)
crop = cropService.getCrop((String) shortName);
if (crop != null) {
// Keep only one crop
AppliedFilter af = appliedFilters.get(FilterConstants.CROPS);
af.getValues().clear();
af.addFilterValue(new FilterHandler.LiteralValueFilter(crop.getShortName()));
}
}
model.addAttribute("crop", crop);
// JSP works with JsonObject // TODO Handle -filter.key!!
final Map<?, ?> filters = mapper.readValue(appliedFilters.toString(), Map.class);
model.addAttribute("filters", filters);
selectedFilters = appliedFilters.getFilterNames();
final List<GenesysFilter> currentFilters = filterHandler.selectFilters(selectedFilters);
final List<GenesysFilter> availableFilters = filterHandler.listAvailableFilters();
LOG.info(appliedFilters.toString());
model.addAttribute("jsonFilter", appliedFilters.toString());
final Page<Accession> accessions = elasticService.find(Accession.class, AccessionFilter.convert(appliedFilters), new PageRequest(page - 1, 50, new Sort("seqNo")));
LOG.info("Got: {}", accessions);
model.addAttribute("crops", cropService.list(getLocale()));
model.addAttribute("pagedData", accessions);
model.addAttribute("appliedFilters", appliedFilters);
model.addAttribute("currentFilters", currentFilters);
model.addAttribute("availableFilters", availableFilters);
return "/accession/explore-es";
}
@RequestMapping(value = "/additional-filter", method = RequestMethod.GET)
public String getAdditionalFilters(ModelMap model, @RequestParam(value = "filter", required = true, defaultValue = "") String[] selectedFilters)
throws IOException {
......@@ -910,35 +853,35 @@ public class ExplorerController extends BaseController implements InitializingBe
private void overviewInstitutes(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsInstCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE), 20));
model.addAttribute("statsInstCountry", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3), 20));
model.addAttribute("statsInstCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE)));
model.addAttribute("statsInstCountry", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3)));
}
private void overviewSources(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsOrgCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3), 20));
model.addAttribute("statsDonorCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.DONORCODE), 20));
model.addAttribute("statsOrgCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3)));
model.addAttribute("statsDonorCode", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.DONORCODE)));
}
private void overviewAvailability(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsMLS", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.MLSSTATUS), 2));
model.addAttribute("statsAvailable", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.AVAILABLE), 2));
model.addAttribute("statsMLS", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.MLSSTATUS)));
model.addAttribute("statsAvailable", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.AVAILABLE)));
}
private void overviewHistoric(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsHistoric", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.HISTORIC), 2));
model.addAttribute("statsHistoric", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.HISTORIC)));
}
private void overviewManagement(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsStorage", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.STORAGE), 30));
model.addAttribute("statsDuplSite", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.DUPLSITE), 20));
model.addAttribute("statsSGSV", elasticService.termStatistics(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.SGSV), 2));
model.addAttribute("statsStorage", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.STORAGE)));
model.addAttribute("statsDuplSite", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.DUPLSITE)));
model.addAttribute("statsSGSV", elasticService.termStatistics(Accession.class, AccessionFilter.convert(appliedFilters), 2, AppliedFiltersConverter.convertTerm(FilterConstants.SGSV)));
}
private void overviewComposition(ModelMap model, AppliedFilters appliedFilters) throws SearchException {
model.addAttribute("statsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS), 20));
model.addAttribute("statsSpecies", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUSSPECIES), 20));
model.addAttribute("statsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.CROPS), 30));
model.addAttribute("statsSampStat", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), AppliedFiltersConverter.convertTerm(FilterConstants.SAMPSTAT), 30));
model.addAttribute("statsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS)));
model.addAttribute("statsSpecies", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 20, AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUSSPECIES)));
model.addAttribute("statsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.CROPS)));
model.addAttribute("statsSampStat", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(appliedFilters), 30, AppliedFiltersConverter.convertTerm(FilterConstants.SAMPSTAT)));
}
@RequestMapping(value = "/explore/shorten-url", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
......
......@@ -119,10 +119,10 @@ public class ProjectController extends BaseController {
model.addAttribute("jsonFilter", filters.toString());
try {
model.addAttribute("statisticsCrop", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), FilterConstants.CROPS, 5));
model.addAttribute("statisticsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), FilterConstants.TAXONOMY_GENUS, 5));
model.addAttribute("statisticsTaxonomy", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), FilterConstants.TAXONOMY_GENUSSPECIES, 5));
model.addAttribute("statisticsOrigCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), FilterConstants.ORGCTY_ISO3, 100));
model.addAttribute("statisticsCrop", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), 5, FilterConstants.CROPS));
model.addAttribute("statisticsGenus", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), 5, FilterConstants.TAXONOMY_GENUS));
model.addAttribute("statisticsTaxonomy", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), 5, FilterConstants.TAXONOMY_GENUSSPECIES));
model.addAttribute("statisticsOrigCty", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(filters), 100, FilterConstants.ORGCTY_ISO3));
} catch (SearchException e) {
LOG.warn(e.getMessage());
}
......
......@@ -72,7 +72,7 @@ public class SearchController {
if (!StringUtils.isBlank(searchQuery)) {
try {
List<Accession> x = searchService.search(null, Accession.class, searchQuery);
List<Accession> x = searchService.search(null, searchQuery, Accession.class);
model.addAttribute("pagedData", x);
LOG.info("Searching for: {} returns {}", searchQuery, x.size());
} catch (Throwable e) {
......@@ -158,15 +158,15 @@ public class SearchController {
try {
// FIXME these are note Page<?> but List<?>
if ("accession".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, Accession.class, searchQuery));
model.addAttribute("pagedData", searchService.search(null, searchQuery, Accession.class));
} else if ("institute".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, FaoInstitute.class, searchQuery));
model.addAttribute("pagedData", searchService.search(null, searchQuery, FaoInstitute.class));
} else if ("country".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, Country.class, searchQuery));
model.addAttribute("pagedData", searchService.search(null, searchQuery, Country.class));
} else if ("article".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, Article.class, searchQuery));
model.addAttribute("pagedData", searchService.search(null, searchQuery, Article.class));
} else if ("activitypost".equals(section)) {
model.addAttribute("pagedData", searchService.search(null, ActivityPost.class, searchQuery));
model.addAttribute("pagedData", searchService.search(null, searchQuery, ActivityPost.class));
} else {
LOG.error("Unsupported full-text searchfor section: {}", section);
throw new UnsupportedOperationException("No such search section");
......
......@@ -185,7 +185,7 @@ public class WiewsController extends BaseController {
instituteFilter.add(new AppliedFilter().setFilterName(FilterConstants.INSTCODE).addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode())));
try {
model.addAttribute("statisticsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(instituteFilter), AppliedFiltersConverter.convertTerm(FilterConstants.CROPS), 5));
model.addAttribute("statisticsCrops", elasticService.termStatisticsAuto(Accession.class, AccessionFilter.convert(instituteFilter), 5, AppliedFiltersConverter.convertTerm(FilterConstants.CROPS)));
} catch (SearchException e) {
LOG.warn(e.getMessage());
}
......
......@@ -25,8 +25,6 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
/**
* The Interface ElasticsearchService.
......@@ -71,34 +69,36 @@ public interface ElasticsearchService {
* Search single entity.
*
* @param shouldMatch the should match
* @param clazz the clazz
* @param searchQuery the search query
* @param clazz the clazz
* @return the list
*/
<T extends BasicModel> List<T> search(QueryBuilder shouldMatch, Class<T> clazz, String searchQuery);
<T extends BasicModel> List<T> search(QueryBuilder shouldMatch, String searchQuery, Class<T> clazz);
/**
* Search using query for selected entity types.
*
* @param shouldMatch Any additional filters that should match
* @param clazzes the clazzes
* @param searchQuery the search query
* @param clazzes the clazzes
* @return the map
*/
<T extends BasicModel> Map<Class<BasicModel>, List<T>> search(QueryBuilder shouldMatch, Set<Class<?>> clazzes, String searchQuery);
<T extends BasicModel> Map<Class<? extends BasicModel>, List<? extends BasicModel>> search(QueryBuilder shouldMatch, String searchQuery, Set<Class<? extends BasicModel>> clazzes);
/**
* Term statistics auto.
*
* @param filters the filters
* @param instcode the instcode
* @param i the i
*
* @return the term result
* @throws SearchException the search exception
*/
TermResult termStatisticsAuto(Class<?> clazz, BasicModelFilter filters, String term, int size) throws SearchException;
TermResult termStatisticsAuto(Class<?> clazz, BasicModelFilter filters, int size, String term) throws SearchException;
Map<String, TermResult> termStatisticsAuto(Class<?> clazz, BasicModelFilter filters, int size, String... terms) throws SearchException;
TermResult termStatistics(Class<?> clazz, BasicModelFilter filters, String term, int size) throws SearchException;
TermResult termStatistics(Class<?> clazz, BasicModelFilter filters, int size, String term) throws SearchException;
Map<String, TermResult> termStatistics(Class<?> clazz, BasicModelFilter filters, int size, String... terms) throws SearchException;
void realias(String aliasName, String indexName);
......@@ -108,7 +108,18 @@ public interface ElasticsearchService {
void deleteIndex(String indexName);
void reindexAll();
<T extends BasicModel> void reindex(Class<T> clazz, BasicModelFilter filter);
List<Class<? extends BasicModel>> getIndexedEntities();
long count(Class<? extends BasicModel> clazz, BasicModelFilter filter);
public static class TermResult {
private List<Term> terms;
......@@ -116,9 +127,9 @@ public interface ElasticsearchService {
private long other;
private Long missing;
public TermResult(String name, List<Term> terms, Long total, long other, Long missing) {
public TermResult(String name, List<Term> terms, long other, Long missing) {
this.terms = terms;
this.total = total;
this.total = (long) terms.size();
this.other = other;
this.missing = missing;
}
......@@ -164,14 +175,5 @@ public interface ElasticsearchService {
}
}
<T> Page<T> find(Class<T> clazz, BasicModelFilter filters, Pageable page);
void reindexAll();
<T extends BasicModel> void reindex(Class<T> clazz, BasicModelFilter filter);
List<Class<? extends BasicModel>> getIndexedEntities();
long count(Class<? extends BasicModel> clazz, BasicModelFilter filter);
<T extends BasicModel> List<T> find(Class<T> clazz, BasicModelFilter filter);
}
......@@ -34,9 +34,9 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.time.StopWatch;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasOrIndex;
......@@ -66,7 +66,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
......@@ -80,8 +79,6 @@ import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -97,7 +94,7 @@ import com.querydsl.jpa.JPQLQuery;
* @author Matija Obreza