Skip to content

Commit

Permalink
[Fixes #11652] Facets: wrong count on multiple tkeywords filtering - …
Browse files Browse the repository at this point in the history
…category
  • Loading branch information
etj committed Nov 3, 2023
1 parent 57ba7eb commit 815e7d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 14 additions & 10 deletions geonode/facets/providers/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def name(self) -> str:
def get_info(self, lang="en", **kwargs) -> dict:
return {
"name": self.name,
"filter": "filter{category.identifier}",
"filter": "filter{category.identifier.in}",
"label": "Category",
"type": FACET_TYPE_CATEGORY,
}

def get_facet_items(
self,
queryset=None,
queryset,
start: int = 0,
end: int = DEFAULT_FACET_PAGE_SIZE,
lang="en",
Expand All @@ -56,22 +56,26 @@ def get_facet_items(
) -> (int, list):
logger.debug("Retrieving facets for %s", self.name)

filters = {"category__isnull": False}
filters = {"resourcebase__in": queryset}

if topic_contains:
filters["category__gn_description"] = topic_contains
filters["gn_description__icontains"] = topic_contains

if keys:
logger.debug("Filtering by keys %r", keys)
filters["category__identifier__in"] = keys
filters["identifier__in"] = keys

q = (
queryset.values("category__identifier", "category__gn_description", "category__fa_class")
TopicCategory.objects.values("identifier", "gn_description", "fa_class")
.filter(**filters)
.annotate(count=Count("owner"))
.annotate(count=Count("resourcebase"))
.order_by("-count")
)

logger.debug(" PREFILTERED QUERY ---> %s\n\n", queryset.query)
logger.debug(" ADDITIONAL FILTERS ---> %s\n\n", filters)
logger.debug(" FINAL QUERY ---> %s\n\n", q.query)

cnt = q.count()

logger.info("Found %d facets for %s", cnt, self.name)
Expand All @@ -80,10 +84,10 @@ def get_facet_items(

topics = [
{
"key": r["category__identifier"],
"label": r["category__gn_description"],
"key": r["identifier"],
"label": r["gn_description"],
"count": r["count"],
"fa_class": r["category__fa_class"],
"fa_class": r["fa_class"],
}
for r in q[start:end].all()
]
Expand Down
19 changes: 13 additions & 6 deletions geonode/facets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def _create_categories(cls):
("C0", "Cat0"),
("C1", "Cat1"),
("C2", "Cat2"),
("C3", "Cat3"),
):
cls.cats[code] = TopicCategory.objects.create(identifier=code, description=name, gn_description=name)

Expand Down Expand Up @@ -157,10 +158,10 @@ def _create_resources(self):
# RB04 -> T1K0 K0,K1 C0
# RB05 -> T0K0 T1K0 K0,K2 C1
# RB06 -> T1K0 FEAT
# RB07 -> T0K0 T1K0 FEAT
# RB08 -> T1K0 T1K1 R1 FEAT
# RB09 -> T0K0 T1K0 T1K1
# RB10 -> T1K1
# RB07 -> T0K0 T1K0 FEAT C3
# RB08 -> T1K0 T1K1 R1 FEAT C3
# RB09 -> T0K0 T1K0 T1K1 C3
# RB10 -> T1K1 C3
# RB11 -> T0K0 T0K1 T1K1
# RB12 -> T1K1 FEAT
# RB13 -> T0K0 T0K1 R1 FEAT
Expand Down Expand Up @@ -205,6 +206,7 @@ def _create_resources(self):
("C0", [0, 2, 4]),
("C1", [5, 15, 16]),
("C2", [18, 19]),
("C3", [7, 8, 9, 10]),
):
if x in idx:
d.category = self.cats[cat]
Expand Down Expand Up @@ -248,10 +250,11 @@ def test_facets_rich(self):
{
"name": "category",
"topics": {
"total": 3,
"total": 4,
"items": [
{"label": "Cat0", "count": 3},
{"label": "Cat1", "count": 3},
{"label": "Cat3", "count": 4},
{"label": "Cat2", "count": 2},
],
},
Expand Down Expand Up @@ -475,6 +478,7 @@ def test_count0(self):
regname = reginfo["name"]

catinfo = CategoryFacetProvider().get_info()
catflt = catinfo["filter"]
catname = catinfo["name"]

kwinfo = KeywordFacetProvider().get_info()
Expand Down Expand Up @@ -502,13 +506,16 @@ def t(tk):
("t_1", {t0flt: t("0_0")}, {t("1_0"): 5, t("1_1"): 2}),
("t_1", {t0flt: t("0_1")}, {t("1_1"): 1}),
("t_1", {t0flt: [t("0_1"), t("0_0")]}, {t("1_0"): 5, t("1_1"): 2}),
("t_1", {catflt: ["C0"]}, {t("1_0"): 3}),
("t_1", {catflt: ["C0", "C1"]}, {t("1_0"): 4}),
# regions
(regname, {t1flt: t("1_1")}, {"R1": 1}),
(regname, {t1flt: t("1_1"), "key": ["R0", "R1"]}, {"R1": 1, "R0": None}),
(regname, {t1flt: t("1_1"), "key": ["R0"]}, {"R0": None}),
# category
(catname, {t1flt: t("1_0")}, {"C0": 3, "C1": 1}),
(catname, {t1flt: t("1_0")}, {"C0": 3, "C1": 1, "C3": 3}),
(catname, {t1flt: t("1_0"), "key": ["C0", "C2"]}, {"C0": 3, "C2": None}),
(catname, {t1flt: [t("1_0"), t("1_1")]}, {"C0": 3, "C1": 1, "C3": 4}),
(catname, {kwflt: "K1"}, {"C0": 1}),
(catname, {kwflt: "K1", "key": ["C0", "C2"]}, {"C0": 1, "C2": None}),
# keyword
Expand Down

0 comments on commit 815e7d6

Please sign in to comment.