Skip to content

Commit

Permalink
Ranked search
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Jul 13, 2024
1 parent 2eb7a16 commit f576d8d
Showing 1 changed file with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,7 @@ public QueryParamPair generateFilterQuery(Filter filter, Pageable pageable) {
if (StringUtils.hasText(filter.search())) {
clauses.add(createSearchFilter(filter.search(), params));
}
clauses.add("""
(
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 <> ''
)
"""
);
clauses.add(createValuelessNodeFilter());


String query = "(\n" + String.join("\n\tINTERSECT\n", clauses) + "\n) ORDER BY concept_node_id\n";
Expand All @@ -63,13 +48,34 @@ public QueryParamPair generateFilterQuery(Filter filter, Pageable pageable) {
}

String superQuery = """
WITH q AS (%s) SELECT concept_node_id FROM q GROUP BY concept_node_id ORDER BY sum(rank) DESC"
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);
}

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 <> ''
""";
}

private String createSearchFilter(String search, MapSqlParameterSource params) {
params.addValue("search", search);
return """
Expand Down

0 comments on commit f576d8d

Please sign in to comment.