def file_download_router(index_name: str, path_property: str, tags: List[str] = ["download"]): router = APIRouter() query_builder = ElasticsearchAPIQueryBuilder() query_builder.set_size(1) query_builder.set_start_from(0) @query_builder.filter() def filter_config(id: str = Path( None, description="Id of the document to download.")): return {"ids": {"values": [id]}} @router.get("/download/{id}", tags=tags, response_class=FileResponse) async def download(query_body: Dict = Depends( query_builder.build(source=[path_property])), es_client: Elasticsearch = Depends(get_client), auth_header: Dict = Depends( get_auth_header)) -> FileResponse: resp = es_client.search(body=query_body, headers=auth_header, index=index_name) if resp["hits"]["total"]["value"] > 0: document_path = resp["hits"]["hits"][0]["_source"][path_property] if os.path.isfile(document_path): return FileResponse(document_path) else: raise HTTPException(status_code=404, detail="Document not found") else: raise HTTPException(status_code=404, detail="Document not found") return router
from typing import Dict, Optional from elasticsearch import Elasticsearch from fastapi import Path, Query from fastapi_elasticsearch import ElasticsearchAPIQueryBuilder query_builder = ElasticsearchAPIQueryBuilder() @query_builder.filter() def filter_config(id: str = Path(None, description="Id of the document.")) -> Dict: return {"parent_id": {"type": "fragment", "id": id}} @query_builder.matcher() def match_content(q: Optional[str] = Query( None, description="Query to match the document text.")) -> Dict: return { "match": { "content": { "query": q, "fuzziness": "auto" } } } if q is not None else None @query_builder.matcher() def match_content_phrase(q: Optional[str] = Query( None, description="Query to match the document text.")) -> Dict:
router = APIRouter() def start_up(): try: auth_header = get_admin_auth_header() su_client = start() if not su_client.indices.exists(index_name, headers=auth_header): logging.info(f"Index {index_name} not found. Creating one.") create_index(su_client, index_name, headers=auth_header) finally: su_client.close() query_builder = ElasticsearchAPIQueryBuilder(size=1, start_from=0) @query_builder.filter() def filter_config(id: str = Path( None, description="Id of the detail configuration.")): return {"ids": {"values": [id]}} class DetailConfigModel(ElasticsearchHitBaseModel): title: str type: str config: dict converter = ElasticsearchModelConverter(DetailConfigModel)
def preview_router(index_name: str, cache_path: str, path_property: str, tags: List[str] = ["preview"]): router = APIRouter() query_builder = ElasticsearchAPIQueryBuilder() conf = get_config() manager = PreviewManager(cache_path, create_folder=True) @query_builder.filter() def filter_config(id: str = Path(None, description="Id of the document to preview.")): return { "ids": { "values": [id] } } @router.get("/preview/{id}", tags=tags) async def preview( page: Optional[int] = Query(0, ge=0, description="The page of the document to generate the preview."), width: Optional[int] = Query(300, ge=1, le=1024, description="The width of the generated preview."), height: Optional[int] = Query(200, ge=1, le=1024, description="The height of the generated preview."), query_body: Dict = Depends(query_builder.build(source=[path_property])), es_client: Elasticsearch = Depends(get_client), auth_header: Dict = Depends(get_auth_header)) -> FileResponse: resp = es_client.search( body=query_body, headers=auth_header, index=index_name ) if resp["hits"]["total"]["value"] > 0: document_path = resp["hits"]["hits"][0]["_source"][path_property] path_to_preview_image = manager.get_jpeg_preview(document_path, page=page, width=width, height=height, ) return FileResponse(path_to_preview_image) else: raise HTTPException(status_code=404, detail="Document not found") @router.get("/preview/info/{id}", tags=tags, response_model=PreviewInfoModel) async def preview_info( query_body: Dict = Depends(query_builder.build(source=[path_property])), es_client: Elasticsearch = Depends(get_client), auth_header: Dict = Depends(get_auth_header)) -> FileResponse: resp = es_client.search( body=query_body, headers=auth_header, index=index_name ) if resp["hits"]["total"]["value"] > 0: document_path = resp["hits"]["hits"][0]["_source"][path_property] if os.path.isfile(document_path): supported = manager.has_jpeg_preview(document_path) pages = manager.get_page_nb(document_path) return PreviewInfoModel(supported=supported, pages=pages) else: return PreviewInfoModel(supported=False, pages=0) else: raise HTTPException(status_code=404, detail="Document not found") return router
ssl_show_warn=False, http_auth=("admin", "admin")) wait_elasticsearch(es) index_name = "sample-data" if not es.indices.exists(index_name): create_sample_index(es, index_name) load_sample_data(es, index_name) app = FastAPI() route_builder = ElasticsearchAPIRouteBuilder(es_client=es, index_name="sample-data") query_builder = ElasticsearchAPIQueryBuilder() @query_builder.filter() def filter_items(): return {"term": {"join_field": "item"}} @query_builder.filter() def filter_category(c: Optional[str] = Query( None, description="Category name to filter results.")): return {"term": {"category": c}} if c is not None else None @query_builder.matcher() def match_fields(q: Optional[str] = Query(
router = APIRouter() auth_header = get_admin_auth_header() class SampleBaseHitModel(ElasticsearchHitBaseModel): name: str category: str class SampleHitModel(SampleBaseHitModel): content_first_fragment: str content: ElasticsearchInnerHit[str] = [] query_builder = ElasticsearchAPIQueryBuilder() @query_builder.filter() def filter_items(): return {"term": {"join_field": "item"}} @query_builder.filter() def filter_category(c: Optional[str] = Query( None, description="Category name to filter results.")): return {"term": {"category": c}} if c is not None else None @query_builder.matcher() def match_fields(q: Optional[str] = Query(