tags dynamic filters #2

This commit is contained in:
Kuroshini 2019-12-03 20:48:35 +03:00
parent c00075feec
commit 013cd864c2

View File

@ -149,25 +149,23 @@ class CustomFacetedSearchFilterBackend(FacetedSearchFilterBackend):
result_bucket = buckets[0]
for bucket in buckets[1:]:
for tag in bucket.tag.buckets._l_:
if tag not in result_bucket.tag.buckets._l_:
result_bucket.tag.buckets._l_.append(tag)
result_bucket.tag.buckets.append(tag)
def reducer(prev, cur):
try:
index = list(map(lambda x: x['key'], prev)).index(cur['key'])
if cur['doc_count'] < prev[index]['doc_count']:
prev[index]['doc_count'] = cur['doc_count']
except ValueError:
"""Unique by key"""
if not len(list(filter(lambda x: x['key'] == cur['key'], prev))):
prev.append(cur)
return prev
result_bucket.tag.buckets._l_ = list(reduce(
reducer, result_bucket.tag.buckets._l_, []
))
result_bucket.doc_count = reduce(
lambda prev, cur: prev + cur['doc_count'],
buckets_count = len(buckets)
result_bucket.tag.buckets = list(filter(lambda t: t is not None, [
tag if len(list(filter(lambda t: t['key'] == tag['key'], result_bucket.tag.buckets._l_))) == buckets_count else None
for tag in result_bucket.tag.buckets._l_])) # here we remove tags which don't present in any bucket
result_bucket.tag.buckets = list(reduce(
reducer,
result_bucket.tag.buckets._l_,
0
)
[]
))
return result_bucket