Meilisearch Python SDK는 Meilisearch API를 위한 비동기 및 동기화 클라이언트를 모두 제공합니다.
사용할 클라이언트는 사용 사례에 따라 다릅니다. 작업 중인 코드 베이스가 asyncio를 사용하는 경우(예: FastAPI를 사용하는 경우) AsyncClient
선택하고, 그렇지 않으면 sync Client
선택하세요. 두 클라이언트의 기능은 동일합니다. 차이점은 AsyncClient
비동기 메서드를 제공하고 자체 추가 비동기 메서드와 함께 AsyncIndex
사용한다는 것입니다. 반면에 Client
차단 방법을 제공하고 자체 차단 방법으로 Index
를 사용합니다.
이 패키지를 설치하려면 가상 환경을 사용하는 것이 좋습니다. 가상 환경이 생성되고 활성화되면 다음을 사용하여 패키지를 설치하십시오.
pip install meilisearch-python-sdk
Meilisearch를 실행하는 방법에는 여러 가지가 있습니다. 귀하의 사용 사례에 가장 적합한 것을 선택한 다음 서버를 시작하십시오.
Docker를 사용하는 예를 들면 다음과 같습니다.
docker pull getmeili/meilisearch:latest
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
from meilisearch_python_sdk import AsyncClient
async with AsyncClient ( 'http://127.0.0.1:7700' , 'masterKey' ) as client :
index = client . index ( "books" )
documents = [
{ "id" : 1 , "title" : "Ready Player One" },
{ "id" : 42 , "title" : "The Hitchhiker's Guide to the Galaxy" },
]
await index . add_documents ( documents )
from meilisearch_python_sdk import Client
client = Client ( 'http://127.0.0.1:7700' , 'masterKey' )
index = client . index ( "books" )
documents = [
{ "id" : 1 , "title" : "Ready Player One" },
{ "id" : 42 , "title" : "The Hitchhiker's Guide to the Galaxy" },
]
index . add_documents ( documents )
서버는 업데이트 상태를 가져오는 데 사용할 수 있는 업데이트 ID를 반환합니다. 이렇게 하려면 문서를 변수에 추가한 결과 응답을 저장해야 합니다. 이는 UpdateId
개체가 되며 이를 사용하여 업데이트 상태를 확인합니다.
update = await index . add_documents ( documents )
status = await client . index ( 'books' ). get_update_status ( update . update_id )
update = index . add_documents ( documents )
status = client . index ( 'books' ). get_update_status ( update . update_id )
search_result = await index . search ( "ready player" )
search_result = index . search ( "ready player" )
SearchResults (
hits = [
{
"id" : 1 ,
"title" : "Ready Player One" ,
},
],
offset = 0 ,
limit = 20 ,
nb_hits = 1 ,
exhaustive_nb_hits = bool ,
facets_distributionn = None ,
processing_time_ms = 1 ,
query = "ready player" ,
)
매개변수에 대한 정보는 설명서의 검색 매개변수 섹션에서 찾을 수 있습니다.
await index . search (
"guide" ,
attributes_to_highlight = [ "title" ],
filters = "book_id > 10"
)
index . search (
"guide" ,
attributes_to_highlight = [ "title" ],
filters = "book_id > 10"
)
SearchResults (
hits = [
{
"id" : 42 ,
"title" : "The Hitchhiker's Guide to the Galaxy" ,
"_formatted" : {
"id" : 42 ,
"title" : "The Hitchhiker's Guide to the <em>Galaxy</em>"
}
},
],
offset = 0 ,
limit = 20 ,
nb_hits = 1 ,
exhaustive_nb_hits = bool ,
facets_distributionn = None ,
processing_time_ms = 5 ,
query = "galaxy" ,
)
다음 벤치마크는 이 라이브러리를 공식 Meilisearch Python 라이브러리와 비교합니다. AsyncClient
에서 볼 수 있는 모든 성능 향상은 asyncio를 활용하여 달성됩니다. 이는 코드가 asyncio를 활용하지 않거나 이벤트 루프를 차단하지 않는 경우 여기서의 이점은 표시되지 않으며 클라이언트 간의 성능은 매우 유사하다는 것을 의미합니다.
이 테스트는 인덱싱을 위해 100만 개의 문서를 1,000개 묶음으로 Meilisearch 서버로 보내는 데 걸리는 시간을 비교합니다(낮을수록 좋음). Meilisearch가 문서를 색인화하는 데 걸리는 시간은 라이브러리 기능 외부에 있으므로 시간은 고려하지 않습니다.
이 테스트는 1000개의 검색을 완료하는 데 걸리는 시간을 비교합니다(낮을수록 좋음).
Prashanth Rao는 몇 가지 독립적인 테스트를 수행한 결과 이 비동기 클라이언트가 데이터 수집에 있어서 동기화 클라이언트보다 최대 30% 더 빠른 것으로 나타났습니다. 그의 블로그 게시물에서 그가 테스트한 결과에 대한 좋은 글을 찾을 수 있습니다.
pytest-meilisearch는 코드 테스트에 도움이 되는 pytest 플러그인입니다. 필요한 보일러 플레이트 코드를 많이 제공합니다.
전체 문서를 보려면 문서를 참조하세요.
이 프로젝트에 대한 기여를 환영합니다. 기여에 관심이 있다면 기여 가이드를 참조하세요.