els-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: Truein the DSL query) - Explain
Installation
uv add els-utils
Example usage
Instantiate
from els_utils import ELS
# Using API key to authenticate
els = ELS(ES_ENDPOINT, api_key=ES_API_KEY)
# Using basic authen with username and password
els = ELS(ES_ENDPOINT, basic_authen=(USERNAME, PASSWORD))
Create index
mapping = {"mappings": {"properties": {...}}}
els.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"}]
els.bulk_update(index_name="my-index", data=data, id_key="some-id")
# Routing option is also available if needed
els.bulk_update(index_name="my-index", data=data, id_key="some-id", routing_key="1")
Results
dsl = {"query": {...}}
results = els.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 = els.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 scores breakdown
explain.get_scores_breakdown(as_df=False) # If you wish to return a DataFrame, can pass `as_df=True`
# Get the scores terms, e.g., tf, idf, boost for each field
explain.get_scores_term(as_df=False)
# Get the contributions summary for each field
explain.get_scores_summary() # You can also pass in `as_df` parameter
Using the explanation from the search API
dsl = {"explain": True, "query": {...}}
results = els.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_scores_term() # Given that the key "75720" exists in the results
Metadata
Release files for els-utils 0.3.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| els_utils-0.3.9.tar.gz | 58.1 kB | Without provenance | Details for els_utils-0.3.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| els_utils-0.3.9-py3-none-any.whl | Python 3 | none | any | Details for els_utils-0.3.9-py3-none-any.whl |
Total release size: 69.9 kB
Release files / els_utils-0.3.9.tar.gz
| Download URL | els_utils-0.3.9.tar.gz |
|---|---|
| Size | 58.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
72aac7ad2f2ff2277e3ba5dc8497ef53c51e663246c344b033d97a4071623435
|
|
BLAKE2b-256 checksum How to use checksums |
c03adc2c4062571e4affaacb469a3aba4e3c8682d3d6503bbf68e2ab3d8e7c00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.11
|
Release files / els_utils-0.3.9-py3-none-any.whl
| Download URL | els_utils-0.3.9-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a70fca1f30cebd062fd59c17470aee243e058ca3f8d21a7e4bca17ffde477a4
|
|
BLAKE2b-256 checksum How to use checksums |
4c87e6a37ec182ef6e69ef58dfc05522f03ef80b70df3f9480b7dcd72e308f13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.11
|
Security for els-utils 0.3.9
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.