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
…11655)

* [Fixes #11652] Facets: wrong count on multiple tkeywords filtering - tkeywords

* [Fixes #11652] Facets: wrong count on multiple tkeywords filtering - category

* [Fixes #11652] Facets: wrong count on multiple tkeywords filtering - region

* [Fixes #11652] Facets: wrong count on multiple tkeywords filtering - keywords
  • Loading branch information
etj authored and giohappy committed Nov 6, 2023
1 parent 04f6e08 commit 4c4edfb
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 90 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

Check warning on line 62 in geonode/facets/providers/category.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/providers/category.py#L62

Added line #L62 was not covered by tests

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
16 changes: 8 additions & 8 deletions geonode/facets/providers/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ def get_facet_items(
) -> (int, list):
logger.debug("Retrieving facets for %s", self.name)

filters = {"keywords__isnull": False}
filters = {"resourcebase__in": queryset}
if topic_contains:
filters["keywords__name__icontains"] = topic_contains
filters["name__icontains"] = topic_contains

Check warning on line 62 in geonode/facets/providers/keyword.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/providers/keyword.py#L61-L62

Added lines #L61 - L62 were not covered by tests
if keys:
logger.debug("Filtering by keys %r", keys)
filters["keywords__slug__in"] = keys
filters["slug__in"] = keys

q = (
queryset.filter(**filters)
.values("keywords__slug", "keywords__name")
.annotate(count=Count("keywords__slug"))
HierarchicalKeyword.objects.filter(**filters)
.values("slug", "name")
.annotate(count=Count("resourcebase"))
.order_by("-count")
)

Expand All @@ -79,8 +79,8 @@ def get_facet_items(

topics = [
{
"key": r["keywords__slug"],
"label": r["keywords__name"],
"key": r["slug"],
"label": r["name"],
"count": r["count"],
}
for r in q[start:end].all()
Expand Down
18 changes: 9 additions & 9 deletions geonode/facets/providers/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_info(self, lang="en", **kwargs) -> dict:

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

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

if topic_contains:
filters["regions__name"] = topic_contains
filters["name__icontains"] = topic_contains

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

q = (
queryset.filter(**filters)
.values("regions__code", "regions__name")
.annotate(count=Count("regions__code"))
Region.objects.filter(**filters)
.values("code", "name")
.annotate(count=Count("resourcebase"))
.order_by("-count")
)

Expand All @@ -80,8 +80,8 @@ def get_facet_items(

topics = [
{
"key": r["regions__code"],
"label": r["regions__name"],
"key": r["code"],
"label": r["name"],
"count": r["count"],
}
for r in q[start:end].all()
Expand Down
28 changes: 14 additions & 14 deletions geonode/facets/providers/thesaurus.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_info(self, lang="en", **kwargs) -> dict:

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

filter = {
"tkeywords__thesaurus__identifier": self._name,
}
filter = {"thesaurus__identifier": self._name, "resourcebase__in": queryset}

if topic_contains:
filter["tkeywords__keyword__label__icontains"] = topic_contains
filter["label__icontains"] = topic_contains

Check warning on line 72 in geonode/facets/providers/thesaurus.py

View check run for this annotation

Codecov / codecov/patch

geonode/facets/providers/thesaurus.py#L72

Added line #L72 was not covered by tests

if keys:
logger.debug("Filtering by keys %r\n", keys)
filter["tkeywords__in"] = keys
filter["id__in"] = keys

q = (
queryset.filter(**filter)
.values("tkeywords", "tkeywords__alt_label", "tkeywords__image")
.annotate(count=Count("tkeywords"))
ThesaurusKeyword.objects.filter(**filter)
.values("id", "alt_label", "image")
.annotate(count=Count("resourcebase"))
.annotate(
localized_label=Subquery(
ThesaurusKeywordLabel.objects.filter(keyword=OuterRef("tkeywords"), lang=lang).values("label")
ThesaurusKeywordLabel.objects.filter(keyword=OuterRef("id"), lang=lang).values("label")
)
)
.order_by("-count")
)

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

cnt = q.count()

Expand All @@ -98,11 +98,11 @@ def get_facet_items(

topics = [
{
"key": r["tkeywords"],
"label": r["localized_label"] or r["tkeywords__alt_label"],
"key": r["id"],
"label": r["localized_label"] or r["alt_label"],
"is_localized": r["localized_label"] is not None,
"count": r["count"],
"image": r["tkeywords__image"],
"image": r["image"],
}
for r in q[start:end].all()
]
Expand Down
Loading

0 comments on commit 4c4edfb

Please sign in to comment.