Skip to main content

ElasticSearchPy

About

ElasticSeachPy is a python library used to connect to and interact with elasticsearch

This library is a python implementation of the Elasticsearch HTTP API and an alternative to ElasticSearch's Python Library

Installation

ElasticSearchPy can be install with the pip distribution system for Python by issuing:

$ pip3 install elasticsearch-python-client

Alternatively, you can run use setup.py to install by cloning this repository and issuing:

$ python3 setup.py install

Limitations

This library is currently under development and is not as feature rich as it will be. This being said, it currently has several limitations

  • Python3+ Only - My primary development environment is python3, I currently do not have the time to test and vet interoperability between python2 and python3
  • ElasticSearch Authentication - Currently, this library only supports "No Authentication" and "SSL w/ Certificate Authentication". This limitation is simply due to my testing environments
  • Limited Functionality - If functionality isn't listed here, it is either not implemented or is not tested. As time moves forward, I will continue to add fuctionality

Contributors

If you are interested in contributing to this project, feel free to contact me at jeff@koebane.net

Examples

Connecting to ElasticSearch

from elasticsearchpy import ElasticSearchConnection
es_ip = "192.168.1.1"
# Connect to the cluster
es_conn = ElasticSearchConnection(es_ip)
# Connect to the cluster with a non 9200 port
# es_conn = ElasticSearchConnection(es_ip, port=80)
# Alternatively Connect to the cluster using SSL certificates
# es_conn = ElasticSearchConnection(es_ip, port=443,
#    use_ssl=True,
#    cert="/path/to/cert.crt",
#    key="/path/to/key.key"
#)

Interact with the Cluster

from elasticsearchpy import ElasticSearchConnection
es_ip = "192.168.1.1"
# Connect to the cluster
es_conn = ElasticSearchConnection(es_ip)

#Get Cluster Object
es_cluster = es_conn.get_cluster()
# get number of active shards
print("There are {} active shards".format(es_cluster.active_shards))
# Get the number of hosts
total_nodes = es_cluster.nodes
data_nodes = es_cluster.data_nodes

print("This cluster has {} nodes ( {} data nodes )".format(total_nodes,data_nodes))

# Get Node Names
nodes = es_cluster.node_names
print("This cluster has the following nodes:")
for node_name in nodes:
  print("- {}".format(node_name))

Interact with a cluster node

from elasticsearchpy import ElasticSearchConnection

es_ip = "192.168.1.1"

# Connect to the cluster
es_conn = ElasticSearchConnection(es_ip)

#Get Cluster Object
es_cluster = es_conn.get_cluster()

# Get Node Names
nodes = es_cluster.node_names

# Get Node Object
node = es_cluster.get_node(nodes[0)

print("Node {}".format(node.name)
if node.master:
    print("* Is the master Node")
if node.ingest_node:
    print("* Is an ingest Node")
if node.data_node:
    print("* Is a data node")

print("Elasticsearch Version: {}".format(node.version))
print("OS: {}".format(node.os))
print("Has the following roles")
print("------------------------")
for role in node.roles:
  print(" - {}".format(role))

Interact with indices

from elasticsearchpy import ElasticSearchConnection

es_ip = "192.168.1.1"

# Connect to the cluster
es_conn = ElasticSearchConnection(es_ip)

# Get Indices Object
non_sys_indices = es_conn.get_indices() # this gets all non-system indices
all_indices = es_conn.get_indices(system_indices=True) # this gets all indices including system indices
bob_indices = es_conn.get_indices(indice_prefix="bob") # this gets all indices that start with bob

# List indices
print("Here are the non-system indices")
for indice in non_sys_indices.indices:
  print("- {}".format(indice)
print("Total: {}".format(non_sys_indices.count)

# Create an indice
my_indice = indices.create_indice("bob-1") # Creates bob-1 with default shard replica values
my_indice2 = indices.create_indice("bob-2", shards=2, replicas=0) # Creates bob-2 with 2 shards and 0 replicas

# Delete an indice
indices.delete_indice("bob-2") # Deletes the Indice named bob-2

Interact with a single indice

from elasticsearchpy import ElasticSearchConnection
from elasticsearchpy import ElasticSearchIndice

es_ip = "192.168.1.1"

# Connect to the cluster
es_conn = ElasticSearchConnection(es_ip)

# Get Indices Object
non_sys_indices = es_conn.get_indices() # this gets all non-system indices

# Get the Indice Object
my_indice = non_sys_indice.get_indice("bob-1") # Retrieves an indice object from the indice bob-1

# Alternative way of getting the Indice Object
my_indice_2 = ElasticSearchIndice("bob-2",es_conn)

print("Indice {} has the following properties".format(my_indice.name))
print("- Status: {}".format(my_indice.status))
print("- Health: {}".format(my_indice.health))
print("- Number of documents: {}".format(my_indice.docs))
print("- Size: {}".format(my_indice.size))

doc_data = {
  "message":"I am a test message",
  "user":"test user",
  "purpose":"TESTING...DUUU"
}
# Creating Documents
doc = my_indice.create_doc(doc_data) # this creates a do and lets elasticsearch generate the id
doc = my_indice.create_doc(doc_data,name="document-1234") # this creates a document that you generate the id for

# Deleting Documents
my_indice.delete_doc("document-1234")

Immediate future TODO List

  • Add document search to indice object
  • Add templates
  • Document documents object

Metadata

Release files for elasticsearch-python-client 0.0.1

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

Source distribution (sdist)

Source distribution for elasticsearch-python-client 0.0.1
File Size Uploaded
elasticsearch-python-client-0.0.1.tar.gz 16.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elasticsearch-python-client 0.0.1
File Interpreter ABI Platform
elasticsearch_python_client-0.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 46.7 kB

Release files / elasticsearch-python-client-0.0.1.tar.gz

Download URL elasticsearch-python-client-0.0.1.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d8ee3bed7e305f5f4bde8dc5d306f8b10a3e8a7762ff70d4f660e8da524c98f0
BLAKE2b-256 checksum
How to use checksums
3ab9046ba7578b2a5600d417b024d1b5405fe28ab1309ca5efa17d099e6496dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

Release files / elasticsearch_python_client-0.0.1-py2.py3-none-any.whl

Download URL elasticsearch_python_client-0.0.1-py2.py3-none-any.whl
Size 30.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e11e265fa45c652c97645c715ccae3d87f1520f1869cb34817144df214adbd38
BLAKE2b-256 checksum
How to use checksums
084a12eecf27c5cd51dfcc4b5de19310219d504bc7d38e1e5b499c5401892753
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

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