Skip to content

Commit

Permalink
Merge pull request #517 from NASA-PDS/builder-defaults-refactor
Browse files Browse the repository at this point in the history
Implement common multiple-products build method
  • Loading branch information
alexdunnjpl authored Aug 6, 2024
2 parents 071dcc2 + cc758fe commit ff3d17b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,7 @@ public ResponseEntity<Object> productList(List<String> fields, List<String> keyw
Integer limit, String q, List<String> sort, List<String> searchAfter) throws Exception {

SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.constrainByQueryString(q)
.addKeywordsParam(keywords)
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.applyMultipleProductsDefaults(fields, q, keywords, limit, sort, searchAfter, true)
.build();

SearchResponse<HashMap> searchResponse =
Expand Down Expand Up @@ -299,7 +295,7 @@ private HashMap<String, Object> getLatestLidVid(PdsProductIdentifier identifier,
SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.matchLid(identifier)
.fieldsFromStrings(fields)
.onlyLatest()
.excludeSupersededProducts()
.build();

// useless to detail here that the HashMap is parameterized <String, Object>
Expand Down Expand Up @@ -347,7 +343,7 @@ private PdsProductClasses resolveProductClass(PdsProductIdentifier identifier)
SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.matchLid(identifier)
.fieldsFromStrings(List.of(PdsProductClasses.getPropertyName()))
.onlyLatest()
.excludeSupersededProducts()
.build();

SearchResponse<HashMap> searchResponse = this.openSearchClient.search(searchRequest, HashMap.class);
Expand All @@ -367,7 +363,7 @@ private PdsLidVid resolveLatestLidvid(PdsProductIdentifier identifier)
SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.matchLid(identifier.getLid())
.fieldsFromStrings(List.of())
.onlyLatest()
.excludeSupersededProducts()
.build();

SearchResponse<HashMap> searchResponse = this.openSearchClient.search(searchRequest, HashMap.class);
Expand Down Expand Up @@ -437,9 +433,7 @@ public ResponseEntity<Object> productMembers(
}

SearchRequest searchRequest = searchRequestBuilder
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.applyMultipleProductsDefaults(fields, "", List.of(), limit, sort, searchAfter, true)
.build();

SearchResponse<HashMap> searchResponse =
Expand All @@ -449,7 +443,7 @@ public ResponseEntity<Object> productMembers(

return formatMultipleProducts(products, fields);

} catch (IOException | OpenSearchException e) {
} catch (IOException | OpenSearchException | UnparsableQParamException e) {
throw new UnhandledException(e);
}
}
Expand All @@ -476,9 +470,7 @@ public ResponseEntity<Object> productMembersMembers(
}

SearchRequest searchRequest = searchRequestBuilder
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.applyMultipleProductsDefaults(fields, "", List.of(), limit, sort, searchAfter, true)
.build();

SearchResponse<HashMap> searchResponse =
Expand All @@ -488,7 +480,7 @@ public ResponseEntity<Object> productMembersMembers(

return formatMultipleProducts(products, fields);

} catch (IOException | OpenSearchException e) {
} catch (IOException | OpenSearchException | UnparsableQParamException e) {
throw new UnhandledException(e);
}
}
Expand Down Expand Up @@ -533,7 +525,7 @@ private List<PdsLidVid> resolveLidVidsFromProductField(PdsProductIdentifier iden
public ResponseEntity<Object> productMemberOf(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{
AcceptFormatNotSupportedException, UnparsableQParamException {

try{
PdsProductIdentifier pdsIdentifier = PdsProductIdentifier.fromString(identifier);
Expand All @@ -551,10 +543,8 @@ public ResponseEntity<Object> productMemberOf(
}

SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.applyMultipleProductsDefaults(fields, "", List.of(), limit, sort, searchAfter, true)
.matchFieldAnyOfIdentifiers("_id", parentIds)
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.build();

SearchResponse<HashMap> searchResponse =
Expand All @@ -573,7 +563,7 @@ public ResponseEntity<Object> productMemberOf(
public ResponseEntity<Object> productMemberOfOf(
String identifier, List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws NotFoundException, UnhandledException, SortSearchAfterMismatchException, BadRequestException,
AcceptFormatNotSupportedException{
AcceptFormatNotSupportedException, UnparsableQParamException {

try{
PdsProductIdentifier pdsIdentifier = PdsProductIdentifier.fromString(identifier);
Expand All @@ -590,10 +580,8 @@ public ResponseEntity<Object> productMemberOfOf(
}

SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.applyMultipleProductsDefaults(fields, "", List.of(), limit, sort, searchAfter, true)
.matchFieldAnyOfIdentifiers("_id", parentIds)
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.build();

SearchResponse<HashMap> searchResponse =
Expand All @@ -619,12 +607,8 @@ public ResponseEntity<Object> classList(String propertyClass, List<String> field
}

SearchRequest searchRequest = new RegistrySearchRequestBuilder(this.connectionContext)
.applyMultipleProductsDefaults(fields, q, keywords, limit, sort, searchAfter, true)
.matchProductClass(pdsProductClass)
.constrainByQueryString(q)
.addKeywordsParam(keywords)
.fieldsFromStrings(fields)
.paginate(limit, sort, searchAfter)
.onlyLatest()
.build();

SearchResponse<HashMap> searchResponse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ public BoolQuery.Builder getQueryBuilder() {
return this.queryBuilder;
}

/**
* Applies a common set of constraints and other build options which generally apply to any endpoint which queries
* OpenSearch for a result-set of multiple products.
* @param includeFieldNames - which properties to include in the results (JSON format, not OpenSearch format)
* @param queryString - a querystring (q=) to constrain the result-set by
* @param keywords - a set of keyword matches to <exact behaviour TBD - not implemented yet>
* @param pageSize - the page size to use for pagination
* @param sortFieldNames - the fields by which results are sorted (ascending), from highest to lowest priority
* @param searchAfterFieldValues - the values corresponding to the sort fields, for pagination
* @param excludeSupersededProducts - whether to exclude superseded products from the result set
*/
public RegistrySearchRequestBuilder applyMultipleProductsDefaults(
List<String> includeFieldNames,
String queryString,
List<String> keywords,
Integer pageSize,
List<String> sortFieldNames,
List<String> searchAfterFieldValues,
Boolean excludeSupersededProducts
) throws UnparsableQParamException, SortSearchAfterMismatchException {
this
.fieldsFromStrings(includeFieldNames)
.constrainByQueryString(queryString)
.addKeywordsParam(keywords)
.paginate(pageSize, sortFieldNames, searchAfterFieldValues);

if (excludeSupersededProducts) {
this.excludeSupersededProducts();
}

return this;
}

public SearchRequest build() {
this.query(this.queryBuilder.build().toQuery());
this.trackTotalHits(t -> t.enabled(true));
Expand Down Expand Up @@ -325,7 +358,7 @@ public RegistrySearchRequestBuilder addKeywordsParam(List<String> keywords) {
* N.B. this does *not* mean the latest version which satisfies other constraints, so application of this constraint
* can result in no hits being returned despite valid results existing.
*/
public RegistrySearchRequestBuilder onlyLatest() {
public RegistrySearchRequestBuilder excludeSupersededProducts() {

ExistsQuery supersededByExists = new ExistsQuery.Builder()
.field("ops:Provenance/ops:superseded_by")
Expand Down

0 comments on commit ff3d17b

Please sign in to comment.