From f576d8d4f699054eec95814bec15f757a31ad4a7 Mon Sep 17 00:00:00 2001 From: Luke Sikina Date: Sat, 13 Jul 2024 10:37:26 -0400 Subject: [PATCH] Ranked search --- .../filter/FilterQueryGenerator.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterQueryGenerator.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterQueryGenerator.java index 4021a66..0f7b33b 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterQueryGenerator.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterQueryGenerator.java @@ -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"; @@ -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 """