Skip to content

Commit

Permalink
Merge pull request #280 from sanders41/meilisearch-v0.28
Browse files Browse the repository at this point in the history
Update for Meilisearch v0.28
  • Loading branch information
sanders41 authored Aug 1, 2022
2 parents 5f6d81f + d146bb1 commit 3b3e795
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 338 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
- name: Install Dependencies
run: poetry install
- name: MeiliSearch (latest version) setup with Docker
run: docker run -d -p 7700:7700 getmeili/meilisearch:v0.27.2 meilisearch --no-analytics --master-key=masterKey
run: docker run -d -p 7700:7700 getmeili/meilisearch:v0.28.1 meilisearch --no-analytics --master-key=masterKey
- name: Test with pytest
run: |
poetry run pytest --cov=meilisearch_fastapi --cov-report=xml
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: mypy
- repo: https://github.com/PyCQA/flake8
rev: '5.0.0'
rev: '5.0.1'
hooks:
- id: flake8
additional_dependencies: [flake8-print]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Now the Meilisearch routes will be available in your FastAPI app. Documentation

## Compatibility with Meilisearch

This package only guarantees the compatibility with [version v0.27 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.27.0).
This package only guarantees the compatibility with [version v0.28 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.28.0).

## Contributing

Expand Down
4 changes: 2 additions & 2 deletions meilisearch_fastapi/models/search_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class SearchParameters(CamelBase):
offset: int = 0
limit: int = 20
filter: Optional[Union[str, List[Union[str, List[str]]]]] = None
facets_distribution: Optional[List[str]] = None
facets: Optional[List[str]] = None
attributes_to_retrieve: List[str] = ["*"]
attributes_to_crop: Optional[List[str]] = None
sort: Optional[List[str]] = None
crop_length: int = 200
attributes_to_highlight: Optional[List[str]] = None
matches: bool = False
show_matches_position: bool = False
highlight_pre_tag: str = "<em>"
highlight_post_tag: str = "</em>"
crop_marker: str = "..."
90 changes: 22 additions & 68 deletions meilisearch_fastapi/routes/document_routes.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,36 @@
from typing import Dict, List, Optional
from typing import List, Optional

from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends
from meilisearch_python_async import Client
from meilisearch_python_async.models.task import TaskStatus
from meilisearch_python_async.models.documents import DocumentsInfo
from meilisearch_python_async.models.task import TaskInfo

from meilisearch_fastapi._client import meilisearch_client
from meilisearch_fastapi.models.document_info import (
DocumentDelete,
DocumentInfo,
DocumentInfoAutoBatch,
DocumentInfoBatches,
)

router = APIRouter()


@router.post("/", response_model=TaskStatus, status_code=202, tags=["MeiliSearch Documents"])
@router.post("/", response_model=TaskInfo, status_code=202, tags=["MeiliSearch Documents"])
async def add_documents(
document_info: DocumentInfo,
client: Client = Depends(meilisearch_client),
) -> TaskStatus:
) -> TaskInfo:
index = client.index(document_info.uid)

return await index.add_documents(document_info.documents, document_info.primary_key)


@router.post(
"/auto-batch", response_model=List[TaskStatus], status_code=202, tags=["MeiliSearch Documents"]
)
async def add_documents_auto_batch(
document_info: DocumentInfoAutoBatch, client: Client = Depends(meilisearch_client)
) -> List[TaskStatus]:
index = client.index(document_info.uid)

if document_info.max_payload_size:
return await index.add_documents_auto_batch(
document_info.documents,
max_payload_size=document_info.max_payload_size,
primary_key=document_info.primary_key,
)

return await index.add_documents_auto_batch(
document_info.documents, primary_key=document_info.primary_key
)


@router.post(
"/batches", response_model=List[TaskStatus], status_code=202, tags=["MeiliSearch Documents"]
"/batches", response_model=List[TaskInfo], status_code=202, tags=["MeiliSearch Documents"]
)
async def add_documents_in_batches(
document_info: DocumentInfoBatches, client: Client = Depends(meilisearch_client)
) -> List[TaskStatus]:
) -> List[TaskInfo]:
index = client.index(document_info.uid)

