|
1 | | -# Meilisearch Python Async |
| 1 | +# Meilisearch Python SDK |
2 | 2 |
|
3 | | -:warning: |
4 | | -This project has been renamed to [meilisearch-python-sdk](https://github.com/sanders41/meilisearch-python-sdk) |
5 | | -:warning: |
| 3 | +[](https://github.com/sanders41/meilisearch-python-sdk/actions?query=workflow%3ATesting+branch%3Amain+event%3Apush) |
| 4 | +[](https://results.pre-commit.ci/latest/github/sanders41/meilisearch-python-sdk/main) |
| 5 | +[](https://codecov.io/gh/sanders41/meilisearch-python-sdk) |
| 6 | +[](https://badge.fury.io/py/meilisearch-python-sdk) |
| 7 | +[](https://github.com/sanders41/meilisearch-python-sdk) |
6 | 8 |
|
7 | | -Under the new name both an async and a sync client are provided. You should install the new package |
8 | | -instead of this one. |
| 9 | +:warning: This project was previously named `meilisearch-python-async`. Development on that project |
| 10 | +continues here under the new name. |
| 11 | + |
| 12 | +Meilisearch Python SDK provides both an async and sync client for the |
| 13 | +[Meilisearch](https://github.com/meilisearch/meilisearch) API. |
| 14 | + |
| 15 | +Which client to use depends on your use case. If the code base you are working with uses asyncio, |
| 16 | +for example if you are using [FastAPI](https://fastapi.tiangolo.com/), chose the `AsyncClint` |
| 17 | +otherwise chose the `Client`. The functionality of the two clients is the same, the difference |
| 18 | +being the `AsyncClient` provides async methods and uses the `AsyncIndex`, which also provides async |
| 19 | +methods, while the `Client` provides blocking methods and uses the `Index`, which also provides |
| 20 | +blocking methods. |
9 | 21 |
|
10 | 22 | ## Installation |
11 | 23 |
|
| 24 | +Using a virtual environmnet is recommended for installing this package. Once the virtual |
| 25 | +environment is created and activated install the package with: |
| 26 | + |
12 | 27 | ```sh |
13 | 28 | pip install meilisearch-python-sdk |
14 | 29 | ``` |
| 30 | + |
| 31 | +## Run Meilisearch |
| 32 | + |
| 33 | +There are several ways to |
| 34 | +[run Meilisearch](https://docs.meilisearch.com/reference/features/installation.html#download-and-launch). |
| 35 | +Pick the one that works best for your use case and then start the server. |
| 36 | + |
| 37 | +As as example to use Docker: |
| 38 | + |
| 39 | +```sh |
| 40 | +docker pull getmeili/meilisearch:latest |
| 41 | +docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey |
| 42 | +``` |
| 43 | + |
| 44 | +## Useage |
| 45 | + |
| 46 | +### Add Documents |
| 47 | + |
| 48 | + |
| 49 | +#### AsyncClient |
| 50 | + |
| 51 | +- Note: `client.index("books") creates an instance of an AsyncIndex object but does not make a |
| 52 | +network call to send the data yet so it does not need to be awaited. |
| 53 | + |
| 54 | +```py |
| 55 | +from meilisearch_python_sdk import AsyncClient |
| 56 | + |
| 57 | +async with AsyncClient('http://127.0.0.1:7700', 'masterKey') as client: |
| 58 | + index = client.index("books") |
| 59 | + |
| 60 | + documents = [ |
| 61 | + {"id": 1, "title": "Ready Player One"}, |
| 62 | + {"id": 42, "title": "The Hitchhiker's Guide to the Galaxy"}, |
| 63 | + ] |
| 64 | + |
| 65 | + await index.add_documents(documents) |
| 66 | +``` |
| 67 | + |
| 68 | +#### Client |
| 69 | + |
| 70 | +```py |
| 71 | +from meilisearch_python_sdk import Client |
| 72 | + |
| 73 | +client = Client('http://127.0.0.1:7700', 'masterKey') |
| 74 | +index = client.index("books") |
| 75 | + |
| 76 | +documents = [ |
| 77 | + {"id": 1, "title": "Ready Player One"}, |
| 78 | + {"id": 42, "title": "The Hitchhiker's Guide to the Galaxy"}, |
| 79 | +] |
| 80 | + |
| 81 | +index.add_documents(documents) |
| 82 | +``` |
| 83 | + |
| 84 | +The server will return an update id that can be used to |
| 85 | +[get the status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status) |
| 86 | +of the updates. To do this you would save the result response from adding the documets to a |
| 87 | +variable, this will be a UpdateId object, and use it to check the status of the updates. |
| 88 | + |
| 89 | +#### AsyncClient |
| 90 | + |
| 91 | +```py |
| 92 | +update = await index.add_documents(documents) |
| 93 | +status = await client.index('books').get_update_status(update.update_id) |
| 94 | +``` |
| 95 | + |
| 96 | +#### Client |
| 97 | + |
| 98 | +```py |
| 99 | +update = index.add_documents(documents) |
| 100 | +status = client.index('books').get_update_status(update.update_id) |
| 101 | +``` |
| 102 | + |
| 103 | +### Basic Searching |
| 104 | + |
| 105 | +#### AsyncClient |
| 106 | + |
| 107 | +```py |
| 108 | +search_result = await index.search("ready player") |
| 109 | +``` |
| 110 | + |
| 111 | +#### Client |
| 112 | + |
| 113 | +```py |
| 114 | +search_result = index.search("ready player") |
| 115 | +``` |
| 116 | + |
| 117 | +### Base Search Results: SearchResults object with values |
| 118 | + |
| 119 | +```py |
| 120 | +SearchResults( |
| 121 | + hits = [ |
| 122 | + { |
| 123 | + "id": 1, |
| 124 | + "title": "Ready Player One", |
| 125 | + }, |
| 126 | + ], |
| 127 | + offset = 0, |
| 128 | + limit = 20, |
| 129 | + nb_hits = 1, |
| 130 | + exhaustive_nb_hits = bool, |
| 131 | + facets_distributionn = None, |
| 132 | + processing_time_ms = 1, |
| 133 | + query = "ready player", |
| 134 | +) |
| 135 | +``` |
| 136 | + |
| 137 | +### Custom Search |
| 138 | + |
| 139 | +Information about the parameters can be found in the |
| 140 | +[search parameters](https://docs.meilisearch.com/reference/features/search_parameters.html) section |
| 141 | +of the documentation. |
| 142 | + |
| 143 | +#### AsyncClient |
| 144 | + |
| 145 | +```py |
| 146 | +await index.search( |
| 147 | + "guide", |
| 148 | + attributes_to_highlight=["title"], |
| 149 | + filters="book_id > 10" |
| 150 | +) |
| 151 | +``` |
| 152 | + |
| 153 | +#### Client |
| 154 | + |
| 155 | +```py |
| 156 | +index.search( |
| 157 | + "guide", |
| 158 | + attributes_to_highlight=["title"], |
| 159 | + filters="book_id > 10" |
| 160 | +) |
| 161 | +``` |
| 162 | + |
| 163 | +### Custom Search Results: SearchResults object with values |
| 164 | + |
| 165 | +```py |
| 166 | +SearchResults( |
| 167 | + hits = [ |
| 168 | + { |
| 169 | + "id": 42, |
| 170 | + "title": "The Hitchhiker's Guide to the Galaxy", |
| 171 | + "_formatted": { |
| 172 | + "id": 42, |
| 173 | + "title": "The Hitchhiker's Guide to the <em>Galaxy</em>" |
| 174 | + } |
| 175 | + }, |
| 176 | + ], |
| 177 | + offset = 0, |
| 178 | + limit = 20, |
| 179 | + nb_hits = 1, |
| 180 | + exhaustive_nb_hits = bool, |
| 181 | + facets_distributionn = None, |
| 182 | + processing_time_ms = 5, |
| 183 | + query = "galaxy", |
| 184 | +) |
| 185 | +``` |
| 186 | + |
| 187 | +## Benchmark |
| 188 | + |
| 189 | +The following benchmarks compare this library to the official |
| 190 | +[Meilisearch Python](https://github.com/meilisearch/meilisearch-python) library. Note that all |
| 191 | +of the performance gains seen with the `AsyncClient` are achieved by taking advantage of asyncio. |
| 192 | +This means that if your code is not taking advantage of asyncio or blocking the event loop the |
| 193 | +gains here will not be seen and the performance between the clients will be very close to the same. |
| 194 | + |
| 195 | +### Add Documents in Batches |
| 196 | + |
| 197 | +This test compares how long it takes to send 1 million documents in batches of 1 thousand to the |
| 198 | +Meilisearch server for indexing (lower is better). The time does not take into account how long |
| 199 | +Meilisearch takes to index the documents since that is outside of the library functionality. |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +### Muiltiple Searches |
| 204 | + |
| 205 | +This test compares how long it takes to complete 1000 searches (lower is better) |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +### Independent testing |
| 210 | + |
| 211 | +[Prashanth Rao](https://github.com/prrao87) did some independent testing and found this async client |
| 212 | +to be ~30% faster than the sync client for data ingestion. You can find a good write-up of the |
| 213 | +results how he tested them in his [blog post](https://thedataquarry.com/posts/meilisearch-async/). |
| 214 | + |
| 215 | +## Documentation |
| 216 | + |
| 217 | +See our [docs](https://meilisearch-python-sdk.paulsanders.dev) for the full documentation. |
| 218 | + |
| 219 | +## Contributing |
| 220 | + |
| 221 | +Contributions to this project are welcome. If you are interested in contributing please see our |
| 222 | +[contributing guide](CONTRIBUTING.md) |
0 commit comments