diff --git a/CHANGELOG.md b/CHANGELOG.md index 53dd6ff375..8246a57327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to ### Fixed +- 🐛(backend) fix full-text search scoped to a document - 🐛(frontend) refresh pins after document deletion and restoration - 🐛(frontend) redirect homepage to login when homepage feat is disabled #2521 - 🐛(backend) ignore CSPs for API docs in development diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index e63fb726de..076db9b28f 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -1550,8 +1550,8 @@ def _search_using_indexer(indexer, request, params, search_type): document_id = params.validated_data.get("document") if document_id: try: - path = models.Document.objects.get(pk=document_id).values_list( - "path", flat=True + path = models.Document.objects.values_list("path", flat=True).get( + pk=document_id ) except models.Document.DoesNotExist as exc: raise drf.exceptions.NotFound("Document not found.") from exc diff --git a/src/backend/core/tests/documents/test_api_documents_search.py b/src/backend/core/tests/documents/test_api_documents_search.py index 4ccc1f00ba..5e4b515c1c 100644 --- a/src/backend/core/tests/documents/test_api_documents_search.py +++ b/src/backend/core/tests/documents/test_api_documents_search.py @@ -612,3 +612,20 @@ def test_api_documents_search_success(indexer_settings): assert results == [ {"id": document["id"], "title": document["title"], "path": document["path"]} ] + + +@mock.patch("core.services.search_indexers.FindDocumentIndexer.search_query") +def test_api_documents_search_success_scoped(search_query, indexer_settings): + """A document-scoped indexer search should use the document path.""" + indexer_settings.SEARCH_URL = "http://find/api/v1.0/search" + search_query.return_value = [] + document = factories.DocumentFactory() + + response = APIClient().get( + "/api/v1.0/documents/search/", + data={"q": "alpha", "document": document.id}, + ) + + assert response.status_code == 200 + assert search_query.call_count == 1 + assert search_query.call_args.kwargs["data"]["path"] == document.path