Python ElasticSearch

ElasticSearch connections created in Dashboard allow one to index, look up or delete stored documents and access other features ElasticSearch itself offers.

Indexing

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.search.es.get('My Connection').conn

        # Create a document
        conn.create('my-idx', 'my-type', {'hello':'world'}, 'my-id')

In server.log:

INFO - PUT http://es:9200/my-idx/my-type/my-id?op_type=create [status:201 request:0.057s]

Searching

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.search.es.get('My Connection').conn

        # Look up documents using ES syntax
        result = conn.search(q='hello:*')

        # Iterate over what ES gave us
        for item in result:
            self.logger.info(result)

In server.log:

  INFO - GET http://es:9200/_search?q=hello%3A%2A [status:200 request:0.058s]
  INFO - {'hits': {'hits': [{'_score': 1.0, '_type': 'my-type', '_id': 'my-id' ..

Deleting

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.search.es.get('My Connection').conn

        # Delete a document by its ID
        conn.delete('my-idx', 'my-type', 'my-id')

In server.log:

INFO - DELETE http://es:9200/my-idx/my-type/my-id [status:200 request:0.003s]

Other features

The connection object is an instance of elasticsearch.client.Elasticsearch. Refer to its main documentation in order to learn how to access other features such as bulk deleting, counting and more.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):
    def handle(self):
        conn = self.search.es.get('My Connection').conn

        self.logger.info(conn.__class__)
INFO - <class 'elasticsearch.client.Elasticsearch'>

Also of interest


Schedule a meaningful demo

Book a demo with an expert who will help you build meaningful systems that match your ambitions

"For me, Zato Source is the only technology partner to help with operational improvements."

— John Adams
Program Manager of Channel Enablement at Keysight