Elastic Search access
Project description
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
fgis 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.
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
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 fg-0.0.5.tar.gz.
File metadata
- Download URL: fg-0.0.5.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c13240a58be5aea4a36273a48be443c5d597a6be47629fccccf25eaea17124b
|
|
| MD5 |
d85ad4cf6454933ec013c5b9543c519b
|
|
| BLAKE2b-256 |
96556d312eb53406728405208bf392c7561a651e1508c06f1b9ec1f63907fcec
|
File details
Details for the file fg-0.0.5-py3-none-any.whl.
File metadata
- Download URL: fg-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
def014efd3dd12e60b63e88e5c08920052d4238f291b24c7a72ca7aa59f5d6d3
|
|
| MD5 |
dd555d051c5f9bda40a2978024802c42
|
|
| BLAKE2b-256 |
fe865c670c4dcd2c96273f4951e291e8cf67834a755bf7f5b7a62ce96bf09032
|