From fd833b47ec76bca8fe552339692e4bf979f8cbfc Mon Sep 17 00:00:00 2001 From: Luke Campbell Date: Thu, 7 Jun 2012 10:59:53 -0400 Subject: [PATCH] Luke: json and readme patches --- README.md | 37 +++++++++++++++++++++---------------- elasticpy/connection.py | 28 +++++++++++++++------------- elasticpy/search.py | 4 ++-- setup.py | 3 ++- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index df190f5..bfd8a52 100644 --- a/README.md +++ b/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 +Author: [Luke Campbell](http://lukecampbell.github.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') @@ -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. @@ -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', @@ -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. diff --git a/elasticpy/connection.py b/elasticpy/connection.py index 5ac6065..9a1f5a8 100644 --- a/elasticpy/connection.py +++ b/elasticpy/connection.py @@ -6,7 +6,8 @@ @description Connection Class for elasticpy ''' import requests -import json +import simplejson as json + _use_gevent = False try: @@ -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() @@ -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) diff --git a/elasticpy/search.py b/elasticpy/search.py index 7ad0be3..169771d 100644 --- a/elasticpy/search.py +++ b/elasticpy/search.py @@ -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): ''' diff --git a/setup.py b/setup.py index 2d0dc37..a98e931 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,8 @@ keywords='elasticsearch search wrapper', packages=['elasticpy'], install_requires = [ - 'Requests>=0.12.1' + 'Requests>=0.12.1', + 'simplejson' ], )