mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-07-23 08:52:16 +00:00

* Add endpoint to get documents by filter * Add test for /documents/get_by_filter and extend the delete documents test * Add rest_api/file-upload to .gitignore * Make sure the document store is empty for each test * Improve docstrings of delete_documents_by_filters and get_documents_by_filters Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from typing import List
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter
|
|
from haystack import Document
|
|
|
|
from rest_api.controller.search import DOCUMENT_STORE
|
|
from rest_api.config import LOG_LEVEL
|
|
from rest_api.schema import FilterRequest, DocumentResponse
|
|
|
|
|
|
logging.getLogger("haystack").setLevel(LOG_LEVEL)
|
|
logger = logging.getLogger("haystack")
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@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]
|
|
|
|
|
|
@router.post("/documents/delete_by_filters", response_model=bool)
|
|
def delete_documents_by_filter(filters: FilterRequest):
|
|
"""
|
|
Can be used to delete documents from a document store.
|
|
|
|
:param filters: Filters to narrow down the documents to delete.
|
|
Example: '{"filters": {{"name": ["some", "more"], "category": ["only_one"]}}'
|
|
To delete all documents you should provide an empty dict, like:
|
|
'{"filters": {}}'
|
|
"""
|
|
DOCUMENT_STORE.delete_documents(filters=filters.filters)
|
|
return True |