return await index.add_documents_in_batches(
Expand All @@ -60,34 +40,32 @@ async def add_documents_in_batches(
)


@router.delete("/{uid}", response_model=TaskStatus, status_code=202, tags=["MeiliSearch Documents"])
async def delete_all_documents(
uid: str, client: Client = Depends(meilisearch_client)
) -> TaskStatus:
@router.delete("/{uid}", response_model=TaskInfo, status_code=202, tags=["MeiliSearch Documents"])
async def delete_all_documents(uid: str, client: Client = Depends(meilisearch_client)) -> TaskInfo:
index = client.index(uid)

return await index.delete_all_documents()


@router.delete(
"/{uid}/{document_id}",
response_model=TaskStatus,
response_model=TaskInfo,
status_code=202,
tags=["MeiliSearch Documents"],
)
async def delete_document(
uid: str, document_id: str, client: Client = Depends(meilisearch_client)
) -> TaskStatus:
) -> TaskInfo:
index = client.index(uid)

return await index.delete_document(document_id)


@router.post("/delete", response_model=TaskStatus, status_code=202, tags=["MeiliSearch Documents"])
@router.post("/delete", response_model=TaskInfo, status_code=202, tags=["MeiliSearch Documents"])
async def delete_documents(
documents: DocumentDelete,
client: Client = Depends(meilisearch_client),
) -> TaskStatus:
) -> TaskInfo:
index = client.index(documents.uid)

return await index.delete_documents(documents.document_ids)
Expand All @@ -104,66 +82,42 @@ async def get_document(
return await index.get_document(document_id)


@router.get("/{uid}", response_model=List[Dict], tags=["MeiliSearch Documents"])
@router.get("/{uid}", response_model=DocumentsInfo, tags=["MeiliSearch Documents"])
async def get_documents(
uid: str,
limit: int = 20,
offset: int = 0,
attributes_to_retrieve: Optional[List[str]] = None,
fields: Optional[List[str]] = None,
client: Client = Depends(meilisearch_client),
) -> List[dict]:
) -> DocumentsInfo:
index = client.index(uid)

documents = await index.get_documents(
offset=offset,
limit=limit,
attributes_to_retrieve=attributes_to_retrieve,
fields=fields,
)

if documents is None:
raise HTTPException(404, "No documents found")

return documents


@router.put("/", response_model=TaskStatus, status_code=202, tags=["MeiliSearch Documents"])
@router.put("/", response_model=TaskInfo, status_code=202, tags=["MeiliSearch Documents"])
async def update_documents(
document_info: DocumentInfo,
client: Client = Depends(meilisearch_client),
) -> TaskStatus:
) -> TaskInfo:
index = client.index(document_info.uid)

return await index.update_documents(document_info.documents, document_info.primary_key)


@router.put(
"/auto-batch", response_model=List[TaskStatus], status_code=202, tags=["MeiliSearch Documents"]
)
async def update_documents_auto_batch(
document_info: DocumentInfoAutoBatch,
client: Client = Depends(meilisearch_client),
) -> List[TaskStatus]:
index = client.index(document_info.uid)

if document_info.max_payload_size:
return await index.update_documents_auto_batch(
document_info.documents,
max_payload_size=document_info.max_payload_size,
primary_key=document_info.primary_key,
)

return await index.update_documents_auto_batch(
document_info.documents, primary_key=document_info.primary_key
)


@router.put(
"/batches", response_model=List[TaskStatus], status_code=202, tags=["MeiliSearch Documents"]
"/batches", response_model=List[TaskInfo], status_code=202, tags=["MeiliSearch Documents"]
)
async def update_documents_in_batches(
document_info: DocumentInfoBatches,
client: Client = Depends(meilisearch_client),
) -> List[TaskStatus]:
) -> List[TaskInfo]:
index = client.index(document_info.uid)

return await index.update_documents_in_batches(
Expand Down
Loading

0 comments on commit 3b3e795

Please sign in to comment.