From 013cd864c29d621a1e25cddb821d384e5fa6a935 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 3 Dec 2019 20:48:35 +0300 Subject: [PATCH] tags dynamic filters #2 --- apps/search_indexes/filters.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/apps/search_indexes/filters.py b/apps/search_indexes/filters.py index 3101fdc2..a12c37f7 100644 --- a/apps/search_indexes/filters.py +++ b/apps/search_indexes/filters.py @@ -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