Skip to content

Commit

Permalink
Restore support for sub-aggregations (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasl committed Nov 18, 2024
1 parent 354c5cb commit f01b5f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
17 changes: 12 additions & 5 deletions rest/src/main/groovy/whelk/rest/api/SearchUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -383,29 +383,36 @@ class SearchUtils {
aggregation['buckets'].each { bucket ->
String itemId = bucket['key']

Map observation
if (key in multiSelected.keySet()) {
String param = makeParam(key, itemId)
boolean isSelected = escapeQueryParam(itemId) in multiSelected[key]
String searchPageUrl = isSelected
? baseUrlForKey.replace("&${param}", '') // FIXME: generate up-link in a cleaner way
: "${baseUrlForKey}&${param}"

Map observation = ['totalItems': bucket.getAt('doc_count'),
observation = ['totalItems': bucket.getAt('doc_count'),
'view': [(JsonLd.ID_KEY): searchPageUrl],
'_selected': isSelected,
'object': lookup.chip(itemId)]

observations << observation
}
else {
String searchPageUrl = "${baseUrlForKey}&${ESQuery.AND_PREFIX}${makeParam(key, itemId)}"

Map observation = ['totalItems': bucket.getAt('doc_count'),
observation = ['totalItems': bucket.getAt('doc_count'),
'view': [(JsonLd.ID_KEY): searchPageUrl],
'object': lookup.chip(itemId)]

observations << observation
}

Map bucketAggs = bucket[ESQuery.FILTERED_AGG_NAME]
if (bucketAggs) {
Map nestedAggs = bucketAggs.clone()
nestedAggs.remove('doc_count')
observation = addSlices(lookup, observation, nestedAggs, baseUrlForKey, null, multiSelected)
}

observations << observation
}

if (observations) {
Expand Down
33 changes: 26 additions & 7 deletions whelk-core/src/main/groovy/whelk/search/ESQuery.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -948,16 +948,20 @@ class ESQuery {
}

keys.each { key ->
String sort = tree[key]?.sort =='key' ? '_key' : '_count'
def sortOrder = tree[key]?.sortOrder =='asc' ? 'asc' : 'desc'
Map treeNode = tree[key]
String sort = treeNode?.sort =='key' ? '_key' : '_count'
def sortOrder = treeNode?.sortOrder =='asc' ? 'asc' : 'desc'
String termPath = getInferredTermPath(key)

// Core agg query
query[termPath] = ['terms': [
def coreQuery = [
'terms': [
'field': termPath,
'size' : tree[key]?.size ?: size,
'order': [(sort): sortOrder]]
'size' : treeNode?.size ?: size,
'order': [(sort): sortOrder]
]
]
query[termPath] = coreQuery

// If field is nested, wrap agg query with nested
nestedFields.find{ key.startsWith(it) }?.with { nestedField ->
Expand All @@ -974,8 +978,23 @@ class ESQuery {
'filter': ['bool': ['must': filters]]
]

if (tree[key].subItems instanceof Map) {
query[termPath]['aggs'] = buildAggQuery(tree[key].subItems, multiSelectFilters, size)
if (treeNode.subItems instanceof Map) {
def subAggQuery = buildAggQuery(treeNode.subItems, multiSelectFilters, size)
// TODO: factor out parts of buildAggQuery to avoid this reshuffle.
//coreQuery['aggs'] = subAggQuery
// TODO: handle multiple keys.
def iter = subAggQuery.iterator()
if (iter.hasNext()) {
def entry = iter.next()
def subkey = entry.getKey()
def subvalue = entry.getValue()
coreQuery['aggs'] = [
(FILTERED_AGG_NAME): [
'aggs': [ (subkey): subvalue['aggs'][FILTERED_AGG_NAME] ],
'filter': subvalue['filter']
]
]
}
}
}
return query
Expand Down

0 comments on commit f01b5f8

Please sign in to comment.