diff --git a/cognee/api/v1/search/search.py b/cognee/api/v1/search/search.py index 0a9e76e96..32035e612 100644 --- a/cognee/api/v1/search/search.py +++ b/cognee/api/v1/search/search.py @@ -1,13 +1,14 @@ from uuid import UUID from typing import Union, Optional, List, Type +from cognee.infrastructure.databases.graph import get_graph_engine from cognee.modules.engine.models.node_set import NodeSet from cognee.modules.users.models import User from cognee.modules.search.types import SearchResult, SearchType, CombinedSearchResult from cognee.modules.users.methods import get_default_user from cognee.modules.search.methods import search as search_function from cognee.modules.data.methods import get_authorized_existing_datasets -from cognee.modules.data.exceptions import DatasetNotFoundError +from cognee.modules.data.exceptions import DatasetNotFoundError, SearchOnEmptyGraphError async def search( @@ -175,6 +176,15 @@ async def search( if not datasets: raise DatasetNotFoundError(message="No datasets found.") + graph_engine = await get_graph_engine() + edges_count = await graph_engine.count_edges() + nodes_count = await graph_engine.count_nodes() + + if nodes_count == 0 or edges_count == 0: + raise SearchOnEmptyGraphError( + message="Knowledge graph is empty, please ensure data is added and cognified." + ) + filtered_search_results = await search_function( query_text=query_text, query_type=query_type, diff --git a/cognee/modules/data/exceptions/__init__.py b/cognee/modules/data/exceptions/__init__.py index 54af81070..ba943634d 100644 --- a/cognee/modules/data/exceptions/__init__.py +++ b/cognee/modules/data/exceptions/__init__.py @@ -9,4 +9,5 @@ from .exceptions import ( UnauthorizedDataAccessError, DatasetNotFoundError, DatasetTypeError, + SearchOnEmptyGraphError, ) diff --git a/cognee/modules/data/exceptions/exceptions.py b/cognee/modules/data/exceptions/exceptions.py index ac3b68e64..c2921750a 100644 --- a/cognee/modules/data/exceptions/exceptions.py +++ b/cognee/modules/data/exceptions/exceptions.py @@ -35,6 +35,16 @@ class DatasetNotFoundError(CogneeValidationError): super().__init__(message, name, status_code) +class SearchOnEmptyGraphError(CogneeValidationError): + def __init__( + self, + message: str = "Knowledge graph is empty, please ensure data is added and cognified.", + name: str = "SearchOnEmptyGraphError", + status_code=status.HTTP_400_BAD_REQUEST, + ): + super().__init__(message, name, status_code) + + class DatasetTypeError(CogneeValidationError): def __init__( self,