diff --git a/posthog/api/search.py b/posthog/api/search.py index 9f0f1fe77a39f..e346c3a905737 100644 --- a/posthog/api/search.py +++ b/posthog/api/search.py @@ -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] @@ -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"[\'&|!<>():]" @@ -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)