A Python client library for querying Elasticsearch API.
poetry installfrom elasticsearch_client import ElasticsearchClient
# Initialize the client
client = ElasticsearchClient(
api_url="https://your-es-instance:9200",
api_key="your-api-key"
)
# Execute a query
results = client.query(
index="my-index",
query={"query": {"match_all": {}}}
)You can use the fields parameter to select only specific fields to return. This is useful for timeseries data where you only need timestamps and values.
from elasticsearch_client import ElasticsearchClient
client = ElasticsearchClient(
api_url="https://your-es-instance:9200",
api_key="your-api-key"
)
# Query with boolean conditions and field selection
query = {
"query": {
"bool": {
"must": [
{"match": {"record.appName": "something-platform"}},
{"match": {"record.CONTEXT_PATH": "/some/endpoint"}}
],
"must_not": [
{"term": {"record.CONTEXT_PATH.keyword": "/something"}}
]
}
}
}
# Only return timestamp and value fields
results = client.query(
index="my-index",
query=query,
fields=["timestamp", "value"]
)
# Results will contain only the selected fields
# Example: {"times": [1234567890, 1234567900], "values": [100, 200]}poetry run python -m unittest test_elasticsearch_client.py -v