Writing · 2024
How To Use OpenSearch in Python
A guide to indexing and searching data with OpenSearch from your Python app.
Need to add search to your Python app? This guide covers everything to get started with OpenSearch, from setting up your first index to writing fuzzy and multi-field queries.
Getting Started
First, install the opensearch-py library:
pip install opensearch-pyConnecting to OpenSearch
Establish a connection to your OpenSearch instance:
from opensearchpy import OpenSearch
client = OpenSearch(
hosts = [{'host': 'localhost', 'port': 9200}],
http_compress = True,
use_ssl = False,
verify_certs = False,
)
Creating an Index
Initialize a new index with settings for shards and replicas.
index_name = 'my-index'
index_body = {
'settings': {
'index': {
'number_of_shards': 4
}
}
}
response = client.indices.create(index_name, body=index_body)
print(response)