Skip to content

Commit

Permalink
#635 additional facets + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Oct 30, 2024
1 parent cdce1bb commit 86d3c2d
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@
import org.gbif.api.model.collections.Address;
import org.gbif.api.model.collections.AlternativeCode;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.descriptors.DescriptorGroup;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.License;
import org.gbif.api.vocabulary.collections.CollectionFacetParameter;
import org.gbif.api.vocabulary.collections.InstitutionFacetParameter;
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.persistence.mapper.collections.AddressMapper;
import org.gbif.registry.persistence.mapper.collections.CollectionMapper;
import org.gbif.registry.persistence.mapper.collections.CollectionsSearchMapper;
import org.gbif.registry.persistence.mapper.collections.DescriptorsMapper;
import org.gbif.registry.persistence.mapper.collections.InstitutionMapper;
import org.gbif.registry.persistence.mapper.collections.dto.CollectionSearchDto;
import org.gbif.registry.persistence.mapper.collections.dto.DescriptorDto;
import org.gbif.registry.persistence.mapper.collections.dto.FacetDto;
import org.gbif.registry.persistence.mapper.collections.params.DescriptorsListParams;
import org.gbif.registry.persistence.mapper.collections.params.InstitutionListParams;
import org.gbif.registry.search.test.EsManageServer;
import org.gbif.registry.ws.it.BaseItTest;
import org.gbif.ws.client.filter.SimplePrincipalProvider;
Expand All @@ -53,20 +57,23 @@ public class CollectionsSearchMapperIT extends BaseItTest {
new TestCaseDatabaseInitializer("collection");

private CollectionMapper collectionMapper;
private InstitutionMapper institutionMapper;
private AddressMapper addressMapper;
private DescriptorsMapper descriptorsMapper;
private CollectionsSearchMapper collectionsSearchMapper;

@Autowired
public CollectionsSearchMapperIT(
CollectionMapper collectionMapper,
InstitutionMapper institutionMapper,
AddressMapper addressMapper,
DescriptorsMapper descriptorsMapper,
CollectionsSearchMapper collectionsSearchMapper,
SimplePrincipalProvider principalProvider,
EsManageServer esServer) {
super(principalProvider, esServer);
this.collectionMapper = collectionMapper;
this.institutionMapper = institutionMapper;
this.addressMapper = addressMapper;
this.descriptorsMapper = descriptorsMapper;
this.collectionsSearchMapper = collectionsSearchMapper;
Expand Down Expand Up @@ -168,6 +175,43 @@ public void searchTest() {

@Test
public void facetsTest() {
UUID i1Key = UUID.randomUUID();
Institution i1 = new Institution();
i1.setKey(i1Key);
i1.setName("i1");
i1.setCode("i1");
i1.setTypes(Arrays.asList("ty1", "ty2"));
i1.setDisciplines(Arrays.asList("di1", "di2"));
i1.setCreatedBy("test");
i1.setModifiedBy("test");

Address ai1 = new Address();
ai1.setCountry(Country.SPAIN);
ai1.setCity("Oviedo");
addressMapper.create(ai1);
assertNotNull(ai1.getKey());
i1.setAddress(ai1);

institutionMapper.create(i1);

UUID i2Key = UUID.randomUUID();
Institution i2 = new Institution();
i2.setKey(i2Key);
i2.setName("i2");
i2.setCode("i2");
i2.setTypes(Collections.singletonList("ty1"));
i2.setCreatedBy("test");
i2.setModifiedBy("test");

Address ai2 = new Address();
ai2.setCountry(Country.SPAIN);
ai2.setCity("Bilbao");
addressMapper.create(ai2);
assertNotNull(ai2.getKey());
i2.setMailingAddress(ai2);

institutionMapper.create(i2);

UUID c1Key = UUID.randomUUID();
Collection c1 = new Collection();
c1.setKey(c1Key);
Expand All @@ -184,6 +228,7 @@ public void facetsTest() {

Address address = new Address();
address.setCountry(Country.SPAIN);
address.setCity("Oviedo");
addressMapper.create(address);
assertNotNull(address.getKey());
c1.setAddress(address);
Expand All @@ -207,13 +252,15 @@ public void facetsTest() {
descriptorDto1.setDescriptorGroupKey(descriptorGroup.getKey());
descriptorDto1.setUsageName("aves");
descriptorDto1.setCountry(Country.DENMARK);
descriptorDto1.setRecordedBy(Arrays.asList("John", "Clint"));
descriptorDto1.setKingdomKey(1);
descriptorsMapper.createDescriptor(descriptorDto1);

DescriptorDto descriptorDto2 = new DescriptorDto();
descriptorDto2.setDescriptorGroupKey(descriptorGroup.getKey());
descriptorDto2.setKingdomKey(1);
descriptorDto2.setCountry(Country.DENMARK);
descriptorDto2.setObjectClassificationName("obn1");
descriptorsMapper.createDescriptor(descriptorDto2);

UUID c2Key = UUID.randomUUID();
Expand All @@ -233,6 +280,7 @@ public void facetsTest() {

mailingAddress = new Address();
mailingAddress.setCountry(Country.SPAIN);
mailingAddress.setCity("Bilbao");
addressMapper.create(mailingAddress);
assertNotNull(mailingAddress.getKey());
c2.setMailingAddress(mailingAddress);
Expand All @@ -249,7 +297,9 @@ public void facetsTest() {
DescriptorDto descriptorDtoC2 = new DescriptorDto();
descriptorDtoC2.setDescriptorGroupKey(descriptorGroupC2.getKey());
descriptorDtoC2.setKingdomKey(2);
descriptorDtoC2.setRecordedBy(Collections.singletonList("John"));
descriptorDtoC2.setCountry(Country.DENMARK);
descriptorDtoC2.setObjectClassificationName("obn1");
descriptorsMapper.createDescriptor(descriptorDtoC2);

List<FacetDto> facetDtos =
Expand Down Expand Up @@ -305,5 +355,87 @@ public void facetsTest() {
collectionsSearchMapper.collectionFacet(
DescriptorsListParams.builder().facet(CollectionFacetParameter.TYPE_STATUS).build());
assertEquals(0, facetDtos.size());

assertEquals(
0,
collectionsSearchMapper.collectionFacetCardinality(
DescriptorsListParams.builder().facet(CollectionFacetParameter.TYPE_STATUS).build()));

facetDtos =
collectionsSearchMapper.collectionFacet(
DescriptorsListParams.builder().facet(CollectionFacetParameter.CITY).build());
assertEquals(2, facetDtos.size());
assertEquals(2, facetDtos.stream().filter(f -> f.getCount() == 1).count());
assertEquals(
2,
collectionsSearchMapper.collectionFacetCardinality(
DescriptorsListParams.builder().facet(CollectionFacetParameter.CITY).build()));

facetDtos =
collectionsSearchMapper.collectionFacet(
DescriptorsListParams.builder().facet(CollectionFacetParameter.RECORDED_BY).build());
assertEquals(2, facetDtos.size());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 1).count());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 2).count());
assertEquals(
2,
collectionsSearchMapper.collectionFacetCardinality(
DescriptorsListParams.builder().facet(CollectionFacetParameter.RECORDED_BY).build()));

facetDtos =
collectionsSearchMapper.collectionFacet(
DescriptorsListParams.builder()
.facet(CollectionFacetParameter.OBJECT_CLASSIFICATION)
.build());
assertEquals(1, facetDtos.size());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 2).count());
assertEquals(
1,
collectionsSearchMapper.collectionFacetCardinality(
DescriptorsListParams.builder()
.facet(CollectionFacetParameter.OBJECT_CLASSIFICATION)
.build()));

// institution facets
facetDtos =
collectionsSearchMapper.institutionFacet(
InstitutionListParams.builder().facet(InstitutionFacetParameter.COUNTRY).build());
assertEquals(1, facetDtos.size());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 2).count());
assertEquals(
1,
collectionsSearchMapper.institutionFacetCardinality(
InstitutionListParams.builder().facet(InstitutionFacetParameter.COUNTRY).build()));

facetDtos =
collectionsSearchMapper.institutionFacet(
InstitutionListParams.builder().facet(InstitutionFacetParameter.CITY).build());
assertEquals(2, facetDtos.size());
assertEquals(2, facetDtos.stream().filter(f -> f.getCount() == 1).count());
assertEquals(
2,
collectionsSearchMapper.institutionFacetCardinality(
InstitutionListParams.builder().facet(InstitutionFacetParameter.CITY).build()));

facetDtos =
collectionsSearchMapper.institutionFacet(
InstitutionListParams.builder().facet(InstitutionFacetParameter.TYPE).build());
assertEquals(2, facetDtos.size());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 2).count());
assertEquals(1, facetDtos.stream().filter(f -> f.getCount() == 1).count());
assertEquals(
2,
collectionsSearchMapper.institutionFacetCardinality(
InstitutionListParams.builder().facet(InstitutionFacetParameter.TYPE).build()));

