Skip to content

Commit

Permalink
take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Jul 13, 2024
1 parent f576d8d commit 76e087c
Showing 1 changed file with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,35 @@ public QueryParamPair generateFilterQuery(Filter filter, Pageable pageable) {
if (!CollectionUtils.isEmpty(filter.facets())) {
clauses.addAll(createFacetFilter(filter.facets(), params));
}
if (StringUtils.hasText(filter.search())) {
clauses.add(createSearchFilter(filter.search(), params));
if (clauses.isEmpty()) {
clauses.add(createValuelessNodeFilter());
}
clauses.add(createValuelessNodeFilter());


String query = "(\n" + String.join("\n\tINTERSECT\n", clauses) + "\n) ORDER BY concept_node_id\n";
String query = "(\n" + String.join("\n\tINTERSECT\n", clauses) + "\n)";
if (StringUtils.hasText(filter.search())) {
String searchQuery = createSearchFilter(filter.search(), params);
query = "(" + query + "\n\tUNION \n\t" + searchQuery + ")";
}
String superQuery = """
WITH q AS (
%s
)
SELECT concept_node_id
FROM q
GROUP BY concept_node_id
HAVING max(rank) > 0
ORDER BY max(rank) DESC
""".formatted(query);
if (pageable.isPaged()) {
query = query + """
superQuery = superQuery + """
LIMIT :limit
OFFSET :offset
""";
params.addValue("limit", pageable.getPageSize())
.addValue("offset", pageable.getOffset());
}

String superQuery = """
WITH q AS (%s)
SELECT concept_node_id
FROM q
GROUP BY concept_node_id
ORDER BY max(rank) DESC"
""".formatted(query);


return new QueryParamPair(superQuery, params);
}
Expand All @@ -63,16 +68,16 @@ private String createValuelessNodeFilter() {
// concept nodes that have no values and no min/max should not get returned by search
return """
SELECT
concept_node.concept_node_id, 0 as rank
FROM
concept_node
LEFT JOIN concept_node_meta AS continuous_min ON concept_node.concept_node_id = continuous_min.concept_node_id AND continuous_min.KEY = 'min'
LEFT JOIN concept_node_meta AS continuous_max ON concept_node.concept_node_id = continuous_max.concept_node_id AND continuous_max.KEY = 'max'
LEFT JOIN concept_node_meta AS categorical_values ON concept_node.concept_node_id = categorical_values.concept_node_id AND categorical_values.KEY = 'values'
WHERE
continuous_min.value <> '' OR
continuous_max.value <> '' OR
categorical_values.value <> ''
concept_node.concept_node_id, 0 as rank
FROM
concept_node
LEFT JOIN concept_node_meta AS continuous_min ON concept_node.concept_node_id = continuous_min.concept_node_id AND continuous_min.KEY = 'min'
LEFT JOIN concept_node_meta AS continuous_max ON concept_node.concept_node_id = continuous_max.concept_node_id AND continuous_max.KEY = 'max'
LEFT JOIN concept_node_meta AS categorical_values ON concept_node.concept_node_id = categorical_values.concept_node_id AND categorical_values.KEY = 'values'
WHERE
continuous_min.value <> '' OR
continuous_max.value <> '' OR
categorical_values.value <> ''
""";
}

Expand All @@ -86,7 +91,12 @@ private String createSearchFilter(String search, MapSqlParameterSource params) {
FROM
concept_node
WHERE
concept_node.searchable_fields @@ (phraseto_tsquery(:search)::text || ':*')::tsquery
concept_node.searchable_fields @@ (phraseto_tsquery(:search)::text || ':*')::tsquery AND
(
continuous_min.value <> '' OR
continuous_max.value <> '' OR
categorical_values.value <> ''
)
)
""";
}
Expand Down

0 comments on commit 76e087c

Please sign in to comment.