feat(indexing): add api fetch document in typesense

This commit is contained in:
arkohut 2024-07-11 12:12:54 +08:00
parent b5d524f7b0
commit 38272c48d0
3 changed files with 51 additions and 4 deletions

View File

@ -137,7 +137,8 @@ def search_entities(
search_parameters = {
"q": q,
"query_by": "tags,metadata_entries,filepath,filename,embedding",
"query_by": "filename,filepath,tags,metadata_entries,embedding",
"infix": "always,always,off,off,off",
"filter_by": f"{filter_by_str} && file_type_group:=image" if filter_by_str else "file_type_group:=image",
"per_page": limit,
"page": offset // limit + 1,
@ -174,3 +175,32 @@ def search_entities(
raise Exception(
f"Failed to search entities: {str(e)}",
)
def fetch_entity_by_id(client, id: str) -> EntityIndexItem:
try:
document = client.collections["entities"].documents[id].retrieve()
return EntitySearchResult(
id=document["id"],
filepath=document["filepath"],
filename=document["filename"],
size=document["size"],
file_created_at=document["file_created_at"],
file_last_modified_at=document["file_last_modified_at"],
file_type=document["file_type"],
file_type_group=document["file_type_group"],
last_scan_at=document.get("last_scan_at"),
library_id=document["library_id"],
folder_id=document["folder_id"],
tags=document["tags"],
metadata_entries=[
MetadataIndexItem(
key=entry["key"], value=entry["value"], source=entry["source"]
)
for entry in document["metadata_entries"]
],
)
except Exception as e:
raise Exception(
f"Failed to fetch document by id: {str(e)}",
)

View File

@ -21,8 +21,8 @@ schema = {
"name": "entities",
"enable_nested_fields": True,
"fields": [
{"name": "filepath", "type": "string"},
{"name": "filename", "type": "string"},
{"name": "filepath", "type": "string", "infix": True},
{"name": "filename", "type": "string", "infix": True},
{"name": "size", "type": "int32"},
{"name": "file_created_at", "type": "int64", "facet": False},
{"name": "file_last_modified_at", "type": "int64", "facet": False},
@ -55,7 +55,7 @@ schema = {
"optional": True,
},
],
"token_separators": [":", "/", ".", " ", "-", "\\"],
"token_separators": [":", "/", " ", "\\"],
}

View File

@ -317,6 +317,23 @@ async def sync_entity_to_typesense(entity_id: int, db: Session = Depends(get_db)
return None
@app.get(
"/entities/{entity_id}/index",
response_model=EntitySearchResult,
tags=["entity"],
)
async def get_entity_index(entity_id: int) -> EntityIndexItem:
try:
entity_index_item = indexing.fetch_entity_by_id(client, entity_id)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=str(e),
)
return entity_index_item
@app.delete(
"/entities/{entity_id}/index",
status_code=status.HTTP_204_NO_CONTENT,