Elasticsearch Elasticsearch Python Example

By Opster Team

Updated: May 23, 2023

| 2 min read

Elasticsearch Python Example: Indexing and Searching Documents

In this article, we will walk through an example of using the Elasticsearch Python client, Elasticsearch-py, to index and search documents. Elasticsearch-py is the official low-level client for Elasticsearch and provides a simple and efficient way to interact with your Elasticsearch cluster.

Prerequisites:

  • Elasticsearch cluster up and running
  • Python 3.x installed
  • Elasticsearch-py installed (you can install it using `pip install elasticsearch`)

Step 1: Connect to Elasticsearch Cluster

First, we need to establish a connection to our Elasticsearch cluster. To do this, we will import the Elasticsearch module and create an instance of the Elasticsearch class.

python
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")

Step 2: Index a Document

Now that we have connected to our Elasticsearch cluster, let’s index a document. In this example, we will index a simple document with two fields: “title” and “content”.

python
doc = {
"title": "Elasticsearch Python Example",
"content": "This is an example of using Elasticsearch with Python."
}

index_name = "example_index"
doc_type = "_doc"
doc_id = 1

response = es.index(index=index_name, doc_type=doc_type, id=doc_id, body=doc)
print(response)

This will index the document in the “example_index” index with a document type of “_doc” and an ID of 1.

Step 3: Search for Documents

Now that we have indexed a document, let’s search for it using a simple query. We will search for documents containing the word “example” in the “title” field.

python
query = {
"query": {
"match": {
"title": "example"
}
}
}

response = es.search(index=index_name, body=query)
print(response)

This will return a response containing the search results. The response will include the total number of matching documents and the documents themselves.

Step 4: Parse and Display Search Results

To make the search results more readable, let’s parse the response and display the document IDs and their corresponding “title” field.

python
hits = response["hits"]["hits"]

for hit in hits:
print(f"Document ID: {hit['_id']}, Title: {hit['_source']['title']}")

This will output the document ID and the title of each matching document.

Conclusion

In this article, we demonstrated how to use the Elasticsearch-py Python client to index and search documents in an Elasticsearch cluster. This example can be easily extended to handle more complex use cases, such as indexing and searching multiple document types, using more advanced queries, and performing aggregations.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?