Skip to content

Commit

Permalink
fix(search): various search improvements (#18674)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Nov 16, 2023
1 parent 1c0ffda commit 5170d7c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions posthog/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from posthog.permissions import ProjectMembershipNecessaryPermissions, TeamMemberAccessPermission
from posthog.models import Action, Cohort, Insight, Dashboard, FeatureFlag, Experiment, Team

LIMIT = 25


class SearchViewSet(StructuredViewSetMixin, viewsets.ViewSet):
permission_classes = [IsAuthenticated, ProjectMembershipNecessaryPermissions, TeamMemberAccessPermission]
Expand All @@ -29,7 +31,10 @@ def list(self, request: Request, **kw) -> HttpResponse:
qs = qs.union(klass_qs)
counts[type] = klass_qs.count()

return Response({"results": qs, "counts": counts})
if query:
qs = qs.order_by("-rank")

return Response({"results": qs[:LIMIT], "counts": counts})


UNSAFE_CHARACTERS = r"[\'&|!<>():]"
Expand Down Expand Up @@ -63,9 +68,12 @@ def class_queryset(klass: type[Model], team: Team, query: str | None):
qs = qs.annotate(result_id=Cast("pk", CharField()))

if query:
qs = qs.annotate(rank=SearchRank(SearchVector("name"), SearchQuery(query, search_type="raw")))
qs = qs.annotate(
rank=SearchRank(
SearchVector("name", config="simple"), SearchQuery(query, config="simple", search_type="raw")
)
)
qs = qs.filter(rank__gt=0.05)
qs = qs.order_by("-rank")
values.append("rank")

qs = qs.values(*values)
Expand Down

0 comments on commit 5170d7c

Please sign in to comment.