Prior to search, check if knowledge graph is empty

This commit is contained in:
Daulet Amirkhanov 2025-10-15 16:39:48 +01:00
parent f3ec180102
commit 9367fa5d03
3 changed files with 22 additions and 1 deletions

View file

@ -1,13 +1,14 @@
from uuid import UUID from uuid import UUID
from typing import Union, Optional, List, Type 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.engine.models.node_set import NodeSet
from cognee.modules.users.models import User from cognee.modules.users.models import User
from cognee.modules.search.types import SearchResult, SearchType, CombinedSearchResult from cognee.modules.search.types import SearchResult, SearchType, CombinedSearchResult
from cognee.modules.users.methods import get_default_user from cognee.modules.users.methods import get_default_user
from cognee.modules.search.methods import search as search_function from cognee.modules.search.methods import search as search_function
from cognee.modules.data.methods import get_authorized_existing_datasets 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( async def search(
@ -175,6 +176,15 @@ async def search(
if not datasets: if not datasets:
raise DatasetNotFoundError(message="No datasets found.") 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( filtered_search_results = await search_function(
query_text=query_text, query_text=query_text,
query_type=query_type, query_type=query_type,

View file

@ -9,4 +9,5 @@ from .exceptions import (
UnauthorizedDataAccessError, UnauthorizedDataAccessError,
DatasetNotFoundError, DatasetNotFoundError,
DatasetTypeError, DatasetTypeError,
SearchOnEmptyGraphError,
) )

View file

@ -35,6 +35,16 @@ class DatasetNotFoundError(CogneeValidationError):
super().__init__(message, name, status_code) 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): class DatasetTypeError(CogneeValidationError):
def __init__( def __init__(
self, self,