11from typing import Optional
22
3- from fastapi import HTTPException , Query , Body , Path , Depends , APIRouter
3+ from fastapi import HTTPException , Query , Body , Path , Depends , APIRouter , Header
44from consts .model import IndexingRequest , IndexingResponse , SearchRequest , HybridSearchRequest
55
66from nexent .vector_database .elasticsearch_core import ElasticSearchCore
77from services .elasticsearch_service import ElasticSearchService , get_es_core
8- from database .knowledge_db import create_knowledge_record
8+ from database .utils import get_current_user_id
99router = APIRouter (prefix = "/indices" )
1010
1111
1212@router .post ("/{index_name}" )
1313def create_new_index (
1414 index_name : str = Path (..., description = "Name of the index to create" ),
1515 embedding_dim : Optional [int ] = Query (None , description = "Dimension of the embedding vectors" ),
16- user_id : Optional [ str ] = Body ( None , description = "ID of the user creating the knowledge base" ),
17- es_core : ElasticSearchCore = Depends ( get_es_core )
16+ es_core : ElasticSearchCore = Depends ( get_es_core ),
17+ authorization : Optional [ str ] = Header ( None )
1818):
1919 """Create a new vector index and store it in the knowledge table"""
2020 try :
2121 # Create the index in Elasticsearch
22+ user_id = get_current_user_id (authorization )
2223 return ElasticSearchService .create_index (index_name , embedding_dim , es_core , user_id )
2324 except Exception as e :
2425 raise HTTPException (
@@ -30,11 +31,13 @@ def create_new_index(
3031@router .delete ("/{index_name}" )
3132def delete_index (
3233 index_name : str = Path (..., description = "Name of the index to delete" ),
33- es_core : ElasticSearchCore = Depends (get_es_core )
34+ es_core : ElasticSearchCore = Depends (get_es_core ),
35+ authorization : Optional [str ] = Header (None )
3436):
3537 """Delete an index"""
3638 try :
37- return ElasticSearchService .delete_index (index_name , es_core )
39+ user_id = get_current_user_id (authorization )
40+ return ElasticSearchService .delete_index (index_name , es_core , user_id )
3841 except Exception as e :
3942 raise HTTPException (status_code = 404 , detail = f"Error delete index: { str (e )} " )
4043
@@ -152,11 +155,13 @@ def health_check(es_core: ElasticSearchCore = Depends(get_es_core)):
152155def summery (
153156 index_name : str = Path (..., description = "Name of the index to get documents from" ),
154157 batch_size : int = Query (1000 , description = "Number of documents to retrieve per batch" ),
155- es_core : ElasticSearchCore = Depends (get_es_core )
158+ es_core : ElasticSearchCore = Depends (get_es_core ),
159+ authorization : Optional [str ] = Header (None )
156160 ):
157161 """Summery Elasticsearch index_name"""
158162 try :
163+ user_id = get_current_user_id (authorization )
159164 # Try to list indices as a health check
160- return ElasticSearchService ().summery_index_name (index_name = index_name ,batch_size = batch_size , es_core = es_core )
165+ return ElasticSearchService ().summery_index_name (index_name = index_name ,batch_size = batch_size , es_core = es_core , user_id = user_id )
161166 except Exception as e :
162167 raise HTTPException (status_code = 500 , detail = f"{ str (e )} " )
0 commit comments