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