Skip to main content

elasticsearch-utils

This library provides utilities for interacting with Elasticsearch and results retrived from it.

The core features are:

  • Index creation (even if already exists)
  • Index deletion
  • Search (can include explain: True in the DSL query)
  • Explain

Installation

uv add elasticsearch-utils

Example usage

Instantiate

from elasticsearch_utils import ESClient


# Using API key to authenticate
es = ESClient(ES_ENDPOINT, api_key=ES_API_KEY)

# Using basic authen with username and password
es = ELS(ES_ENDPOINT, basic_authen=(USERNAME, PASSWORD))

Create index

mapping = {"mappings": {"properties": {...}}}

es.create_index(index_name="my-index", json_mapping=mapping, replace_if_exists=True)

Bulk update

data = [{"some-id": "1", "field1": "some-value"}, {"some-id": "2", "field1": "another-value"}]

es.bulk_update(index_name="my-index", data=data, id_key="some-id")

# Routing option is also available if needed
es.bulk_update(index_name="my-index", data=data, id_key="some-id", routing_key="1")

Results

dsl = {"query": {...}}
results = es.search(INDEX_NAME, dsl)

# results: <SearchResults total_hits=510>

# Get the JSON
results.json

# Get the hits
result.hits

# Get the sources
results.get_sources(as_list=False)  # as_list can be `True` if you wish to get just a list of sources

# Get results in a DataFrame format
results.to_dataframe()

# You can also pass in just the columns you want
results.to_dataframe(columns=["field1"])

# If explain: True was passed in the DSL, you can also get explanations
results.get_explanations()  # This returns a dict of {`_id`: `<ExplainResult>`}

Explanation

Using the explain API

dsl = {"query": {...}}
explain = es.explain(INDEX_NAME, doc_id="75720", dsl=dsl, routing=None)

# Get the JSON
explain.json

# Get score
explain.score

# Get the explanation dict
explain.explanation

# Get the scores terms, e.g., field, term, score, boost, etc.
explain.get_field_details()

# Get the contributions summary for each field
explain.get_field_summary()

Using the explanation from the search API

dsl = {"explain": True, "query": {...}}
results = es.search(INDEX_NAME, dsl=dsl)

# This returns a dict of {`_id`: `<ExplainResult>`}
explanations = results.get_explanations()

# You can do the same thing as the `ExplainResult` for each item
explanations["75720"].get_field_summary()  # Given that the key "75720" exists in the results

Index Mappings and settings

mappings_results = es.get_mappings(INDEX_NAME)

mapping_results.fields_mappings
settings_results = es.get_settings(INDEX_NAME)

settings_results.index_settings

Metadata

Release files for elasticsearch-utils 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elasticsearch-utils 0.6.0
File Size Uploaded Provenance Actions
elasticsearch_utils-0.6.0.tar.gz 60.4 kB Without provenance Details for elasticsearch_utils-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for elasticsearch-utils 0.6.0
File Interpreter ABI Platform Actions
elasticsearch_utils-0.6.0-py3-none-any.whl Python 3 none any Details for elasticsearch_utils-0.6.0-py3-none-any.whl

Total release size: 74.4 kB

Release files / elasticsearch_utils-0.6.0.tar.gz

Download URL elasticsearch_utils-0.6.0.tar.gz
Size 60.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d3847256a28311fd3c02586140ed7e5b6c3a8d1650af79146bd872eb06f8ab53
BLAKE2b-256 checksum
How to use checksums
aec06214f798456c48d996fcd43e6bb94378f0013930ece0720739235cafb227
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.11

Release files / elasticsearch_utils-0.6.0-py3-none-any.whl

Download URL elasticsearch_utils-0.6.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26f9a21645dca8ed8cc8581313c092fa8c732767cfbeb9c6210c0b7466f9b5a9
BLAKE2b-256 checksum
How to use checksums
9d0d54edd5941ed4338229c3db6bcf7e78b246f1031c88af61607075ce3e23e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.11

Security for elasticsearch-utils 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page