Skip to main content

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: 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 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 = 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_scores_term()  # 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.4.1.tar.gz (58.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

elasticsearch_utils-0.4.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file elasticsearch_utils-0.4.1.tar.gz.

File metadata

  • Download URL: elasticsearch_utils-0.4.1.tar.gz
  • Upload date:
  • Size: 58.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for elasticsearch_utils-0.4.1.tar.gz
Algorithm Hash digest
SHA256 bf285c8401a7b2d99f3529582c8e80472a92e59f9ccc19a6de4c8f2196acee59
MD5 e3f5d018869ebd746a3efa934cf27473
BLAKE2b-256 611544edf221188abff98a470ce1a2a06a73ff2f6ac984e518a14bb27de992cb

See more details on using hashes here.

File details

Details for the file elasticsearch_utils-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for elasticsearch_utils-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0ee340b16520fc15cd195c6345218820dc5d482c7a2a9459d374107ab71af5
MD5 6d5f4850c13e836378d9f6abe8bc7070
BLAKE2b-256 30db556381359e922177ecd9b152ef4e4f48e6656e428e72a213efc913680051

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page