Skip to main content

A Python client providing both async and sync support for the Meilisearch API

Project description

Meilisearch Python SDK

Tests Status pre-commit.ci status Coverage PyPI version PyPI - Python Version

Meilisearch Python SDK provides both an async and sync client for the Meilisearch API.

Which client to use depends on your use case. If the code base you are working with uses asyncio, for example if you are using FastAPI, choose the AsyncClient, otherwise choose the sync Client. The functionality of the two clients is the same, the difference being that the AsyncClient provides async methods and uses the AsyncIndex with its own additional async methods. On the other hand, Client provides blocking methods and uses the Index with its own blocking methods.

Installation

Using a virtual environment is recommended for installing this package. Once the virtual environment is created and activated, install the package with:

pip install meilisearch-python-sdk

Run Meilisearch

There are several ways to run Meilisearch. Pick the one that works best for your use case and then start the server.

As as example to use Docker:

docker pull getmeili/meilisearch:latest
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

Usage

Add Documents

AsyncClient

  • Note: `client.index("books") creates an instance of an AsyncIndex object but does not make a network call to send the data yet so it does not need to be awaited.
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)

Client

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)

The server will return an update id that can be used to get the status of the updates. To do this you would save the result response from adding the documents to a variable, this will be an UpdateId object, and use it to check the status of the updates.

AsyncClient

update = await index.add_documents(documents)
status = await client.index('books').get_update_status(update.update_id)

Client

update = index.add_documents(documents)
status = client.index('books').get_update_status(update.update_id)

Basic Searching

AsyncClient

search_result = await index.search("ready player")

Client

search_result = index.search("ready player")

Base Search Results: SearchResults object with values

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",
)

Custom Search

Information about the parameters can be found in the search parameters section of the documentation.

AsyncClient

await index.search(
    "guide",
    attributes_to_highlight=["title"],
    filters="book_id > 10"
)

Client

index.search(
    "guide",
    attributes_to_highlight=["title"],
    filters="book_id > 10"
)

Custom Search Results: SearchResults object with values

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",
)

Benchmark

The following benchmarks compare this library to the official Meilisearch Python library. Note that all of the performance gains seen with the AsyncClient are achieved by taking advantage of asyncio. This means that if your code is not taking advantage of asyncio or it does not block the event loop, the gains here will not be seen and the performance between the clients will be very similar.

Add Documents in Batches

This test compares how long it takes to send 1 million documents in batches of 1 thousand to the Meilisearch server for indexing (lower is better). The time does not take into account how long Meilisearch takes to index the documents since that is outside of the library functionality.

Add Documents in Batches

Muiltiple Searches

This test compares how long it takes to complete 1000 searches (lower is better)

Multiple Searches

Independent testing

Prashanth Rao did some independent testing and found this async client to be ~30% faster than the sync client for data ingestion. You can find a good write-up of the results how he tested them in his blog post.

Testing

pytest-meilisearch is a pytest plugin that can help with testing your code. It provides a lot of the boiler plate code you will need.

Documentation

See our docs for the full documentation.

Contributing

Contributions to this project are welcome. If you are interested in contributing please see our contributing guide

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

meilisearch_python_sdk-2.11.1.tar.gz (48.3 kB view details)

Uploaded Source

Built Distribution

meilisearch_python_sdk-2.11.1-py3-none-any.whl (54.4 kB view details)

Uploaded Python 3

File details

Details for the file meilisearch_python_sdk-2.11.1.tar.gz.

File metadata

  • Download URL: meilisearch_python_sdk-2.11.1.tar.gz
  • Upload date:
  • Size: 48.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1022-azure

File hashes

Hashes for meilisearch_python_sdk-2.11.1.tar.gz
Algorithm Hash digest
SHA256 58904fe5c6b372fe93c25a3e9fd91dd35d21613056e280a1b2ea71edaca56a1d
MD5 aabfee216eb1c09351763386e00a136e
BLAKE2b-256 f8d104a8ad03380dddbd4cb5d45e3676a41c7c85fbfadffe39b8d9bf7f9d0ddc

See more details on using hashes here.

File details

Details for the file meilisearch_python_sdk-2.11.1-py3-none-any.whl.

File metadata

File hashes

Hashes for meilisearch_python_sdk-2.11.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54b812cae319abbc288f59ad3eba3575da139cc29484484cfc7937dbf03dde88
MD5 881ca0c6cd5a0ee8b4e8b9c33d339b86
BLAKE2b-256 78c8085c59c2f3cd9a53099d66281dc2d5b11ea396141a42e126be507f5ef5e1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page