From 76e087cfe576e5c07ed2da063144f65dbdb935e2 Mon Sep 17 00:00:00 2001 From: Luke Sikina Date: Sat, 13 Jul 2024 11:58:22 -0400 Subject: [PATCH] take 2 --- .../filter/FilterQueryGenerator.java | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 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 0f7b33b..4a88b7f 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 @@ -31,15 +31,28 @@ 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 """; @@ -47,14 +60,6 @@ public QueryParamPair generateFilterQuery(Filter filter, Pageable pageable) { .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); } @@ -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 <> '' """; } @@ -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 <> '' + ) ) """; }