Skip to content

Commit 87296fa

Browse files
authored
Merge pull request #687 from sanders41/sync_client
Add a sync client
2 parents b6f9942 + ff335b9 commit 87296fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+12570
-5319
lines changed

.github/workflows/pypi_publish.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v4
1111
- name: Install Poetry
12-
run: |
13-
pip install pipx
14-
pipx install poetry
12+
run: pipx install poetry
13+
- name: Add pypi token to Poetry
14+
run: poetry config pypi-token.pypi ${{ secrets.PYPI_API_KEY }}
1515
- name: Set up Python
1616
uses: actions/setup-python@v4
1717
with:
1818
python-version: 3.11
1919
cache: "poetry"
2020
- name: Install Dependencies
21-
run: |
22-
poetry install
23-
- name: Add pypi token to Poetry
24-
run: |
25-
poetry config pypi-token.pypi ${{ secrets.PYPI_API_KEY }}
21+
run: poetry install
2622
- name: Publish package
2723
run: poetry publish --build
2824
- name: Deploy Docs

CONTRIBUTING.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.
66

7-
The best place to start is to check the [issues](https://github.com/sanders41/meilisearch-python-async/issues)
7+
The best place to start is to check the [issues](https://github.com/sanders41/meilisearch-python-sdk/issues)
88
for something that interests you.
99

1010
## Bug Reports
@@ -15,7 +15,7 @@ Please include:
1515
[GitHub markdown](https://docs.github.com/en/free-pro-team@latest/github/writing-on-github). For example:
1616

1717
```py
18-
from meilisearch_python_async import Client
18+
from meilisearch_python_sdk import Client
1919

2020
async with Client(BASE_URL, MASTER_KEY) as client:
2121
client.index("movies")
@@ -34,12 +34,12 @@ this project.
3434
Once the project is forked clone it to your local machine:
3535

3636
```sh
37-
git clone https://github.com/your-user-name/meilisearch-python-async.git
38-
cd meilisearch-python-async
39-
git remote add upstream https://github.com/sanders41/meilisearch-python-async.git
37+
git clone https://github.com/your-user-name/meilisearch-python-sdk.git
38+
cd meilisearch-python-sdk
39+
git remote add upstream https://github.com/sanders41/meilisearch-python-sdk.git
4040
```
4141

42-
This creates the directory meilisearch-python-async and connects your repository to the upstream (main project) repository.
42+
This creates the directory meilisearch-python-sdk and connects your repository to the upstream (main project) repository.
4343

4444
### Working with the code
4545

@@ -74,21 +74,21 @@ git pull upstream main --ff-only
7474

7575
### Code Standards and tests (ruff, black, mypy, pytest, and pre-commit)
7676

77-
meilisearch-python-async uses [ruff](https://github.com/charliermarsh/ruff),
77+
meilisearch-python-sdk uses [ruff](https://github.com/charliermarsh/ruff),
7878
[Black](https://github.com/psf/black), and [mypy](https://mypy.readthedocs.io/en/stable/) to ensure
7979
consistent code formatting.
8080

8181
You can run linting on your code at any time with:
8282

8383
```sh
8484
# Run black
85-
poetry run black meilisearch_python_async tests
85+
poetry run black meilisearch_python_sdk tests
8686

8787
# Run ruff
8888
poetry run ruff check .
8989

9090
# Run mypy
91-
poetry run mypy meilisearch_python_async
91+
poetry run mypy meilisearch_python_sdk
9292
```
9393

9494
It is also suggested that you setup [pre-commit](https://pre-commit.com/) in order to run linting
@@ -140,7 +140,7 @@ This project uses [pytest](https://docs.pytest.org/en/stable/) for testing. Plea
140140
additions/changes you make to the code have tests to go along with them. Code coverage should not
141141
drop blow it's current level with any pull requests you make, if it does the pull request will not
142142
be accepted. You can view the current coverage level in the codecov badge on the
143-
[main github page](https://github.com/sanders41/meilisearch-python-async). You can run tests and see the
143+
[main github page](https://github.com/sanders41/meilisearch-python-sdk). You can run tests and see the
144144
code coverage.
145145

146146
There are multiple way Meilisearch can be setup for testing. The examples below use docker, however
@@ -186,7 +186,7 @@ To see a full list of `just` commands run `just --list`
186186

187187
Documentation is automatically generated based on the doc strings from the functions/methods. If
188188
functions/methods are added/removed make sure to update the
189-
[api documentation page](https://github.com/sanders41/meilisearch-python-async/docs/api.md) accordingly.
189+
[api documentation page](https://github.com/sanders41/meilisearch-python-sdk/docs/api.md) accordingly.
190190

191191
You can view any changes to the docs locally by running:
192192

@@ -238,11 +238,11 @@ git remote -v
238238

239239
## Making a Pull Request
240240

241-
After pushing your code to origin it is now on GitHub but not yet part of the meilisearch-python-async project. When you’re ready to ask for a code review, file a pull request. Before you do, once again make sure that you have followed all the guidelines outlined in this document regarding code style, tests, and documentation.
241+
After pushing your code to origin it is now on GitHub but not yet part of the meilisearch-python-sdk project. When you’re ready to ask for a code review, file a pull request. Before you do, once again make sure that you have followed all the guidelines outlined in this document regarding code style, tests, and documentation.
242242

243243
### Make the pull request
244244

245-
If everything looks good, you are ready to make a pull request. This is how you let the maintainers of the meilisearch-python-async project know you have code ready to be reviewed. To submit the pull request:
245+
If everything looks good, you are ready to make a pull request. This is how you let the maintainers of the meilisearch-python-sdk project know you have code ready to be reviewed. To submit the pull request:
246246

247247
1. Navigate to your repository on GitHub
248248
2. Click on the Pull Request button for your feature branch

README.md

Lines changed: 214 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,222 @@
1-
# Meilisearch Python Async
1+
# Meilisearch Python SDK
22

3-
:warning:
4-
This project has been renamed to [meilisearch-python-sdk](https://github.com/sanders41/meilisearch-python-sdk)
5-
:warning:
3+
[![Tests Status](https://github.com/sanders41/meilisearch-python-sdk/workflows/Testing/badge.svg?branch=main&event=push)](https://github.com/sanders41/meilisearch-python-sdk/actions?query=workflow%3ATesting+branch%3Amain+event%3Apush)
4+
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/sanders41/meilisearch-python-sdk/main.svg)](https://results.pre-commit.ci/latest/github/sanders41/meilisearch-python-sdk/main)
5+
[![Coverage](https://codecov.io/github/sanders41/meilisearch-python-sdk/coverage.svg?branch=main)](https://codecov.io/gh/sanders41/meilisearch-python-sdk)
6+
[![PyPI version](https://badge.fury.io/py/meilisearch-python-sdk.svg)](https://badge.fury.io/py/meilisearch-python-sdk)
7+
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/meilisearch-python-sdk?color=5cc141)](https://github.com/sanders41/meilisearch-python-sdk)
68

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.
921

1022
## Installation
1123

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+
1227
```sh
1328
pip install meilisearch-python-sdk
1429
```
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+
![Add Documents in Batches](https://raw.githubusercontent.com/sanders41/meilisearch-python-sdk/main/assets/add_in_batches.png)
202+
203+
### Muiltiple Searches
204+
205+
This test compares how long it takes to complete 1000 searches (lower is better)
206+
207+
![Multiple Searches](https://raw.githubusercontent.com/sanders41/meilisearch-python-sdk/main/assets/searches.png)
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)

assets/add_in_batches.png

16.6 KB
Loading

assets/searches.png

2.4 KB
Loading

0 commit comments

Comments
 (0)