Master Elasticsearch with Python: From Installation to Advanced Queries

This tutorial walks you through installing Elasticsearch, creating indices, mapping schemas, inserting, updating, deleting, and searching documents using both cURL commands and the official Python client, illustrated with practical code examples and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Elasticsearch with Python: From Installation to Advanced Queries

What is Elasticsearch?

Elasticsearch (ES) is a highly available, distributed open‑source search engine built on Apache Lucene. It stores JSON‑formatted unstructured data, acting as a NoSQL database with powerful search capabilities.

Elasticsearch Use Cases

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

Collecting and analyzing various log data to discover trends and statistics.

Setup and Run

Download the ES binary, ensure Java 7+ is installed, unzip, and run the executable. After startup you should see console output confirming the service is running.

Access http://localhost:9200 in a browser or via cURL to view the welcome page, confirming a successful installation.

Basic Example

Create an index (e.g., company) using a REST client like PostMan. The index stores documents similar to a database table. You can view the index via http://localhost:9200/company, which shows settings such as creation_date, number_of_shards, and replica configuration.

Insert a document by sending JSON data; ES assigns an _id if none is provided. Retrieve the document at http://localhost:9200/company/employees/1 to see both metadata and the stored record.

Update a document by sending a modified JSON payload; the response will show _result as updated. Delete a document with a DELETE request, or wipe all data with:

curl -XDELETE localhost:9200/_all

Using Elasticsearch in Python

While you can use the REST API with the requests library, the official elasticsearch Python client simplifies interactions. Install it via pip install elasticsearch and verify the installation with a simple script.

Web Search and Elasticsearch

This section demonstrates a real‑world workflow: scraping recipe data from Allrecipes, storing it in an ES index named recipes with a type salads, and defining strict mappings (e.g., calories as integer, nested ingredients as nested type).

After creating the index and applying the mapping, documents are indexed. Errors such as inserting a string into an integer field are shown, highlighting the importance of proper mappings.

Search queries are performed using the search() function, illustrating basic term queries, range filters, and field‑specific returns, analogous to SQL SELECT statements.

Conclusion

Elasticsearch is a powerful tool for building searchable applications, offering features like fuzzy search and robust relevance scoring. Continue exploring its documentation to master advanced capabilities such as Query DSL.

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.

Pythonsearch engineBackend DevelopmentElasticsearchREST API
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.