Skip to content

Commit

Permalink
fixed it!
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Jul 21, 2024
1 parent 5bab91c commit b24ce80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion one_facet.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ private String createMultiCategorySQLWithSearch(Map<String, List<Facet>> 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<String[]> selectedFacetsNotInCategory = facets.entrySet().stream()
.filter(e -> !e.getKey().equals(category))
List<String[]> 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 (
Expand All @@ -77,7 +77,7 @@ private String createMultiCategorySQLWithSearch(Map<String, List<Facet>> 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
Expand All @@ -96,7 +96,7 @@ private String createMultiCategorySQLWithSearch(Map<String, List<Facet>> 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 """
(
Expand All @@ -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
(
Expand All @@ -149,16 +149,16 @@ private String createMultiCategorySQLNoSearch(Map<String, List<Facet>> facets, M
Map<String, String> 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<String[]> selectedFacetsNotInCategory = facets.entrySet().stream()
.filter(e -> !e.getKey().equals(category))
List<String[]> 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 (
Expand All @@ -173,7 +173,7 @@ private String createMultiCategorySQLNoSearch(Map<String, List<Facet>> 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
Expand All @@ -183,7 +183,6 @@ private String createMultiCategorySQLNoSearch(Map<String, List<Facet>> 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
*/
Expand All @@ -192,7 +191,7 @@ private String createMultiCategorySQLNoSearch(Map<String, List<Facet>> 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 """
(
Expand Down Expand Up @@ -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
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions two_cats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -53,7 +53,7 @@ facet_category_cat_1_concepts AS (
facet_count DESC
)

UNION
UNION
(
SELECT
facet.facet_id, count(*) as facet_count
Expand All @@ -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
)
)

0 comments on commit b24ce80

Please sign in to comment.