From b24ce80033c0595d4a92ab024cf8bb428b7b5889 Mon Sep 17 00:00:00 2001 From: Luke Sikina Date: Sun, 21 Jul 2024 11:39:13 -0400 Subject: [PATCH] fixed it! --- one_facet.sql | 2 +- .../dictionary/facet/FacetQueryGenerator.java | 33 +++++++++---------- .../facet/FacetQueryGeneratorTest.java | 2 -- two_cats.sql | 10 +++--- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/one_facet.sql b/one_facet.sql index 0256cdc..9414727 100644 --- a/one_facet.sql +++ b/one_facet.sql @@ -29,8 +29,8 @@ UNION FROM facet JOIN facet__concept_node fcn ON fcn.facet_id = facet.facet_id - JOIN facet_category fc on fc.facet_category_id = facet.facet_category_id JOIN concept_node ON concept_node.concept_node_id = fcn.concept_node_id + JOIN facet_category fc on fc.facet_category_id = facet.facet_category_id 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' diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGenerator.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGenerator.java index 83bd9a5..bbc68fe 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGenerator.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGenerator.java @@ -53,16 +53,16 @@ private String createMultiCategorySQLWithSearch(Map> facets, params.addValue("search", search); /* - for each category of facet present in the filter, create a query that gives all the concept IDs for those - facets + For each category of facet present in the filter, create a query that represents all the concept IDs + associated with the selected facets in that category */ String conceptsQuery = "WITH " + facets.keySet().stream().map(category -> { - List selectedFacetsNotInCategory = facets.entrySet().stream() - .filter(e -> !e.getKey().equals(category)) + List selectedFacetsInCateory = facets.entrySet().stream() + .filter(e -> category.equals(e.getKey())) .flatMap(e -> e.getValue().stream()) .map(facet -> new String[]{facet.category(), facet.name()}) .toList(); - params.addValue("facets_not_in_cat_" + categoryKeys.get(category), selectedFacetsNotInCategory); + params.addValue("facets_in_cat_" + categoryKeys.get(category), selectedFacetsInCateory); params.addValue("facet_category_" + categoryKeys.get(category), category); return """ facet_category_%s_concepts AS ( @@ -77,7 +77,7 @@ private String createMultiCategorySQLWithSearch(Map> facets, 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 - (fc.name, facet.name) IN (:facets_not_in_cat_%s) + (fc.name, facet.name) IN (:facets_in_cat_%s) AND concept_node.searchable_fields @@ (phraseto_tsquery(:search)::text || ':*')::tsquery AND ( continuous_min.value <> '' OR @@ -96,7 +96,7 @@ private String createMultiCategorySQLWithSearch(Map> facets, String allConceptsForCategory = categoryKeys.values().stream() .filter(key -> !categoryKeys.get(category).equals(key)) .map(key -> "SELECT * FROM facet_category_" + key + "_concepts") - .collect(Collectors.joining(" UNION ")); + .collect(Collectors.joining(" INTERSECT ")); params.addValue("", ""); return """ ( @@ -122,7 +122,7 @@ fcn.concept_node_id IN (%s) params.addValue("all_selected_facet_categories", facets.keySet()); String allConceptsForUnselectedCategories = categoryKeys.values().stream() .map(key -> "SELECT * FROM facet_category_" + key + "_concepts") - .collect(Collectors.joining(" UNION ")); + .collect(Collectors.joining(" INTERSECT ")); String unselectedFacetsQuery = """ UNION ( @@ -149,16 +149,16 @@ private String createMultiCategorySQLNoSearch(Map> facets, M Map categoryKeys = createSQLSafeCategoryKeys(facets.keySet().stream().toList()); /* - for each category of facet present in the filter, create a query that gives all the concept IDs for those - facets + For each category of facet present in the filter, create a query that represents all the concept IDs + associated with the selected facets in that category */ String conceptsQuery = "WITH " + facets.keySet().stream().map(category -> { - List selectedFacetsNotInCategory = facets.entrySet().stream() - .filter(e -> !e.getKey().equals(category)) + List selectedFacetsInCateory = facets.entrySet().stream() + .filter(e -> category.equals(e.getKey())) .flatMap(e -> e.getValue().stream()) .map(facet -> new String[]{facet.category(), facet.name()}) .toList(); - params.addValue("facets_not_in_cat_" + categoryKeys.get(category), selectedFacetsNotInCategory); + params.addValue("facets_in_cat_" + categoryKeys.get(category), selectedFacetsInCateory); params.addValue("facet_category_" + categoryKeys.get(category), category); return """ facet_category_%s_concepts AS ( @@ -173,7 +173,7 @@ private String createMultiCategorySQLNoSearch(Map> facets, M 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 - (fc.name, facet.name) IN (:facets_not_in_cat_%s) + (fc.name, facet.name) IN (:facets_in_cat_%s) AND ( continuous_min.value <> '' OR continuous_max.value <> '' OR @@ -183,7 +183,6 @@ private String createMultiCategorySQLNoSearch(Map> facets, M """.formatted(categoryKeys.get(category), categoryKeys.get(category)); }).collect(Collectors.joining(",\n")); /* - Categories with no selected facets contribute no concepts, so ignore them for now. Now, for each category with selected facets, take all the concepts from all other categories with selections and INTERSECT them. This creates the concepts for this category */ @@ -192,7 +191,7 @@ private String createMultiCategorySQLNoSearch(Map> facets, M String allConceptsForCategory = categoryKeys.values().stream() .filter(key -> !categoryKeys.get(category).equals(key)) .map(key -> "SELECT * FROM facet_category_" + key + "_concepts") - .collect(Collectors.joining(" UNION ")); + .collect(Collectors.joining(" INTERSECT ")); params.addValue("", ""); return """ ( @@ -220,7 +219,7 @@ fcn.concept_node_id IN (%s) params.addValue("all_selected_facet_categories", facets.keySet()); String allConceptsForUnselectedCategories = categoryKeys.values().stream() .map(key -> "SELECT * FROM facet_category_" + key + "_concepts") - .collect(Collectors.joining(" UNION ")); + .collect(Collectors.joining(" INTERSECT ")); String unselectedFacetsQuery = """ UNION ( diff --git a/src/test/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGeneratorTest.java b/src/test/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGeneratorTest.java index 733c3f7..99eff80 100644 --- a/src/test/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGeneratorTest.java +++ b/src/test/java/edu/harvard/dbmi/avillach/dictionary/facet/FacetQueryGeneratorTest.java @@ -16,8 +16,6 @@ class FacetQueryGeneratorTest { @Autowired FacetQueryGenerator generator; -// {"facets":[{"name":"LOINC","display":"LOINC","description":null,"count":1,"children":null,"category":"nsrr_harmonized","meta":null,"categoryRef":{"display":"Common Data Element Collection","name":"nsrr_harmonized","description":""}},{"name":"phs000007","display":"FHS","description":null,"count":1,"children":null,"category":"study_ids_dataset_ids","meta":null,"categoryRef":{"display":"Study IDs/Dataset IDs","name":"study_ids_dataset_ids","description":""}}],"search":""} - @Test void shouldaaa() { Filter jim = new Filter(List.of( diff --git a/two_cats.sql b/two_cats.sql index 54d1300..c378561 100644 --- a/two_cats.sql +++ b/two_cats.sql @@ -10,7 +10,7 @@ WITH facet_category_cat_0_concepts AS ( 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 - (fc.name, facet.name) IN (('nsrr_harmonized', 'LOINC')) + (fc.name, facet.name) IN (('study_ids_dataset_ids', 'phs000007')) AND ( continuous_min.value <> '' OR continuous_max.value <> '' OR @@ -30,7 +30,7 @@ facet_category_cat_1_concepts AS ( 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 - (fc.name, facet.name) IN (('study_ids_dataset_ids', 'phs000007')) + (fc.name, facet.name) IN (('nsrr_harmonized', 'LOINC')) AND ( continuous_min.value <> '' OR continuous_max.value <> '' OR @@ -53,7 +53,7 @@ facet_category_cat_1_concepts AS ( facet_count DESC ) - UNION + UNION ( SELECT facet.facet_id, count(*) as facet_count @@ -79,9 +79,9 @@ UNION JOIN facet__concept_node fcn ON fcn.facet_id = facet.facet_id WHERE fc.name NOT IN ('nsrr_harmonized', 'study_ids_dataset_ids') - AND fcn.concept_node_id IN (SELECT * FROM facet_category_cat_0_concepts UNION SELECT * FROM facet_category_cat_1_concepts) + AND fcn.concept_node_id IN (SELECT * FROM facet_category_cat_0_concepts INTERSECT SELECT * FROM facet_category_cat_1_concepts) GROUP BY facet.facet_id ORDER BY facet_count DESC -) \ No newline at end of file +)