Databases 11 min read

Master Elasticsearch with Python: From Installation to Advanced Queries

This tutorial walks you through installing Elasticsearch, creating indices, inserting and updating documents, performing searches via REST API, and integrating Elasticsearch into Python applications using both raw HTTP requests and the official Python client, illustrated with practical 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 unstructured JSON data, making it a NoSQL database that also provides powerful search capabilities.

Elasticsearch Use Cases

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

Collecting and analyzing log data to discover trends and statistics.

Setup and Run

Download the Elasticsearch binary, ensure Java 7+ is installed, unzip, and run the executable. Verify the installation by accessing http://localhost:9200 or using curl to see the welcome page.

Basic Example

Create an index (e.g., company) using a REST client like PostMan. The index stores documents similar to a database table. Access http://localhost:9200/company to view its settings, including mappings, shards, and replicas.

Insert a document by sending JSON via POST; Elasticsearch assigns an _id if none is provided. Retrieve the document with GET /company/employees/1 or view only the source with GET /company/employees/1/_source. Update a document by sending a modified JSON payload, and delete documents with the DELETE method.

Searching

Perform a basic search with GET /company/employees/_search?q=adnan, which returns matching records and a max_score indicating relevance. Restrict searches to specific fields, e.g., q=name:Adnan, analogous to an SQL SELECT statement.

Using Elasticsearch in Python

The REST API can be accessed via the requests library, but the official elasticsearch Python client simplifies interactions. Install it with pip install elasticsearch and verify the installation by running a short script.

Web Search and Elasticsearch

Example project: scrape recipes from Allrecipes, store them in an index named recipes with a type salads, and define strict mappings for fields such as title (text) and calories (integer).

Steps include:

Fetching JSON data from the web.

Connecting to Elasticsearch ( es.ping()).

Creating the index with mappings, using ignore=400 to avoid overwriting existing indices.

Indexing documents, handling errors when field types mismatch (e.g., inserting a string into an integer field).

Query the indexed data with functions that accept an index name and search criteria, demonstrating filters such as calories=102 or calories>20, and selecting specific fields in the response.

Conclusion

Elasticsearch is a powerful tool for building searchable applications, offering precise result sets and advanced features like fuzzy search. Continue exploring the documentation and consider learning Query DSL for more complex queries.

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.

search engineElasticsearchREST APINoSQLdata indexing
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.