- Notifications
You must be signed in to change notification settings - Fork93
Python wrapper for the Meilisearch API
License
meilisearch/meilisearch-python
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
⚡ The Meilisearch API client written for Python 🐍
Meilisearch Python is the Meilisearch API client for Python developers.
Meilisearch is an open-source search engine.Learn more about Meilisearch.
- 📖 Documentation
- 🔧 Installation
- 🚀 Getting started
- 🤖 Compatibility with Meilisearch
- 💡 Learn more
- ⚙️ Contributing
To learn more about Meilisearch Python, refer to the in-depthMeilisearch Python documentation. To learn more about Meilisearch in general, refer to ourdocumentation or ourAPI reference.
Note: Python 3.8+ is required.
Withpip3
in command line:
pip3 install meilisearch
⚡️Launch, scale, and streamline in minutes with Meilisearch Cloud—no maintenance, no commitment, cancel anytime.Try it free now.
🪨 Prefer to self-host?Download and deploy our fast, open-source search engine on your own infrastructure.
importmeilisearchclient=meilisearch.Client('http://127.0.0.1:7700','masterKey')# An index is where the documents are stored.index=client.index('movies')documents= [ {'id':1,'title':'Carol','genres': ['Romance','Drama'] }, {'id':2,'title':'Wonder Woman','genres': ['Action','Adventure'] }, {'id':3,'title':'Life of Pi','genres': ['Adventure','Drama'] }, {'id':4,'title':'Mad Max: Fury Road','genres': ['Adventure','Science Fiction'] }, {'id':5,'title':'Moana','genres': ['Fantasy','Action']}, {'id':6,'title':'Philadelphia','genres': ['Drama'] },]# If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.index.add_documents(documents)# => { "uid": 0 }
With the taskuid
, you can check the status (enqueued
,canceled
,processing
,succeeded
orfailed
) of your documents addition using thetask.
# Meilisearch is typo-tolerant:index.search('caorl')
Output:
{"hits": [ {"id":1,"title":"Carol","genre": ["Romance","Drama"] } ],"offset":0,"limit":20,"processingTimeMs":1,"query":"caorl"}
All the supported options are described in thesearch parameters
index.search('phil', {'attributesToHighlight': ['*'], })
JSON output:
{"hits": [ {"id":6,"title":"Philadelphia","_formatted": {"id":6,"title":"<em>Phil</em>adelphia","genre": ["Drama"] } } ],"offset":0,"limit":20,"processingTimeMs":0,"query":"phil"}
Hybrid search combines traditional keyword search with semantic search for more relevant results. You need to have an embedder configured in your index settings to use this feature.
# Using hybrid search with the search methodindex.search('action movie', {"hybrid": {"semanticRatio":0.5,"embedder":"default"} })
ThesemanticRatio
parameter (between 0 and 1) controls the balance between keyword search and semantic search:
- 0: Only keyword search
- 1: Only semantic search
- Values in between: A mix of both approaches
Theembedder
parameter specifies which configured embedder to use for the semantic search component.
If you want to enable filtering, you must add your attributes to thefilterableAttributes
index setting.
index.update_filterable_attributes(['id','genres'])
If your documents contain fields that the Python JSON serializer does not know how to handle youcan use your own custom serializer.
fromdatetimeimportdatetimefromjsonimportJSONEncoderfromuuidimportuuid4classCustomEncoder(JSONEncoder):defdefault(self,o):ifisinstance(o, (UUID,datetime)):returnstr(o)# Let the base class default method raise the TypeErrorreturnsuper().default(o)documents= [ {"id":uuid4(),"title":"test 1","when":datetime.now()}, {"id":uuid4(),"title":"Test 2","when":datetime.now()},]index.add_documents(documents,serializer=CustomEncoder)
You only need to perform this operation once.
Note that Meilisearch will rebuild your index whenever you updatefilterableAttributes
. Depending on the size of your dataset, this might take time. You can track the process using thetask.
Then, you can perform the search:
index.search('wonder', {'filter': ['id > 1 AND genres = Action'] })
{"hits": [ {"id":2,"title":"Wonder Woman","genres": ["Action","Adventure"] } ],"offset":0,"limit":20,"estimatedTotalHits":1,"processingTimeMs":0,"query":"wonder"}
This package guarantees compatibility withversion v1.2 and above of Meilisearch, but some features may not be present. Please check theissues for more info.
The following sections in our main documentation website may interest you:
- Manipulate documents: see theAPI references or read more aboutdocuments.
- Search: see theAPI references.
- Manage the indexes: see theAPI references or read more aboutindexes.
- Configure the index settings: see theAPI references or follow our guide onsettings parameters.
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit ourcontributing guidelines for detailed instructions!
Meilisearch provides and maintains manySDKs and Integration tools like this one. We want to provide everyone with anamazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in theintegration-guides repository.
About
Python wrapper for the Meilisearch API
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.