Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/main/java/gov/nih/nci/bento_ri/model/PrivateESDataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,15 +759,19 @@ private Map<String, Object> searchParticipants(Map<String, Object> params) throw
AGG_ENDPOINT, PARTICIPANTS_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
CARDINALITY_AGG_NAME, "pid",
AGG_NAME, "dbgap_accession",
ADDITIONAL_UPDATE, Map.of("phs001327", 2000),
FILTER_COUNT_QUERY, "filterParticipantCountByDBGAPAccession",
AGG_ENDPOINT, PARTICIPANTS_END_POINT
AGG_ENDPOINT, STUDIES_FACET_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
CARDINALITY_AGG_NAME, "pid",
AGG_NAME, "study_acronym",
ADDITIONAL_UPDATE, Map.of("CCSS_SMN", 2000),
WIDGET_QUERY, "participantCountByStudy",
FILTER_COUNT_QUERY, "filterParticipantCountByAcronym",
AGG_ENDPOINT, PARTICIPANTS_END_POINT
AGG_ENDPOINT, STUDIES_FACET_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
CARDINALITY_AGG_NAME, "pid",
Expand Down Expand Up @@ -922,24 +926,24 @@ private Map<String, Object> searchParticipants(Map<String, Object> params) throw
AGG_ENDPOINT, FILES_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
// CARDINALITY_AGG_NAME, "pid",
CARDINALITY_AGG_NAME, "pid",
// AGG_NAME, "study_name",
// FILTER_COUNT_QUERY, "filterParticipantCountByStudyTitle",
// ADDITIONAL_UPDATE, Map.of("Childhood Cancer Survivor Study (CCSS)", 2000, "Molecular Characterization Initiative", 1000),
// AGG_ENDPOINT, STUDIES_FACET_END_POINT
ADDITIONAL_UPDATE, Map.of("Childhood Cancer Survivor Study (CCSS)", 2000),
AGG_ENDPOINT, STUDIES_FACET_END_POINT,
AGG_NAME, "study_name",
FILTER_COUNT_QUERY, "filterParticipantCountByStudyTitle",
AGG_ENDPOINT, PARTICIPANTS_END_POINT
FILTER_COUNT_QUERY, "filterParticipantCountByStudyTitle"
// AGG_ENDPOINT, PARTICIPANTS_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
// CARDINALITY_AGG_NAME, "pid",
CARDINALITY_AGG_NAME, "pid",
// AGG_NAME, "study_status",
// FILTER_COUNT_QUERY, "filterParticipantCountByStudyStatus",
// ADDITIONAL_UPDATE, Map.of("Active", 2000,"Completed", 3000),
// AGG_ENDPOINT, STUDIES_FACET_END_POINT
ADDITIONAL_UPDATE, Map.of("Active", 2000,"Completed", 3000),
AGG_ENDPOINT, STUDIES_FACET_END_POINT,
AGG_NAME, "study_status",
FILTER_COUNT_QUERY, "filterParticipantCountByStudyStatus",
AGG_ENDPOINT, PARTICIPANTS_END_POINT
FILTER_COUNT_QUERY, "filterParticipantCountByStudyStatus"
//AGG_ENDPOINT, PARTICIPANTS_END_POINT
));
PARTICIPANT_TERM_AGGS.add(Map.of(
CARDINALITY_AGG_NAME, "pid",
Expand Down Expand Up @@ -1093,8 +1097,15 @@ private Map<String, Object> searchParticipants(Map<String, Object> params) throw
nestedProperty = "treatment_filters";
} else if (indexType.equals("treatment_responses")) {
nestedProperty = "treatment_response_filters";
} else {
} else if (indexType.equals("samples")) {
nestedProperty = "sample_diagnosis_file_filters";
} else if (indexType.equals("diagnosis")) {
nestedProperty = "sample_diagnosis_file_filters";
} else if (indexType.equals("files")) {
nestedProperty = "sample_diagnosis_file_filters";
} else {
// study_participants or participants
nestedProperty = "";
}
query_4_update = inventoryESService.addCustomAggregations(query_4_update, "facetAgg", prop, nestedProperty);
Request request = new Request("GET", PARTICIPANTS_END_POINT);
Expand Down
80 changes: 55 additions & 25 deletions src/main/java/gov/nih/nci/bento_ri/service/InventoryESService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1102,31 +1102,24 @@ public Map<String, Object> addAggregations(Map<String, Object> query, String[] t
}

public Map<String, Object> addCustomAggregations(Map<String, Object> query, String aggName, String field, String nestedProperty) {
// "aggs": {
// "customAgg": {
// "nested": {
// "path": "sample_diagnosis_file_filters"
// },
// "aggs": {
// "min_price": {
// "terms": {
// "field": "sample_diagnosis_file_filters.diagnosis_classification_system"
// },
// "aggs": {
// "top_reverse_nested": {
// "reverse_nested": {}
// }
// }
// }
// }
// }
// }
// When nestedProperty is empty: root-level terms aggregation (no nested path).
// When nestedProperty is set: nested aggregation with reverse_nested for doc_count at root.
Map<String, Object> newQuery = new HashMap<>(query);
newQuery.put("size", 0);
Map<String, Object> aggSection = new HashMap<String, Object>();
Map<String, Object> aggSubSection = new HashMap<String, Object>();
aggSubSection.put("agg_buckets", Map.of("terms", Map.of("field", nestedProperty + "." + field, "size", 1000), "aggs", Map.of("top_reverse_nested", Map.of("reverse_nested", Map.of()))));
aggSection.put(aggName, Map.of("nested", Map.of("path", nestedProperty), "aggs", aggSubSection));

if (nestedProperty == null || nestedProperty.isEmpty()) {
// Root-level aggregation: terms on field only, no nested/reverse_nested
Map<String, Object> aggSubSection = new HashMap<String, Object>();
aggSubSection.put("terms", Map.of("field", field, "size", 1000));
aggSection.put(aggName, aggSubSection);
} else {
// Nested aggregation: nested path + terms on nestedProperty.field + reverse_nested
Map<String, Object> aggSubSection = new HashMap<String, Object>();
aggSubSection.put("agg_buckets", Map.of("terms", Map.of("field", nestedProperty + "." + field, "size", 1000), "aggs", Map.of("top_reverse_nested", Map.of("reverse_nested", Map.of()))));
aggSection.put(aggName, Map.of("nested", Map.of("path", nestedProperty), "aggs", aggSubSection));
}

newQuery.put("aggs", aggSection);
return newQuery;
}
Expand Down Expand Up @@ -1183,11 +1176,48 @@ public List<String> collectTerms(JsonObject jsonObject, String aggName) {
}

public Map<String, Integer> collectCustomTerms(JsonObject jsonObject, String aggName) {
// "facetAgg": {
// "doc_count_error_upper_bound": 0,
// "sum_other_doc_count": 0,
// "buckets": [
// {
// "key": "Active",
// "doc_count": 40919
// },
// {
// "key": "Completed",
// "doc_count": 19703
// }
// ]
// }
// or in the case of nested aggregation:
// "facetAgg": {
// "doc_count": 822822,
// "agg_buckets": {
// "doc_count_error_upper_bound": 0,
// "sum_other_doc_count": 0,
// "buckets": [
// {
// "key": "see diagnosis_comment",
// "doc_count": 125542,
// "top_reverse_nested": {
// "doc_count": 6374
// }
// }

Map<String, Integer> data = new HashMap<>();
JsonObject aggs = jsonObject.getAsJsonObject("aggregations").getAsJsonObject(aggName);
JsonArray buckets = aggs.getAsJsonObject("agg_buckets").getAsJsonArray("buckets");
for (var bucket: buckets) {
data.put(bucket.getAsJsonObject().get("key").getAsString(), bucket.getAsJsonObject().getAsJsonObject("top_reverse_nested").get("doc_count").getAsInt());
JsonArray buckets = aggs.getAsJsonObject("agg_buckets") != null ? aggs.getAsJsonObject("agg_buckets").getAsJsonArray("buckets") : aggs.getAsJsonArray("buckets");
for (var bucket : buckets) {
JsonObject bucketObj = bucket.getAsJsonObject();
String key = bucketObj.get("key").getAsString();
int docCount;
if (bucketObj.has("top_reverse_nested")) {
docCount = bucketObj.getAsJsonObject("top_reverse_nested").get("doc_count").getAsInt();
} else {
docCount = bucketObj.get("doc_count").getAsInt();
}
data.put(key, docCount);
}
return data;
}
Expand Down