Skip to content
Draft
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
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
Loading