Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(search): scope ranked search typo matches to the project's teams (#121837)

Shy Altereb32fee5d185
diff --git a/posthog/api/file_system/file_system.py b/posthog/api/file_system/file_system.py
index 1afb1a259e88..a5aa5043fdad 100644
--- a/posthog/api/file_system/file_system.py
+++ b/posthog/api/file_system/file_system.py
@@ -1367,8 +1367,11 @@ class FileSystemViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
field, has_colon, _ = bare.partition(":")
is_filter = bare != token or (has_colon and field.lower() in SEARCH_FILTER_FIELDS)
(filter_tokens if is_filter else free_tokens).append(token)
+ project_team_ids = list(Team.objects.filter(project_id=self.team.project_id).values_list("id", flat=True))
queryset = self._scope_by_project_and_environment(FileSystem.objects.select_related("created_by"))
- queryset = queryset.exclude(type__in=[*RETIRED_FILE_SYSTEM_TYPES, "folder"])
+ queryset = queryset.filter(team_id__in=project_team_ids).exclude(
+ type__in=[*RETIRED_FILE_SYSTEM_TYPES, "folder"]
+ )
if filter_tokens:
queryset = self._apply_search_to_queryset(queryset, " ".join(shlex.quote(t) for t in filter_tokens))
queryset = self._filter_by_access_control(queryset)
diff --git a/posthog/api/file_system/test/test_ranked_search.py b/posthog/api/file_system/test/test_ranked_search.py
index 53e1e1987af1..99600c6cfe3e 100644
--- a/posthog/api/file_system/test/test_ranked_search.py
+++ b/posthog/api/file_system/test/test_ranked_search.py
@@ -159,6 +159,18 @@ class TestFileSystemRankedSearch(APIBaseTest):
self.assertEqual([item["entry"]["type"] for item in data["results"]], ["dashboard"])
self.assertEqual(data["type_counts"], [{"type": "dashboard", "count": 1}])
+ def test_typo_search_covers_every_environment_of_the_project_only(self) -> None:
+ self._create("Unfiled/Insights/Retention", type="insight")
+ environment = Team.objects.create(organization=self.organization, project=self.project, name="Env-2")
+ FileSystem.objects.create(team=environment, path="Unfiled/Dashboards/Retention", type="dashboard")
+ other_team = Team.objects.create(organization=self.organization, name="Other project")
+ FileSystem.objects.create(team=other_team, path="Unfiled/Notebooks/Retention", type="notebook")
+
+ data = self._search("retension")
+
+ self.assertTrue(data["matched_with_typos"])
+ self.assertCountEqual([item["entry"]["type"] for item in data["results"]], ["insight", "dashboard"])
+
def test_per_type_previews_include_lower_ranked_types(self) -> None:
for index in range(5):
self._create(f"Unfiled/Insights/Revenue {index}", type="insight")