add index_document to search_service.py

This commit is contained in:
phernandez
2025-01-04 19:53:50 -06:00
parent 0b918ee9e8
commit 6b94ec57a4
4 changed files with 114 additions and 15 deletions
+31 -4
View File
@@ -3,7 +3,7 @@
from typing import List, Optional
from basic_memory.repository.search_repository import SearchRepository
from basic_memory.schemas.search import SearchQuery, SearchResult
from basic_memory.schemas.search import SearchQuery, SearchResult, SearchItemType
class SearchService:
@@ -52,7 +52,7 @@ class SearchService:
content=content,
path_id=entity.path_id,
file_path=entity.file_path,
type="entity",
type=SearchItemType.ENTITY,
metadata=metadata,
)
else:
@@ -60,10 +60,37 @@ class SearchService:
content=content,
path_id=entity.path_id,
file_path=entity.file_path,
type="entity",
type=SearchItemType.ENTITY,
metadata=metadata,
)
async def index_document(self, document, content: str, background_tasks=None):
"""Index a document and its content."""
metadata = {
**document.doc_metadata,
"created_at": document.created_at.isoformat(),
"updated_at": document.updated_at.isoformat(),
}
# Queue indexing if background_tasks provided
if background_tasks:
background_tasks.add_task(
self._do_index,
content=content,
path_id=document.path_id,
file_path=document.file_path,
type=SearchItemType.DOCUMENT,
metadata=metadata,
)
else:
await self._do_index(
content=content,
path_id=document.path_id,
file_path=document.file_path,
type=SearchItemType.DOCUMENT,
metadata=metadata,
)
async def _do_index(self, **kwargs):
"""Actually perform the indexing."""
await self.repository.index_item(**kwargs)
await self.repository.index_item(**kwargs)