Commit 95b33002 authored by Matija Obreza's avatar Matija Obreza Committed by Maxym Borodenko
Browse files

TaxonomyFilter: Updates for OR operator in Elasticsearch

parent c22ffcd9
......@@ -80,7 +80,6 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
public InstituteFilter institute;
/** The acce numb. */
@Deprecated
public StringFilter accessionNumber;
/** The accession numbers. */
......@@ -545,7 +544,6 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/**
* Accession Number filter.
*
* @deprecated Will be removed soon.
* @return the string filter
*/
public synchronized StringFilter accessionNumber() {
......
......@@ -46,7 +46,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
/** The species. */
public Set<String> genusSpecies;
/** The subtaxa. */
public StringFilter subtaxa;
......@@ -61,7 +61,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
public List<Predicate> collectPredicates() {
return collectPredicates(QTaxonomy2.taxonomy2);
}
/**
* Builds the query.
*
......@@ -73,14 +73,17 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
final TaxonomyService taxonomyService = CurrentApplicationContext.getContext().getBean(TaxonomyService.class);
if (CollectionUtils.isNotEmpty(genus) && taxonomyService != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
// We need to look up the taxonomy2.id for provided names so that ES queries
// also work out of the box
List<Long> taxGenus = taxonomyService.findTaxonomy2GenusId(new ArrayList<>(genus));
List<Long> grinGenus = taxonomyService.findGrinGenusId(new ArrayList<>(genus));
BooleanBuilder builder = new BooleanBuilder(taxonomy.taxGenus.in(taxGenus));
builder.or(taxonomy.grinTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
if (synonyms == null || synonyms) {
builder.or(taxonomy.currentTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
if (!grinGenus.isEmpty()) {
builder.or(taxonomy.grinTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
if (synonyms == null || synonyms) {
builder.or(taxonomy.currentTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
}
}
predicates.add(builder);
}
......@@ -109,14 +112,17 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
predicates.add(builder);
}
if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyService != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
// We need to look up the taxonomy2.id for provided names so that ES queries
// also work out of the box
List<Long> taxSpecies = taxonomyService.findTaxonomy2SpeciesId(new ArrayList<>(genusSpecies));
List<Long> grinSpecies = taxonomyService.findGrinSpeciesId(new ArrayList<>(genusSpecies));
BooleanBuilder builder = new BooleanBuilder(taxonomy.taxSpecies.in(taxSpecies));
builder.or(taxonomy.grinTaxonomySpecies.id.in(grinSpecies));
if (synonyms == null || synonyms) {
builder.or(taxonomy.currentTaxonomySpecies.id.in(grinSpecies));
if (!grinSpecies.isEmpty()) {
builder.or(taxonomy.grinTaxonomySpecies.id.in(grinSpecies));
if (synonyms == null || synonyms) {
builder.or(taxonomy.currentTaxonomySpecies.id.in(grinSpecies));
}
}
predicates.add(builder);
}
......
......@@ -228,12 +228,55 @@ public class ElasticQueryBuilderTest extends AbstractElasticServiceTest {
@Test
public void testOverviewSubtaxa() throws SearchException, JsonProcessingException {
public void testOverviewByTaxonomyFilter() throws SearchException, JsonProcessingException {
AccessionFilter af = new AccessionFilter();
TermResult overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(6L));
af.taxa().genus = Sets.newHashSet("Beta");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "taxonomy.genus");
assertThat(overview.getTotal(), is(2L));
af.taxa().species = Sets.newHashSet("vulgaris");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "taxonomy.genus");
assertThat(overview.getTotal(), is(2L));
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(2L));
af.taxa().subtaxa().contains("cicla");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(1L));
// Contains
af.taxa().subtaxa().contains = null;
af.taxa().subtaxa().contains("var. flavescens");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(1L));
// Contains
af.taxa().subtaxa().contains = null;
af.taxa().subtaxa().contains("flavescens");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(1L));
}
@Test
public void testOverviewSubtaxa() throws SearchException, JsonProcessingException {
AccessionFilter af = new AccessionFilter();
af.taxa().genus = Sets.newHashSet("Beta");
af.taxa().species = Sets.newHashSet("vulgaris");
TermResult overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(2L));
af.taxa().species = null;
af.taxa().subtaxa().contains("cicla");
overview = elasticsearchService.termStatisticsAuto(Accession.class, af, 10, "institute.code");
assertThat(overview.getTotal(), is(1L));
// Contains
......
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