Skip to main content

fg

Elastic Search access

To install: pip install fg

Overview

The fg package provides a comprehensive interface for interacting with Elasticsearch, allowing for operations such as searching, counting, and sampling documents, as well as importing data from MongoDB into Elasticsearch. It includes functionality to handle complex queries, manage document mappings, and perform data extraction and transformation efficiently.

Features

  • ElasticCom Class: Central to the package, this class facilitates connections to Elasticsearch and provides methods to perform various operations like search, count, and data extraction.
  • Data Sampling: Supports sampling strategies based on arithmetic sequences or random field values, useful for handling large datasets.
  • Integration with MongoDB: Offers methods to import data directly from MongoDB collections into Elasticsearch, which is handy for data migration or synchronization tasks.
  • Data Export: Includes utilities to export data from Elasticsearch into Python dictionaries or pandas DataFrames, supporting data analysis and manipulation.
  • Utility Functions: Additional helper functions to manage and convert data types, handle UTF-8 encoding issues, and more.

Usage Examples

Connecting to Elasticsearch

from fg import ElasticCom

# Initialize ElasticCom with the index name
elastic_instance = ElasticCom(index="your_index_name")

Searching Documents

# Perform a search operation
search_results = elastic_instance.search(query={"match_all": {}})

Sampling Documents Based on Arithmetic Sequence

# Sample documents using an arithmetic sequence
sampled_docs = elastic_instance.sample_with_arithmetic_seq(sample_size=10)

Importing Data from MongoDB

from pymongo import MongoClient

# Connect to MongoDB
mongo_client = MongoClient()
mongo_db = mongo_client["your_mongo_db"]
mongo_collection = mongo_db["your_collection"]

# Import data from MongoDB to Elasticsearch
import_result = elastic_instance.import_mongo_collection(mongo_db=mongo_db, mongo_collection=mongo_collection)

Exporting Search Results to DataFrame

# Export search results to a pandas DataFrame
df = elastic_instance.search_and_export_to_df(query={"match_all": {}})

Class and Method Documentation

Class: ElasticCom

  • __init__(self, index, doc_type=None, hosts='localhost:9200', **kwargs): Initializes a connection to an Elasticsearch cluster.
  • search(self, *args, **kwargs): Executes a search query.
  • count(self, *args, **kwargs): Counts the documents matching a query.
  • sample_with_arithmetic_seq(self, sample_size, initial_idx=None, **kwargs): Samples documents based on an arithmetic sequence.
  • sample_based_on_random_field_val(self, n_rand_picks=10, rand_field='timestamp', rand_batch_size=1, *args, **kwargs): Samples documents based on random values of a specified field.

Utility Functions

  • get_search_hits(es_response, _id=True, data_key=None): Extracts hits from an Elasticsearch response.
  • es_types_to_main_types(mapping): Converts Elasticsearch field types to more general types.
  • to_utf8_or_bust(obj): Ensures strings are in UTF-8 format, attempting conversion if necessary.

Notes

  • Ensure Elasticsearch and MongoDB instances are accessible from the environment where fg is used.
  • Handle exceptions and errors, especially when dealing with network operations and data conversions.

This package is designed to simplify and streamline the process of working with Elasticsearch, providing high-level abstractions for common tasks while still allowing detailed control when needed.

Metadata

Release files for fg 0.0.6

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

Source distribution (sdist)

Source distribution for fg 0.0.6
File Size Uploaded
fg-0.0.6.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fg 0.0.6
File Interpreter ABI Platform
fg-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 21.0 kB

Release files / fg-0.0.6.tar.gz

Download URL fg-0.0.6.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6fcabf7652ae418532c0fc22236c7de6be1e6c9655d8c066542f45d109218460
BLAKE2b-256 checksum
How to use checksums
6bc7baa8f211474b14490c9b3a17b68f4d519a31d0a5a02604305fa6ba730d8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / fg-0.0.6-py3-none-any.whl

Download URL fg-0.0.6-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cad2d8ace2f948a7571fd726f60f2d2c8da597945375b0e0784b6f139d71562
BLAKE2b-256 checksum
How to use checksums
796eb124605dcd09f87763f3408715fece1d611e96ac39901930cd1ca0ffd166
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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