Elasticsearch: Build your first search query with Python

Overview

Introduction to Elasticsearch

Elasticsearch provides a range of search techniques, starting with BM25, the industry standard for textual search. It also offers semantic search powered by AI models, improving results based on context and intent.

Elasticsearch provides official clients for multiple programming languages, including Python, Rust, Java, JavaScript, and others. These clients offer full API support for indexing, searching, and cluster management. They are optimized for performance and kept up to date with Elasticsearch releases, ensuring compatibility and security.


Let's get started

In this example, you will index a couple documents and query them using Python. By the end of this guide, you’ll have learned how to connect a backend application to elasticsearch to answer your queries.

Create an Elastic Cloud project

Get started with a 14-day trial. Once you go to and create an account, follow the steps below to learn how to launch your first Elasticsearch Serverless project, select Elasticsearch.

screenshot-which-type-of-project-would-you-like-to-create.png

Then create General purpose, give it a name like My Project and create it.

elasticsearch general purpose.png

Let's create our first Elasticsearch index, we can name it my-index. Select Create my index.

screenshot-get-started-with-elasticsearch.png

You've created your first index! Next, create an API key so your application can talk to Elasticsearch. Select your preferred language. For this example, leverage Python.

screenshot-my-index.png

Get started data into Elasticsearch

In your terminal, install the Elasticsearch client using pip:

pip install elasticsearch

Copy your API key from the top right corner and add it to the client’s configuration alongside the project url. We can already create the mappings for our index, which will have just a text field creatively named “text”.

from elasticsearch import Elasticsearch

client = Elasticsearch(
"https://my-project-bff300.es.us-east-1.aws.elastic.cloud:443",
api_key="YOUR-API-KEY"
)

index_name = "my-index"

mappings = {
"properties": {
"text": {
"type": "text"
}
}
}

mapping_response = client.indices.put_mapping(index=index_name, body=mappings)
print(mapping_response)

Then we can finally use bulk requests to send data to Elasticsearch, let’s index 3 documents using a _bulk request. Remember to use bulk requests when indexing hundreds to billions of documents too, as this is the preferred choice when indexing large quantities of documents in Elasticsearch.

docs = [
{
"text": "Yellowstone National Park is one of the largest national parks in the United States. It ranges from the Wyoming to Montana and Idaho, and contains an area of 2,219,791 acress across three different states. Its most famous for hosting the geyser Old Faithful and is centered on the Yellowstone Caldera, the largest super volcano on the American continent. Yellowstone is host to hundreds of species of animal, many of which are endangered or threatened. Most notably, it contains free-ranging herds of bison and elk, alongside bears, cougars and wolves. The national park receives over 4.5 million visitors annually and is a UNESCO World Heritage Site."
},
{
"text": "Yosemite National Park is a United States National Park, covering over 750,000 acres of land in California. A UNESCO World Heritage Site, the park is best known for its granite cliffs, waterfalls and giant sequoia trees. Yosemite hosts over four million visitors in most years, with a peak of five million visitors in 2016. The park is home to a diverse range of wildlife, including mule deer, black bears, and the endangered Sierra Nevada bighorn sheep. The park has 1,200 square miles of wilderness, and is a popular destination for rock climbers, with over 3,000 feet of vertical granite to climb. Its most famous and cliff is the El Capitan, a 3,000 feet monolith along its tallest face."
},
{
"text": "Rocky Mountain National Park is one of the most popular national parks in the United States. It receives over 4.5 million visitors annually, and is known for its mountainous terrain, including Longs Peak, which is the highest peak in the park. The park is home to a variety of wildlife, including elk, mule deer, moose, and bighorn sheep. The park is also home to a variety of ecosystems, including montane, subalpine, and alpine tundra. The park is a popular destination for hiking, camping, and wildlife viewing, and is a UNESCO World Heritage Site."
}
]

bulk_response = helpers.bulk(client, docs, index=index_name)
print(bulk_response)

You should be able to see the documents in Elasticsearch.

view docs in elasticsearch.png


Working with Elasticsearch

Build your query

Create a new script (for instance search.py), which defines a query and executes the following search request:

FROM my-index
| WHERE MATCH(text, "yosemite")
| LIMIT 5

Add this query inside client.esql.query:

from elasticsearch import Elasticsearch

client = Elasticsearch(
"https://my-project-bff307.es.us-east-1.aws.elastic.cloud:443",
api_key="YOUR-API-KEY"
)

# Execute the search query
response = client.esql.query(
query="""
FROM my-index
| WHERE MATCH(text, "yosemite")
| LIMIT 5
""",
format="csv"
)

print(response)

Check your result:

"Yosemite National Park is a United States National Park, covering over 750,000 acres of land in California. A UNESCO World Heritage Site, the park is best known for its granite cliffs, waterfalls and giant sequoia trees. Yosemite hosts over four million visitors in most years, with a peak of five million visitors in 2016. The park is home to a diverse range of wildlife, including mule deer, black bears, and the endangered Sierra Nevada bighorn sheep. The park has 1,200 square miles of wilderness, and is a popular destination for rock climbers, with over 3,000 feet of vertical granite to climb. Its most famous and cliff is the El Capitan, a 3,000 feet monolith along its tallest face."

Now you are ready to use the client to query Elasticsearch from any Python backend like Flask, Django, etc. Check out the Elasticsearch Python Client documentation to explore further.


Next steps

Thanks for taking the time to set up semantic search for your data with Elastic Cloud. As you begin your journey with Elastic, understand some operational, security, and data components you should manage as a user when you deploy across your environment.

Ready to get started? Spin up a free 14-day trial on Elastic Cloud or try out these 15 minute hands-on learning on Search AI 101.

Start free trial