Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26 gdi dataset discovery service add facet query operator #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CkanDatasetsSearchService(

@Override
public DatasetsSearchResponse search(DatasetSearchQuery query, String accessToken) {
var facetsQuery = CkanFacetsQueryBuilder.buildFacetQuery(query.getFacets());
var facetsQuery = CkanFacetsQueryBuilder.buildFacetQuery(query);

var response = ckanQueryApi.packageSearch(
query.getQuery(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Objects;

import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery;
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQueryFacet;
import lombok.experimental.UtilityClass;

Expand All @@ -21,18 +22,20 @@ public class CkanFacetsQueryBuilder {
private final String CKAN_FACET_GROUP = "ckan";
private final String QUOTED_VALUE = "\"%s\"";
private final String FACET_PATTERN = "%s:(%s)";
private final String AND = " AND ";

public String buildFacetQuery(List<DatasetSearchQueryFacet> facets) {
public String buildFacetQuery(DatasetSearchQuery query) {
var facets = query.getFacets();
var operator = query.getOperator();

var nonNullFacets = ofNullable(facets)
.orElseGet(List::of)
.stream()
.filter(CkanFacetsQueryBuilder::isCkanGroupAndFacetIsNotBlank)
.collect(groupingBy(DatasetSearchQueryFacet::getFacet));

return nonNullFacets.entrySet().stream()
.map(entry -> getFacetQuery(entry.getKey(), entry.getValue()))
.collect(joining(AND));
.map(entry -> getFacetQuery(entry.getKey(), entry.getValue(), operator))
.collect(joining(CkanQueryOperatorMapper.getOperator(operator)));
}

private Boolean isCkanGroupAndFacetIsNotBlank(DatasetSearchQueryFacet facet) {
Expand All @@ -43,10 +46,11 @@ private Boolean isCkanGroupAndFacetIsNotBlank(DatasetSearchQueryFacet facet) {
!facet.getValue().isBlank();
}

private String getFacetQuery(String key, List<DatasetSearchQueryFacet> facets) {
private String getFacetQuery(String key, List<DatasetSearchQueryFacet> facets,
DatasetSearchQuery.OperatorEnum operator) {
var values = facets.stream()
.map(facet -> QUOTED_VALUE.formatted(facet.getValue()))
.collect(joining(AND));
.collect(joining(CkanQueryOperatorMapper.getOperator(operator)));

return FACET_PATTERN.formatted(key, values);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.discovery.services;

import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery;
import lombok.experimental.UtilityClass;

@UtilityClass
public class CkanQueryOperatorMapper {

private final String AND = " AND ";
private final String OR = " OR ";

public String getOperator(DatasetSearchQuery.OperatorEnum operator) {
return operator.equals(DatasetSearchQuery.OperatorEnum.OR) ? OR : AND;
}
}
7 changes: 7 additions & 0 deletions src/main/openapi/discovery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ components:
title: Offset in the complete result set
default: 0
minimum: 0
operator:
title: Ckan query operator
type: string
enum:
- AND
- OR
default: AND
DatasetSearchQueryFacet:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.github.genomicdatainfrastructure.discovery.services;

import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
Expand All @@ -19,22 +20,24 @@ class CkanFacetsQueryBuilderTest {

@Test
void can_parse_if_empty_list() {
var query = new DatasetSearchQuery();
query.setFacets(List.of());
var expected = "";
var actual = CkanFacetsQueryBuilder.buildFacetQuery(List.of());
var actual = CkanFacetsQueryBuilder.buildFacetQuery(query);
assertEquals(expected, actual);
}

@Test
void can_parse_if_null_list() {
String expected = "";
var actual = CkanFacetsQueryBuilder.buildFacetQuery(null);
var actual = CkanFacetsQueryBuilder.buildFacetQuery(new DatasetSearchQuery());
assertEquals(expected, actual);
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", " "})
void can_parse(String value) {
void can_parse_with_and_operator(String value) {
var facets = List.of(
new DatasetSearchQueryFacet("ckan", "field1", "value1"),
new DatasetSearchQueryFacet("ckan", "field1", "value2"),
Expand All @@ -47,8 +50,37 @@ void can_parse(String value) {
new DatasetSearchQueryFacet(value, "field2", "value3")
);

var query = new DatasetSearchQuery();
query.setFacets(facets);
query.setOperator(DatasetSearchQuery.OperatorEnum.AND);

var expected = "field1:(\"value1\" AND \"value2\") AND field2:(\"value3\")";
var actual = CkanFacetsQueryBuilder.buildFacetQuery(facets);
var actual = CkanFacetsQueryBuilder.buildFacetQuery(query);
assertEquals(expected, actual);
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", " "})
void can_parse_with_or_operator(String value) {
var facets = List.of(
new DatasetSearchQueryFacet("ckan", "field1", "value1"),
new DatasetSearchQueryFacet("ckan", "field1", "value2"),
new DatasetSearchQueryFacet("ckan", "field2", "value3"),
new DatasetSearchQueryFacet("dummy", "field2", "value"),
new DatasetSearchQueryFacet(null, "field2", "value"),
new DatasetSearchQueryFacet(null, null, "value"),
new DatasetSearchQueryFacet("ckan", value, "value3"),
new DatasetSearchQueryFacet("ckan", "field2", value),
new DatasetSearchQueryFacet(value, "field2", "value3")
);

var query = new DatasetSearchQuery();
query.setFacets(facets);
query.setOperator(DatasetSearchQuery.OperatorEnum.OR);

var expected = "field1:(\"value1\" OR \"value2\") OR field2:(\"value3\")";
var actual = CkanFacetsQueryBuilder.buildFacetQuery(query);
assertEquals(expected, actual);
}
}