diff --git a/lightrag/lightrag.py b/lightrag/lightrag.py index 43614a93..72510ac0 100644 --- a/lightrag/lightrag.py +++ b/lightrag/lightrag.py @@ -78,6 +78,7 @@ from .operate import ( extract_entities, merge_nodes_and_edges, kg_query, + kg_search, naive_query, _rebuild_knowledge_from_chunks, ) @@ -2105,6 +2106,55 @@ class LightRAG: await self._query_done() return response + def search( + self, + query: str, + param: QueryParam = QueryParam(), + ) -> dict[str, Any]: + """ + Synchronous search API: returns structured retrieval results without LLM generation. + + Args: + query: Query text. + param: Query parameters (reuse the same QueryParam as query/aquery). + + Returns: + dict[str, Any]: {"entities": [...], "relationships": [...], "chunks": [...], "metadata": {...}} + """ + loop = always_get_an_event_loop() + return loop.run_until_complete(self.asearch(query, param)) + + async def asearch( + self, + query: str, + param: QueryParam = QueryParam(), + ) -> dict[str, Any]: + """ + Asynchronous search API: calls kg_search and returns retrieval-only results + (entities, relationships, and merged chunks). + + Args: + query: Query text. + param: Query parameters (reuse the same QueryParam as query/aquery). + + Returns: + dict[str, Any]: Structured search result + """ + global_config = asdict(self) + response = await kg_search( + query.strip(), + self.chunk_entity_relation_graph, + self.entities_vdb, + self.relationships_vdb, + self.text_chunks, + param, + global_config, + hashing_kv=self.llm_response_cache, + chunks_vdb=self.chunks_vdb, + ) + await self._query_done() + return response + async def _query_done(self): await self.llm_response_cache.index_done_callback() diff --git a/lightrag/operate.py b/lightrag/operate.py index f41ca5ff..275e9c64 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -2814,6 +2814,7 @@ async def kg_search( knowledge_graph_inst, entities_vdb, relationships_vdb, + text_chunks_db, query_param, chunks_vdb, ) @@ -3118,6 +3119,7 @@ async def _build_query_context( knowledge_graph_inst, entities_vdb, relationships_vdb, + text_chunks_db, query_param, chunks_vdb, )