Skip to content

Commit

Permalink
#517 sort by numbers of specimens for collections and institutions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Sep 4, 2023
1 parent 2bc33b5 commit 4761789
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<spring-cloud-sleuth.version>2.2.8.RELEASE</spring-cloud-sleuth.version>

<!-- GBIF -->
<gbif-api.version>1.11.0</gbif-api.version>
<gbif-api.version>1.12.1-SNAPSHOT</gbif-api.version>
<gbif-common.version>0.59</gbif-common.version>
<gbif-common-mybatis.version>1.3</gbif-common-mybatis.version>
<gbif-common-ws.version>1.25</gbif-common-ws.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import org.gbif.api.service.registry.InstallationService;
import org.gbif.api.service.registry.NodeService;
import org.gbif.api.service.registry.OrganizationService;
import org.gbif.api.vocabulary.CollectionsSortField;
import org.gbif.api.vocabulary.ContactType;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.GbifRegion;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.SortOrder;
import org.gbif.api.vocabulary.collections.AccessionStatus;
import org.gbif.api.vocabulary.collections.CollectionContentType;
import org.gbif.api.vocabulary.collections.IdType;
Expand Down Expand Up @@ -458,6 +460,33 @@ public void listTest() {
.getResults()
.size());

assertEquals(
collection1.getKey(),
collectionService
.list(
CollectionSearchRequest.builder()
.sortBy(CollectionsSortField.NUMBER_SPECIMENS)
.page(DEFAULT_PAGE)
.build())
.getResults()
.get(0)
.getCollection()
.getKey());

assertEquals(
collection2.getKey(),
collectionService
.list(
CollectionSearchRequest.builder()
.sortBy(CollectionsSortField.NUMBER_SPECIMENS)
.sortOrder(SortOrder.DESC)
.page(DEFAULT_PAGE)
.build())
.getResults()
.get(0)
.getCollection()
.getKey());

