Commit b37efc7f authored by Maxym Borodenko's avatar Maxym Borodenko

Accession filters autocomplete

parent c40f004d
......@@ -43,16 +43,19 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.AppliedFiltersConverter;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -111,6 +114,9 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private TaxonomyService taxonomyService;
@Autowired
private GeoService geoService;
private static final int LOAD_CHUNK_SIZE = 200;
private <T extends AccessionData> T lazyLoad(T accession) {
......@@ -379,11 +385,11 @@ public class AccessionServiceImpl implements AccessionService {
}
final List<LabelValue<String>> completed = new ArrayList<>();
if (FilterConstants.INSTCODE.equalsIgnoreCase(field)) {
ElasticsearchService.TermResult termResult = null;
if (AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE).equalsIgnoreCase(field)) {
final List<FaoInstitute> faoInst = instituteService.autocomplete(text);
ElasticsearchService.TermResult termResult = null;
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, FilterConstants.INSTCODE);
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, AppliedFiltersConverter.convertTerm(FilterConstants.INSTCODE));
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
......@@ -391,38 +397,74 @@ public class AccessionServiceImpl implements AccessionService {
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (FaoInstitute inst : faoInst) {
String label = inst.getCode() + ", " + inst.getFullName();
for (ElasticsearchService.Term term : termResult.getTerms()) {
if (term.getTerm().equalsIgnoreCase(inst.getCode()) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<>(inst.getCode(), label));
break;
}
LabelValue<String> suggestion = getSuggestion(termResult.getTerms(), label, inst.getCode());
if (suggestion != null) {
completed.add(suggestion);
}
}
}
} else if (FilterConstants.TAXONOMY_GENUS.equalsIgnoreCase(field)) {
} else if (AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS).equalsIgnoreCase(field)) {
final List<String> genusList = taxonomyService.autocompleteGenus(text, null);
ElasticsearchService.TermResult termResult = null;
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, FilterConstants.TAXONOMY_GENUS);
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000,AppliedFiltersConverter.convertTerm(FilterConstants.TAXONOMY_GENUS));
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (String genus : genusList) {
for (ElasticsearchService.Term term : termResult.getTerms()) {
if (term.getTerm().equalsIgnoreCase(genus) && term.getCount() > 0) {
completed.add(new LabelValue<>(genus, genus.concat(" (" + term.getCount() + ")")));
break;
}
LabelValue<String> suggestion = getSuggestion(termResult.getTerms(), genus, genus);
if (suggestion != null) {
completed.add(suggestion);
}
}
}
} else if (AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3).equalsIgnoreCase(field)) {
final List<Country> countries = geoService.autocomplete(text);
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, AppliedFiltersConverter.convertTerm(FilterConstants.ORGCTY_ISO3));
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
completed.addAll(getCountrySuggestions(termResult, countries));
} else if (AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3).equalsIgnoreCase(field)) {
final List<Country> countries = geoService.autocomplete(text);
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3));
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
completed.addAll(getCountrySuggestions(termResult, countries));
}
return completed;
}
private List<LabelValue<String>> getCountrySuggestions(ElasticsearchService.TermResult termResult, List<Country> countries) {
final List<LabelValue<String>> result = new ArrayList<>();
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (Country c : countries) {
String label = c.getCode3() + ", " + c.getName();
LabelValue<String> suggestion = getSuggestion(termResult.getTerms(), label, c.getCode3());
if (suggestion != null) {
result.add(suggestion);
}
}
}
return result;
}
private LabelValue<String> getSuggestion(List<ElasticsearchService.Term> terms, String label, String value) {
for (ElasticsearchService.Term term : terms) {
if (term.getTerm().equalsIgnoreCase(value) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
return new LabelValue<>(value, label);
}
}
return null;
}
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException {
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
......
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