QUERY_KNOWLEDGE_GRAPH

Use the DBMS_AI_GRAPHRAG.QUERY_KNOWLEDGE_GRAPH function to search a knowledge graph and returns the result as JSON. The result can contain retrieved context or a generated answer.

Purpose

This function retrieves relevant chunks and graph components for a query. It returns JSON containing the retrieved context and, optionally, a generated answer. The function supports vector search (VECTOR), graph-based retrieval (GRAPH), and combined retrieval (HYBRID, the default). It embeds the query and uses vector similarity to find relevant results. VECTOR search does not traverse the graph. GRAPH and HYBRID searches use vector similarity to find seed chunks or relationships, then traverse the graph and rank the results. HYBRID combines vector search with graph-based retrieval. When answer generation is requested, the function generates an answer from the retrieved context and includes it in the returned JSON. Otherwise, it returns the retrieved context.

Syntax

DBMS_AI_GRAPHRAG.QUERY_KNOWLEDGE_GRAPH (
    kg_name          IN VARCHAR2,
    query            IN CLOB,
    search_type      IN VARCHAR2 DEFAULT 'HYBRID',
    params           IN JSON,
    generate_answer  IN BOOLEAN DEFAULT FALSE
) RETURN JSON;

Parameters

Parameter Description
kg_name Name of the knowledge graph to query.
query Natural-language query.
search_type Specifies the retrieval method:
- 'VECTOR': Performs semantic search over chunks, entities, and relationships.
- 'GRAPH': Retrieves results by traversing the knowledge graph.
- 'HYBRID': Combines the results of 'VECTOR' and 'GRAPH': Searches using Reciprocal Rank Fusion (RRF). This is the default.
params JSON object that specifies embedding and retrieval settings.
generate_answer Specifies whether to generate an answer from the retrieved context. The default is FALSE; when false, the function returns the retrieved context.

The params object supports the following fields:

Field Description
llm_generate_params (Optional) LLM configuration for answer generation. If specified, it overrides the graph’s default LLM configuration.
embedding_params (Optional) Embedding configuration. If specified, it overrides the graph’s default embedding model.
topk_seeds (Optional) Number of seeds used to start graph retrieval for GRAPH and HYBRID searches. The default is 10.
topk_chunks (Optional) Maximum number of chunk results to include in the retrieved context. The default is 10.
topk_entities (Optional) Maximum number of entity results to include in the retrieved context. The default is 0.
topk_relationships (Optional) Maximum number of relationship results to include in the retrieved context. The default is 0.
n_hops (Optional) Maximum number of graph traversal hops for GRAPH and HYBRID searches. The valid range is 1 through 5. The default is 3.

Example

This example queries BOOKS_KG with hybrid search and generates an answer.

DECLARE
    l_result JSON;
BEGIN
    l_result := DBMS_AI_GRAPHRAG.QUERY_KNOWLEDGE_GRAPH(
        kg_name          => 'BOOKS_KG',
        query            => 'Who is the spouse of the author of Young Man Luther?',
        search_type      => 'HYBRID',
        params           => JSON('{"topk_chunks":5,"topk_seeds":10,"topk_entities":5}'),
        generate_answer  => TRUE
    );
END;
/