Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python wrapper for the Meilisearch API

License

NotificationsYou must be signed in to change notification settings

meilisearch/meilisearch-python

Repository files navigation

Meilisearch-Python

Meilisearch Python

PyPI versionTest StatusLicenseBors enabled

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

Table of Contents

📖 Documentation

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.

🔧 Installation

Note: Python 3.8+ is required.

Withpip3 in command line:

pip3 install meilisearch

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

🚀 Getting started

Add Documents

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.

Basic Search

# Meilisearch is typo-tolerant:index.search('caorl')

Output:

{"hits": [    {"id":1,"title":"Carol","genre": ["Romance","Drama"]    }  ],"offset":0,"limit":20,"processingTimeMs":1,"query":"caorl"}

Custom Search

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

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.

Custom Search With Filters

If you want to enable filtering, you must add your attributes to thefilterableAttributes index setting.

index.update_filterable_attributes(['id','genres'])

Custom Serializer for documents

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"}

🤖 Compatibility with Meilisearch

This package guarantees compatibility withversion v1.2 and above of Meilisearch, but some features may not be present. Please check theissues for more info.

💡 Learn more

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

⚙️ Contributing

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.


[8]ページ先頭

©2009-2025 Movatter.jp