2021-10-12 10:53:54 +02:00
|
|
|
from typing import List
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from fastapi import APIRouter
|
2021-10-12 10:53:54 +02:00
|
|
|
from haystack import Document
|
2021-10-04 11:21:00 +02:00
|
|
|
|
2021-10-04 21:18:23 +02:00
|
|
|
from rest_api.controller.search import DOCUMENT_STORE
|
2021-10-04 11:21:00 +02:00
|
|
|
from rest_api.config import LOG_LEVEL
|
2021-10-12 10:53:54 +02:00
|
|
|
from rest_api.schema import FilterRequest, DocumentResponse
|
2021-10-04 11:21:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
logging.getLogger("haystack").setLevel(LOG_LEVEL)
|
|
|
|
logger = logging.getLogger("haystack")
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2021-10-12 10:53:54 +02:00
|
|
|
@router.post("/documents/get_by_filters", response_model=List[DocumentResponse])
|
|
|
|
def get_documents_by_filter(filters: FilterRequest):
|
|
|
|
"""
|
|
|
|
Can be used to get documents from a document store.
|
|
|
|
|
|
|
|
:param filters: Filters to narrow down the documents to delete.
|
|
|
|
Example: '{"filters": {{"name": ["some", "more"], "category": ["only_one"]}}'
|
|
|
|
To get all documents you should provide an empty dict, like:
|
|
|
|
'{"filters": {}}'
|
|
|
|
"""
|
|
|
|
docs = [doc.to_dict() for doc in DOCUMENT_STORE.get_all_documents(filters=filters.filters)]
|
|
|
|
for doc in docs:
|
|
|
|
del doc["embedding"]
|
|
|
|
return [DocumentResponse(**doc) for doc in docs]
|
|
|
|
|
|
|
|
|
2021-10-04 11:21:00 +02:00
|
|
|
@router.post("/documents/delete_by_filters", response_model=bool)
|
2021-10-12 10:53:54 +02:00
|
|
|
def delete_documents_by_filter(filters: FilterRequest):
|
2021-10-04 11:21:00 +02:00
|
|
|
"""
|
|
|
|
Can be used to delete documents from a document store.
|
|
|
|
|
|
|
|
:param filters: Filters to narrow down the documents to delete.
|
2021-10-12 10:53:54 +02:00
|
|
|
Example: '{"filters": {{"name": ["some", "more"], "category": ["only_one"]}}'
|
|
|
|
To delete all documents you should provide an empty dict, like:
|
|
|
|
'{"filters": {}}'
|
2021-10-04 11:21:00 +02:00
|
|
|
"""
|
|
|
|
DOCUMENT_STORE.delete_documents(filters=filters.filters)
|
|
|
|
return True
|