Skip to content

Commit

Permalink
Luke: json and readme patches
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecampbell committed Jun 7, 2012
1 parent 38af46a commit fd833b4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
37 changes: 21 additions & 16 deletions README.md
@@ -1,15 +1,16 @@
elasticpy
===========
"ElasticSearch for the rest of us"
_"ElasticSearch for the rest of us"_

Python wrapper for the elasticsearch indexing utility.

Author: Luke Campbell <luke.s.campbell@gmail.com>
Author: [Luke Campbell](http://lukecampbell.github.com/) [<luke.s.campbell@gmail.com>](mailto:luke.s.campbell@gmail.com)

About
-----
The goal of this module is to provide an intuitive interface between Python APIs and the ElasticSearch API. Connections are provided through the `requests` library so the connections are inherently thread safe and pooled.

Writing complex queries in JSON can be tedius and time consuming if you need to constantly reference the Query DSL guide so we've wrapped most of the operations in classes with methods corresponding to the parameters of the operation. For example:
Writing complex queries in JSON can be tedius and time consuming if you need to constantly reference the Query DSL guide, so we've wrapped most of the operations in classes with methods corresponding to the parameters of the operation. For example:

sorts = ElasticSort()
sorts.sort('name',order='asc')
Expand All @@ -23,6 +24,7 @@ HOWTO
-----

To begin using elasticpy start by importing.

import elasticpy as ep

To interface with the elasticsearch server use the ElasticSearch object.
Expand All @@ -31,9 +33,12 @@ To interface with the elasticsearch server use the ElasticSearch object.

To form a query use the ElasticQuery wrapper objects.

query = elasticpy.ElasticQuery().term('users':'luke')
query = ep.ElasticQuery().term('users':'luke')

# and then pass it to the search object

search.search_advanced('twitter','feeds',query)

> {u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
u'hits': {u'hits': [{u'_id': u'1',
u'_index': u'twitter',
Expand Down Expand Up @@ -114,18 +119,18 @@ USAGE
Copying
-----------

elasticpy - Python Wrapper for ElasticSearch
Copyright 2012 UC Regents
**elasticpy** - Python Wrapper for ElasticSearch

Copyright 2012 UC Regents

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
28 changes: 15 additions & 13 deletions elasticpy/connection.py
Expand Up @@ -6,7 +6,8 @@
@description Connection Class for elasticpy
'''
import requests
import json
import simplejson as json


_use_gevent = False
try:
Expand All @@ -25,6 +26,11 @@ class ElasticConnection(object):
def __init__(self, timeout=None, **params):
self.status_code = 0
self.timeout=timeout
self.encoding = None
self.headers = {'Content-Type' : 'Application/json; charset=utf-8'}
if params.has_key('encoding'):
self.encoding = 'utf8'
del params['encoding']
if _use_gevent:
if ElasticConnection.session is None:
ElasticConnection.session_lock.acquire()
Expand All @@ -33,42 +39,38 @@ def __init__(self, timeout=None, **params):
else:
self.session = requests.Session(timeout=timeout, **params)
def get(self, url):
headers = {'Content-Type' : 'Application/json'}
try:
response = self.session.get(url,headers=headers,timeout=self.timeout)
response = self.session.get(url,headers=self.headers,timeout=self.timeout)
except requests.ConnectionError as e:
self.status_code = 0
return {'error':e.message}
self.status_code = response.status_code
return json.loads(response.text)
return json.loads(response.content,encoding=self.encoding)
def post(self, url, data):
headers = {'Content-Type' : 'Application/json'}
body = json.dumps(data)
try:
response = self.session.post(url,data=body,headers=headers,timeout=self.timeout)
response = self.session.post(url,data=body,headers=self.headers,timeout=self.timeout)
except requests.ConnectionError as e:
self.status_code = 0
return {'error' : e.message}
self.status_code = response.status_code
return json.loads(response.text)
return json.loads(response.content,encoding=self.encoding)

def put(self, url, data):
headers = {'Content-Type' : 'Application/json'}
body = json.dumps(data)
try:
response = self.session.post(url,data=body,headers=headers,timeout=self.timeout)
response = self.session.post(url,data=body,headers=self.headers,timeout=self.timeout)
except requests.ConnectionError as e:
self.status_code = 0
return {'error' : e.message}
self.status_code = response.status_code
return json.loads(response.text)
return json.loads(response.content,encoding=self.encoding)

def delete(self,url):
headers = {'Content-Type' : 'Application/json'}
try:
response = self.session.delete(url,headers=headers,timeout=self.timeout)
response = self.session.delete(url,headers=self.headers,timeout=self.timeout)
except requests.ConnectionError as e:
self.status_code = 0
return {'error' : e.message}
self.status_code = response.status_code
return json.loads(response.text)
return json.loads(response.content,encoding=self.encoding)
4 changes: 2 additions & 2 deletions elasticpy/search.py
Expand Up @@ -15,13 +15,13 @@ class ElasticSearch(object):
Uses simple HTTP queries (RESTful) with json to provide the interface.
'''

def __init__(self, host='localhost',port='9200',timeout=None,verbose=False):
def __init__(self, host='localhost',port='9200',timeout=None,verbose=False, encoding=None):
self.host = host
self.port = port
self.params = None
self.verbose = verbose
self.timeout = timeout
self.session = ElasticConnection(timeout=timeout)
self.session = ElasticConnection(timeout=timeout,encoding=encoding)

def timeout(self, value):
'''
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -19,7 +19,8 @@
keywords='elasticsearch search wrapper',
packages=['elasticpy'],
install_requires = [
'Requests>=0.12.1'
'Requests>=0.12.1',
'simplejson'
],

)

0 comments on commit fd833b4

Please sign in to comment.