This guide shows you how to install the Elasticsearch Python client and perform basic operations like indexing or searching documents.
To install the client, run the following command:
$ python -m pip install elasticsearch
You can connect to Elastic Cloud using an API key and the Cloud ID.
from elasticsearch import Elasticsearch
client = Elasticsearch(cloud_id="YOUR_CLOUD_ID", api_key="YOUR_API_KEY")
Your Cloud ID can be found on the My deployment page of your deployment under Cloud ID.
You can generate an API key on the Management page under Security.
Confirm that the connection was successful.
print(client.info())
Time to use Elasticsearch! This section walks you through the most important operations of Elasticsearch. The following examples assume that the Python client was instantiated as above.
This is how you create the my_index index. Optionally, you can first define the expected types of your features with a custom mapping.
mappings = {
"properties": {
"foo": {"type": "text"},
"bar": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256,
}
},
},
}
}
client.indices.create(index="my_index", mappings=mappings)
This indexes a document with the index API:
client.index(
index="my_index",
id="my_document_id",
document={
"foo": "foo",
"bar": "bar",
},
)
You can also index multiple documents at once with the bulk helper function:
from elasticsearch import helpers
def generate_docs():
for i in range(10):
yield {
"_index": "my_index",
"foo": f"foo {i}",
"bar": "bar",
}
helpers.bulk(client, generate_docs())
These helpers are the recommended way to perform bulk ingestion. While it is also possible to perform bulk ingestion using client.bulk
directly, the helpers handle retries, ingesting chunk by chunk and more. See the helpers
page for more details.
You can get documents by using the following code:
client.get(index="my_index", id="my_document_id")
This is how you can create a single match query with the Python client:
client.search(index="my_index", query={"match": {"foo": {"query": "foo"}}})
This is how you can update a document, for example to add a new field:
client.update(
index="my_index",
id="my_document_id",
doc={
"foo": "bar",
"new_field": "new value",
},
)
client.delete(index="my_index", id="my_document_id")
client.indices.delete(index="my_index")