A simple wrapper for Elasticsearch tool
Project description
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: Truein 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
elasticsearch_utils-0.5.1.tar.gz
(59.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file elasticsearch_utils-0.5.1.tar.gz.
File metadata
- Download URL: elasticsearch_utils-0.5.1.tar.gz
- Upload date:
- Size: 59.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79a478804ac79ccd20f078b1594d1b1fcde5f7e5a3a1a7464f549770c1d73d87
|
|
| MD5 |
b5b8503dc4c42111b7fc05bf4311ee77
|
|
| BLAKE2b-256 |
069e1b9f6f56609eb18f350e45d05a43a9d0de5720a5bae953f4d2df3e4e11cd
|
File details
Details for the file elasticsearch_utils-0.5.1-py3-none-any.whl.
File metadata
- Download URL: elasticsearch_utils-0.5.1-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9550fcb1b5cee083e53ed5cce9d8b39fc8dfa003b4d0eb1056b83bed93e977de
|
|
| MD5 |
d8a95d69b1cd63ded9158588b40e9cd5
|
|
| BLAKE2b-256 |
06d5ccd9dde835908bff27279886dee8f7dfa72736340994d8364281203e5141
|