Skip to main content

Thin Python connector for Elasticsearch.

Project description

Thin Python client for Elasticsearch

This is a little wrapper over the requests module that helps you do your thing with Elasticsearch. But not too much. For each request, you need to specify the following, where they are applicable:

  • desired request method
  • index, type, ID
  • “suffix” (which might be the _search, _bulk, etc at the end of the URL)
  • a dictionary or a string with the data you want to submit

Why would I want that?

Two reasons:

  • flexibility - the point of this client is to keep up with the many features constantly added to Elasticsearch. You can customize your index operations and queries as much as you need
  • documentation - once you get the hang of the few options particular to this client, you only have to look at the documentation on Elasticsearch itself to access its features

If this is not what you want, I would suggest to look at pyes or pyelasticsearch

Features and usage

There are a few files in there at the moment:

  • slimes.py - the module itself
  • tests_slimes.py - unit tests

Here are the main features:

Converts your dict to JSON (you can disable that for stuff like bulks)

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200"])
#define the document
testdoc = {"foo": 2345, "bar": True}
#post the document
my_requester.request(method="post",
    myindex="testindex",
    mytype="testtype",
    mydata=testdoc)

Can get a list of nodes, for redundancy

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200","my_other_server:9200"])

Loads the JSON results for you

If something goes wrong (eg: non 2XX status code), you’ll get an exception. Otherwise, you’ll get a dictionary with the results from ES:

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200"])
myquery = {\
    "query": {\
            "term": {\
                    "age": 28\
            }\
    }\
}
results = my_requester.request(method="post",
    myindex="testindex",
    mytype="testtype",
    mysuffix="_search",
    mydata=myquery)
print "Number of hits is %d" % result['hits']['total']

Can receive parameters at will

So you can index with the “create” parameter, like this:

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200"])
testdoc={"foo": "bar"}
my_requester.request(method="put",
    myindex="testindex",
    mytype="testtype",
    myID=1,
    mysuffix="?op_type=create",
    mydata=testdoc)

Or, you can put the parameter(s) as a dictionary:

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200"])
testdoc={"foo": "bar"}
my_requester.request(method="put",
    myindex="testindex",
    mytype="testtype",
    myID=1,
    myparams={"op_type":"create"},
    mydata=testdoc)

And you can also specify a timeout:

#!/usr/bin/env python
import slimes
my_requester = slimes.Requester(["localhost:9200"])
#give it 10 seconds to complete, otherwise give Exception
my_requester.request(mytimeout=10)

TODOes

Enhancements will be about the transport. Stuff like: - use thrift - get all cluster nodes

Stay tuned :)

Bugs

Please feel free to report them on github

Project details


Release history Release notifications

This version
History Node

0.0.2

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
SlimES-0.0.2.tar.gz (4.2 kB) Copy SHA256 hash SHA256 Source None Sep 10, 2012

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page