facetDtos =
collectionsSearchMapper.institutionFacet(
InstitutionListParams.builder().facet(InstitutionFacetParameter.DISCIPLINE).build());
assertEquals(2, facetDtos.size());
assertEquals(2, facetDtos.stream().filter(f -> f.getCount() == 1).count());
assertEquals(
2,
collectionsSearchMapper.institutionFacetCardinality(
InstitutionListParams.builder().facet(InstitutionFacetParameter.DISCIPLINE).build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public boolean descriptorFacet() {
|| facet == CollectionFacetParameter.FAMILY_KEY
|| facet == CollectionFacetParameter.GENUS_KEY
|| facet == CollectionFacetParameter.SPECIES_KEY
|| facet == CollectionFacetParameter.TYPE_STATUS;
|| facet == CollectionFacetParameter.TYPE_STATUS
|| facet == CollectionFacetParameter.RECORDED_BY
|| facet == CollectionFacetParameter.OBJECT_CLASSIFICATION;
}

public boolean descriptorSearch() {
Expand Down Expand Up @@ -75,4 +77,11 @@ public boolean descriptorSearchWithoutQuery() {
|| objectClassification != null
|| issues != null;
}

public boolean isArrayFieldFacet() {
return facet == CollectionFacetParameter.PRESERVATION_TYPE
|| facet == CollectionFacetParameter.CONTENT_TYPE
|| facet == CollectionFacetParameter.TYPE_STATUS
|| facet == CollectionFacetParameter.RECORDED_BY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class InstitutionListParams extends ListParams {

// facets
@Nullable InstitutionFacetParameter facet;

public boolean isArrayFieldFacet() {
return facet == InstitutionFacetParameter.TYPE || facet == InstitutionFacetParameter.DISCIPLINE;
}
}
Loading

0 comments on commit 86d3c2d

Please sign in to comment.