This script connects to an Elasticsearch server, runs a query to fetch all documents from a specified index, and prints the results. The script includes logging and exception handling for robust error reporting.
This script is configured to disable SSL certificate verification. This is suitable for development environments but should be handled with caution in production. This code also considers a local installation of Elasticsearch running as localhost, however it can be modified to connect to any remote server with basic authentication.
Replace your server details and credentials in the elastic-query.py as following:
https://[ELASTIC_USER]:[ELASTIC_PASSWORD]@localhost:9200Make sure your Elasticsearch server is running and accessible at the specified URL.
- Python 3.x
elasticsearchlibraryurllib3library
-
Clone the repository (if applicable):
git clone <repository_url> cd <repository_directory>
-
Create a virtual environment (optional but recommended):
python -m venv myenv source myenv/bin/activate # On Windows: myenv\Scripts\activate
-
Install dependencies:
pip install elasticsearch urllib3
Before running the script, replace [ELASTIC_USER] and [ELASTIC_PASSWORD] in the Elasticsearch connection string with your actual Elasticsearch credentials.
Run the script with Python (Windows):
python .\elastic-query.py