Skip to content

Commit

Permalink
Merge branch 'emc-181-sort-option' into 'develop'
Browse files Browse the repository at this point in the history
Add sort option to search endpoint

See merge request eip/catalogue!754
  • Loading branch information
MWatkeysCEH committed Nov 1, 2024
2 parents f7c6951 + 0abdd4f commit cbef2e0
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public SearchResults search(
int page,
int rows,
List<FacetFilter> facetFilters,
String catalogueKey
String catalogueKey,
String sortField,
SolrQuery.ORDER sortOrder
) {
val basicResults= super.search(
val basicResults = super.search(
endpoint,
user,
term,
Expand All @@ -61,7 +63,9 @@ public SearchResults search(
page,
rows,
facetFilters,
catalogueKey
catalogueKey,
sortField,
sortOrder
);
val relatedSearches = relatedSearches(endpoint, term);
log.debug(relatedSearches.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
Expand All @@ -28,6 +29,8 @@ public class SearchController {
public static final String PAGE_QUERY_PARAM = "page";
public static final String ROWS_QUERY_PARAM = "rows";
public static final String FACET_QUERY_PARAM = "facet";
public static final String SORT_FIELD_PARAM = "sortField";
public static final String SORT_ORDER_PARAM = "order";

public static final int PAGE_DEFAULT = Integer.parseInt(PAGE_DEFAULT_STRING);
public static final int ROWS_DEFAULT = Integer.parseInt(ROWS_DEFAULT_STRING);
Expand Down Expand Up @@ -78,6 +81,10 @@ public SearchResults search(
int rows,
@RequestParam(value=FACET_QUERY_PARAM, defaultValue = "")
List<FacetFilter> facetFilters,
@RequestParam(value=SORT_FIELD_PARAM, required = false)
String sortField,
@RequestParam(value=SORT_ORDER_PARAM, defaultValue = "asc")
String sortOrder,
HttpServletRequest request
) {
return searcher.search(
Expand All @@ -89,7 +96,9 @@ public SearchResults search(
page,
rows,
facetFilters,
catalogueKey
catalogueKey,
sortField,
"desc".equals(sortOrder) ? SolrQuery.ORDER.desc : SolrQuery.ORDER.asc
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public class SearchQuery {
public static final String DEFAULT_SEARCH_TERM = "*";
private static final String RANDOM_DYNAMIC_FIELD_NAME = "random";

// Fields which need to have _raw appended before they can be used
// as a Solr sort option. Keep in sync with managed-schema.
private static final List<String> RAW_SORT_FIELDS = List.of(
"title",
"description",
"lineage",
"objectives",
"infrastructureCapabilities"
);

String endpoint;
CatalogueUser user;
@NotNull String term;
Expand All @@ -38,6 +48,8 @@ public class SearchQuery {
GroupStore<CatalogueUser> groupStore;
Catalogue catalogue;
List<Facet> facets;
String sortField;
SolrQuery.ORDER sortOrder;

public SearchQuery(
String endpoint,
Expand All @@ -50,7 +62,9 @@ public SearchQuery(
List<FacetFilter> facetFilters,
GroupStore<CatalogueUser> groupStore,
Catalogue catalogue,
List<Facet> facets
List<Facet> facets,
String sortField,
SolrQuery.ORDER sortOrder
) {
this.endpoint = endpoint;
this.user = user;
Expand All @@ -63,6 +77,8 @@ public SearchQuery(
this.groupStore = groupStore;
this.facets = facets;
this.catalogue = catalogue;
this.sortField = sortField;
this.sortOrder = sortOrder;
}

public SolrQuery build(){
Expand Down Expand Up @@ -103,7 +119,9 @@ public SearchQuery withPage(int newPage) {
facetFilters,
groupStore,
catalogue,
facets
facets,
sortField,
sortOrder
);
}
else {
Expand All @@ -130,7 +148,9 @@ public SearchQuery withBbox(String newBbox) {
facetFilters,
groupStore,
catalogue,
facets
facets,
sortField,
sortOrder
);
}
else {
Expand Down Expand Up @@ -158,7 +178,9 @@ public SearchQuery withSpatialOperation(SpatialOperation newSpatialOperation) {
facetFilters,
groupStore,
catalogue,
facets
facets,
sortField,
sortOrder
);
}
else {
Expand Down Expand Up @@ -192,7 +214,9 @@ public SearchQuery withFacetFilter(FacetFilter filter) {
newFacetFilters,
groupStore,
catalogue,
facets
facets,
sortField,
sortOrder
);
}
else {
Expand Down Expand Up @@ -223,7 +247,9 @@ public SearchQuery withoutFacetFilter(FacetFilter filter) {
newFacetFilters,
groupStore,
catalogue,
facets
facets,
sortField,
sortOrder
);
}
else {
Expand Down Expand Up @@ -267,6 +293,13 @@ public String toUrl() {
facetFilters.forEach((f)-> builder.queryParam(FACET_QUERY_PARAM, f.asURIContent()));
}

if(sortField != null) {
builder.queryParam(SORT_FIELD_PARAM, sortField);
if (sortOrder != null) {
builder.queryParam(SORT_ORDER_PARAM, sortOrder);
}
}

return builder.build().toUriString();
}

Expand Down Expand Up @@ -342,8 +375,11 @@ private void setCatalogueFilter(SolrQuery query) {
);
}

private void setSortOrder(SolrQuery query){
if(DEFAULT_SEARCH_TERM.equals(term)){
private void setSortOrder(SolrQuery query) {
if (sortField != null) {
final String maybeRawSuffix = RAW_SORT_FIELDS.contains(sortField) ? "_raw" : "";
query.setSort(sortField + maybeRawSuffix, sortOrder);
} else if (DEFAULT_SEARCH_TERM.equals(term)) {
query.setSort(getRandomFieldName(), SolrQuery.ORDER.asc);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.ac.ceh.gateway.catalogue.search;

import uk.ac.ceh.gateway.catalogue.model.CatalogueUser;
import org.apache.solr.client.solrj.SolrQuery;

import java.util.List;

Expand All @@ -14,6 +15,8 @@ SearchResults search(
int page,
int rows,
List<FacetFilter> facetFilters,
String catalogueKey
String catalogueKey,
String sortField,
SolrQuery.ORDER sortOrder
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.SneakyThrows;
import lombok.val;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import uk.ac.ceh.components.userstore.GroupStore;
Expand Down Expand Up @@ -45,7 +46,9 @@ public SearchResults search(
int page,
int rows,
List<FacetFilter> facetFilters,
String catalogueKey
String catalogueKey,
String sortField,
SolrQuery.ORDER sortOrder
) {
val catalogue = catalogueService.retrieve(catalogueKey);

Expand All @@ -60,7 +63,9 @@ public SearchResults search(
facetFilters,
groupStore,
catalogue,
facetFactory.newInstances(catalogue.getFacetKeys())
facetFactory.newInstances(catalogue.getFacetKeys()),
sortField,
sortOrder
);
val response = solrClient.query(
"documents",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.val;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -44,6 +45,8 @@ class EnhancedSolrSearcherTest {
private final int rows = 20;
private final List<FacetFilter> facetFilters = List.of(new FacetFilter("filter|test"));
private final String catalogueKey = "green";
private final String sortField = "publishedDate";
private final ORDER sortOrder = ORDER.desc;

@BeforeEach
void setup() {
Expand Down Expand Up @@ -75,7 +78,9 @@ void search() {
page,
rows,
facetFilters,
catalogueKey
catalogueKey,
sortField,
sortOrder
);

//then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private void givenSearchResults() {
anyInt(),
anyInt(),
any(),
any(),
any(),
any()
)).willReturn(searchResults);

Expand Down
Loading

0 comments on commit cbef2e0

Please sign in to comment.