Scripts

Python Script to Query Elasticsearch

Here is a basic Python script to query Elasticsearch using the elasticsearch library. Very useful for data mining.

Here is a basic Python script to query Elasticsearch using the elasticsearch library:

from elasticsearch import Elasticsearch

# Connect to Elasticsearch cluster
es = Elasticsearch(["http://localhost:9200/"])

# Define query body
query_body = {
    "query": {
        "match_all": {}
    }
}

# Execute search
result = es.search(index="your_index_name", body=query_body)

# Extract search hits
hits = result["hits"]["hits"]

# Print results
for hit in hits:
    print(hit["_source"])

In this script, the elasticsearch library is imported, and a connection to an Elasticsearch cluster is established. Then, a query body is defined as a dictionary, which includes a match_all query. Finally, the search method is called on the es instance to execute the query, and the search hits are extracted from the result and printed.

PyJoy

Recent Posts

Python Script to Monitor Network Traffic

Here is a simple Python script to monitor network traffic. import psutil def monitor_network(): #…

2 years ago

Python Script for Web Scraping

Here's a basic example of web scraping in Python using the requests and BeautifulSoup libraries:…

2 years ago

Python Script to SSH and Run Commands

Here's an example of how you can use Python to SSH into a remote server…

2 years ago

Python Code Example with Classes

Here's an example of a Python class that represents a rectangle: class Rectangle: def __init__(self,…

2 years ago

Python Script Example with Main Method in Active Directory

Here is a simple Python script that interacts with Active Directory (AD) using the pyad…

2 years ago

Python Script Example for System Administrators

Here's a Python script to monitor disk usage on a system and send an email…

2 years ago