collectionService.delete(key2);
assertEquals(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gbif.api.model.collections.Institution;
import org.gbif.api.model.collections.MasterSourceMetadata;
import org.gbif.api.model.collections.UserId;
import org.gbif.api.model.collections.request.CollectionSearchRequest;
import org.gbif.api.model.collections.request.InstitutionSearchRequest;
import org.gbif.api.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
Expand All @@ -29,10 +30,12 @@
import org.gbif.api.service.registry.InstallationService;
import org.gbif.api.service.registry.NodeService;
import org.gbif.api.service.registry.OrganizationService;
import org.gbif.api.vocabulary.CollectionsSortField;
import org.gbif.api.vocabulary.ContactType;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.GbifRegion;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.SortOrder;
import org.gbif.api.vocabulary.collections.Discipline;
import org.gbif.api.vocabulary.collections.IdType;
import org.gbif.api.vocabulary.collections.InstitutionGovernance;
Expand Down Expand Up @@ -388,6 +391,31 @@ public void listTest() {
.getResults()
.size());

assertEquals(
institution1.getKey(),
institutionService
.list(
InstitutionSearchRequest.builder()
.sortBy(CollectionsSortField.NUMBER_SPECIMENS)
.page(DEFAULT_PAGE)
.build())
.getResults()
.get(0)
.getKey());

assertEquals(
institution2.getKey(),
institutionService
.list(
InstitutionSearchRequest.builder()
.sortBy(CollectionsSortField.NUMBER_SPECIMENS)
.sortOrder(SortOrder.DESC)
.page(DEFAULT_PAGE)
.build())
.getResults()
.get(0)
.getKey());

institutionService.delete(key2);
assertEquals(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package org.gbif.registry.persistence.mapper.collections.params;

import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.vocabulary.CollectionsSortField;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.SortOrder;
import org.gbif.api.vocabulary.collections.MasterSourceType;

import java.util.List;
Expand Down Expand Up @@ -51,5 +53,9 @@ public abstract class SearchParams {
@Nullable private UUID replacedBy;
@Nullable RangeParam occurrenceCount;
@Nullable RangeParam typeSpecimenCount;
@Nullable
CollectionsSortField sortBy;
@Nullable
SortOrder sortOrder;
@Nullable private Pageable page;
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,36 @@

<!-- Basic paging search functionality -->
<select id="list" resultType="CollectionDto" resultMap="COLLECTION_DTO_MAP" parameterType="Pageable">
SELECT DISTINCT ON (<if test="params.query != null" >ts_rank_cd(c.fulltext_search, query), </if>
<if test="params.fuzzyName != null">similarity_score, </if>
c.created, c.key)
<include refid="COLLECTION_READ_FIELDS"/>, inst.name institutionName, inst.code institutionCode
<if test="params.fuzzyName != null">,similarity(c.name, #{params.fuzzyName,jdbcType=VARCHAR}) AS similarity_score</if>
<include refid="LIST_FILTER" />
ORDER BY <if test="params.query != null" >ts_rank_cd(c.fulltext_search, query) DESC, </if>
SELECT * FROM(
SELECT DISTINCT ON (<if test="params.query != null" >ts_rank_cd(c.fulltext_search, query), </if>
<if test="params.fuzzyName != null">similarity_score, </if>
c.created, c.key)
<include refid="COLLECTION_READ_FIELDS"/>, inst.name institutionName, inst.code institutionCode, c.fulltext_search
<if test="params.query != null" >,query</if>
<if test="params.fuzzyName != null">,similarity(c.name, #{params.fuzzyName,jdbcType=VARCHAR}) AS similarity_score</if>
<include refid="LIST_FILTER" />
) AS c
ORDER BY
<if test="params.sortBy != null and params.query == null">
<choose>
<when test="params.sortBy.name.equals('NUMBER_SPECIMENS')">
c.number_specimens
</when>
<otherwise/>
</choose>
<if test="params.sortOrder != null">
<choose>
<when test="params.sortOrder.name.equals('DESC')">
DESC
</when>
<otherwise>
ASC
</otherwise>
</choose>
</if>
NULLS LAST,
</if>
<if test="params.query != null" >ts_rank_cd(c.fulltext_search, query) DESC, </if>
<if test="params.fuzzyName != null" >similarity_score DESC, </if>
c.created DESC, c.key
<if test="params.page != null" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,36 @@
</select>

<select id="list" resultType="Institution" resultMap="INSTITUTION_MAP" parameterType="Pageable">
SELECT DISTINCT ON (<if test="params.query != null" >ts_rank_cd(i.fulltext_search, query), </if>
<if test="params.fuzzyName != null" >similarity_score, </if>
i.created, i.key)
<include refid="INSTITUTION_READ_FIELDS"/>
<if test="params.fuzzyName != null" >,similarity(i.name, #{params.fuzzyName,jdbcType=VARCHAR}) AS similarity_score</if>
<include refid="LIST_FILTER"/>
ORDER BY <if test="params.query != null" >ts_rank_cd(i.fulltext_search, query) DESC, </if>
SELECT * FROM(
SELECT DISTINCT ON (<if test="params.query != null" >ts_rank_cd(i.fulltext_search, query), </if>
<if test="params.fuzzyName != null" >similarity_score, </if>
i.created, i.key)
<include refid="INSTITUTION_READ_FIELDS"/>, i.fulltext_search
<if test="params.query != null" >,query</if>
<if test="params.fuzzyName != null" >,similarity(i.name, #{params.fuzzyName,jdbcType=VARCHAR}) AS similarity_score</if>
<include refid="LIST_FILTER"/>
) AS i
ORDER BY
<if test="params.sortBy != null and params.query == null">
<choose>
<when test="params.sortBy.name.equals('NUMBER_SPECIMENS')">
i.number_specimens
</when>
<otherwise/>
</choose>
<if test="params.sortOrder != null">
<choose>
<when test="params.sortOrder.name.equals('DESC')">
DESC
</when>
<otherwise>
ASC
</otherwise>
</choose>
</if>
NULLS LAST,
</if>
<if test="params.query != null" >ts_rank_cd(i.fulltext_search, query) DESC, </if>
<if test="params.fuzzyName != null" >similarity_score DESC, </if>
i.created DESC, i.key
<if test="params.page != null" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ private PagingResponse<CollectionView> listInternal(
.occurrenceCount(parseIntegerRangeParameter(searchRequest.getOccurrenceCount()))
.typeSpecimenCount(parseIntegerRangeParameter(searchRequest.getTypeSpecimenCount()))
.deleted(deleted)
.sortBy(searchRequest.getSortBy())
.sortOrder(searchRequest.getSortOrder())
.page(page)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ private InstitutionSearchParams buildSearchParams(
.occurrenceCount(parseIntegerRangeParameter(searchRequest.getOccurrenceCount()))
.typeSpecimenCount(parseIntegerRangeParameter(searchRequest.getTypeSpecimenCount()))
.deleted(deleted)
.sortBy(searchRequest.getSortBy())
.sortOrder(searchRequest.getSortOrder())
.page(page)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.gbif.api.model.collections.request.SearchRequest;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.util.VocabularyUtils;
import org.gbif.api.vocabulary.CollectionsSortField;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.GbifRegion;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.SortOrder;
import org.gbif.api.vocabulary.collections.MasterSourceType;
import org.gbif.registry.service.collections.utils.SearchUtils;
import org.gbif.ws.server.provider.PageableProvider;
Expand Down Expand Up @@ -130,6 +132,24 @@ protected <T extends SearchRequest> void fillSearchRequestParams(
}
}

String sortByParam = webRequest.getParameter("sortBy");
if (!Strings.isNullOrEmpty(sortByParam)) {
try {
request.setSortBy(CollectionsSortField.valueOf(sortByParam));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid sort by parameter: " + sortByParam);
}
}

String sortOrderParam = webRequest.getParameter("sortOrder");
if (!Strings.isNullOrEmpty(sortOrderParam)) {
try {
request.setSortOrder(SortOrder.valueOf(sortOrderParam));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid sort order parameter: " + sortOrderParam);
}
}

String occurrenceCountParam = webRequest.getParameter("occurrenceCount");
if (!Strings.isNullOrEmpty(occurrenceCountParam)) {
validateIntegerRange(occurrenceCountParam, "occurrenceCount");
Expand Down

0 comments on commit 4761789

Please sign in to comment.