Skip to content

Commit 7f7d066

Browse files
committed
Add example to README
1 parent 4b6ca92 commit 7f7d066

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ index.update_filterable_attributes([
169169
])
170170
```
171171

172+
#### Custom Serializer for documents <!-- omit in toc -->
173+
174+
If your documents contain fields that the Python JSON serializer does not know how to handle you
175+
can use your own custom serializer.
176+
177+
```py
178+
from datetime import datetime
179+
from json import JSONEncoder
180+
from uuid import uuid4
181+
182+
183+
class CustomEncoder(JSONEncoder):
184+
def default(self, o):
185+
if isinstance(o, (UUID, datetime)):
186+
return str(o)
187+
188+
# Let the base class default method raise the TypeError
189+
return super().default(o)
190+
191+
192+
documents = [
193+
{"id": uuid4(), "title": "test 1", "when": datetime.now()},
194+
{"id": uuid4(), "title": "Test 2", "when": datetime.now()},
195+
]
196+
index = empty_index()
197+
index.add_documents(documents, serializer=CustomEncoder)
198+
```
199+
172200
You only need to perform this operation once.
173201

174202
Note that Meilisearch will rebuild your index whenever you update `filterableAttributes`. Depending on the size of your dataset, this might take time. You can track the process using the [task](https://www.meilisearch.com/docs/reference/api/tasks#get-tasks).
@@ -205,7 +233,6 @@ index.search(
205233

206234
This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-python/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
207235

208-
209236
## 💡 Learn more
210237

211238
The following sections in our main documentation website may interest you:

0 commit comments

Comments
 (0)