Skip to content

chrishayen/example_elasticsearch_client

Repository files navigation

Elasticsearch API Client

A Python client library for querying Elasticsearch API.

Installation

poetry install

Usage

Basic Query

from 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": {}}}
)

Query with Field Selection

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]}

Testing

poetry run python -m unittest test_elasticsearch_client.py -v

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages