Databases 13 min read

Master Elasticsearch in Python: From Installation to Advanced Queries

This tutorial introduces Elasticsearch, explains its architecture and use cases, walks through installation, index creation, mapping, CRUD operations, and demonstrates how to integrate and query Elasticsearch from Python using both the REST API and the official client library.

21CTO
21CTO
21CTO
Master Elasticsearch in Python: From Installation to Advanced Queries

In this article I discuss Elasticsearch and how to integrate it into various Python applications.

What is Elasticsearch?

Elasticsearch (ES) is a highly available distributed open‑source search engine built on Apache Lucene. It runs on Java, stores unstructured data in JSON format, and also serves as a NoSQL database with built‑in search capabilities.

Elasticsearch Use Cases

Powerful search and native autocomplete for dynamic‑content sites such as e‑commerce platforms or blogs.

Log aggregation and analysis to discover trends and statistics.

Setup and Run

The simplest way to install ES is to download the binary, unzip it and run it with Java 7 or higher. After starting, you can verify the installation by opening http://localhost:9200 in a browser or using curl.

When the server starts you should see a welcome screen similar to the one shown below.

Accessing http://localhost:9200 in a browser displays a JSON welcome message confirming the installation.

Basic Example

The first step is to create an index; everything in ES is stored as an index, similar to a database in RDBMS terminology. You can use tools like PostMan to issue REST API calls.

If the request succeeds you will see a response like the following:

We created an index named company. Visiting http://localhost:9200/company shows the index details.

The index metadata includes creation_date, number_of_shards, and number_of_replicas, which control data partitioning and replication across cluster nodes.

Replication works like master‑slave mirroring, ensuring high availability.

Creating an index via curl is single‑threaded:

curl -XPUT "localhost:9200/company" -H 'Content-Type: application/json' -d '{"settings":{...}}'

To add a document you can POST JSON data. The following example creates a type employees under the company index.

After indexing you can retrieve the document via GET /company/employees/1:

Updating a document is as simple as sending a new JSON payload:

curl -XPOST "localhost:9200/company/employees/1" -H 'Content-Type: application/json' -d '{"name":"Adnan Updated"}'

The response now shows _result":"updated" instead of created.

Deleting a document or an entire index is also straightforward:

curl -XDELETE "localhost:9200/company/employees/1"

To wipe all data you can run curl -XDELETE localhost:9200/_all (use with caution).

Basic search can be performed with a simple query string:

GET /company/employees/_search?q=adnan

The max_score field indicates relevance. You can restrict the query to a specific field, e.g. name:Adnan, which is equivalent to SELECT * FROM table WHERE name='Adnan'.

Using Elasticsearch in Python

The REST API is sufficient, but the official elasticsearch Python client simplifies interactions.

Install it with:

pip install elasticsearch

Verify the installation by running a small snippet that pings the cluster:

Web Search and Elasticsearch

As a practical example, we fetch recipe data from Allrecipes, store it in ES, and query it.

We create an index called recipes with a type salads and define a strict mapping:

After creating the index we verify the mapping:

The mapping enforces field types (e.g., calories as integer) and uses a nested type for ingredients:

Attempting to index a document with an invalid calories type triggers an error:

After correcting the mapping, we index the recipes and query them. A simple search function search() wraps the query logic:

Example queries retrieve records with calories equal to 102 or greater than 20, demonstrating field filtering and relevance scoring.

Conclusion

Elasticsearch is a powerful tool that can add robust search capabilities to existing or new applications. It supports full‑text, fuzzy, and structured queries, and its RESTful API makes it easy to integrate from Python or any language. Further exploration of Query DSL is recommended for advanced use cases.

English original: http://blog.adnansiddiqi.me/getting-started-with-elasticsearch-in-python/ Translator: β
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonElasticsearchREST APINoSQLdata indexing
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.