mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-08 20:25:30 +00:00
feat(indexing): add api fetch document in typesense
This commit is contained in:
parent
b5d524f7b0
commit
38272c48d0
@ -137,7 +137,8 @@ def search_entities(
|
|||||||
|
|
||||||
search_parameters = {
|
search_parameters = {
|
||||||
"q": q,
|
"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",
|
"filter_by": f"{filter_by_str} && file_type_group:=image" if filter_by_str else "file_type_group:=image",
|
||||||
"per_page": limit,
|
"per_page": limit,
|
||||||
"page": offset // limit + 1,
|
"page": offset // limit + 1,
|
||||||
@ -174,3 +175,32 @@ def search_entities(
|
|||||||
raise Exception(
|
raise Exception(
|
||||||
f"Failed to search entities: {str(e)}",
|
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)}",
|
||||||
|
)
|
||||||
|
@ -21,8 +21,8 @@ schema = {
|
|||||||
"name": "entities",
|
"name": "entities",
|
||||||
"enable_nested_fields": True,
|
"enable_nested_fields": True,
|
||||||
"fields": [
|
"fields": [
|
||||||
{"name": "filepath", "type": "string"},
|
{"name": "filepath", "type": "string", "infix": True},
|
||||||
{"name": "filename", "type": "string"},
|
{"name": "filename", "type": "string", "infix": True},
|
||||||
{"name": "size", "type": "int32"},
|
{"name": "size", "type": "int32"},
|
||||||
{"name": "file_created_at", "type": "int64", "facet": False},
|
{"name": "file_created_at", "type": "int64", "facet": False},
|
||||||
{"name": "file_last_modified_at", "type": "int64", "facet": False},
|
{"name": "file_last_modified_at", "type": "int64", "facet": False},
|
||||||
@ -55,7 +55,7 @@ schema = {
|
|||||||
"optional": True,
|
"optional": True,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"token_separators": [":", "/", ".", " ", "-", "\\"],
|
"token_separators": [":", "/", " ", "\\"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -317,6 +317,23 @@ async def sync_entity_to_typesense(entity_id: int, db: Session = Depends(get_db)
|
|||||||
return None
|
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(
|
@app.delete(
|
||||||
"/entities/{entity_id}/index",
|
"/entities/{entity_id}/index",
|
||||||
status_code=status.HTTP_204_NO_CONTENT,
|
status_code=status.HTTP_204_NO_CONTENT,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user