Posted on Leave a comment

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.

Leave a Reply

Your email address will not be published. Required fields are marked *