Rawes ElasticSearch驱动程序

rawes的Python项目详细描述


rawes是一个针对python的弹性搜索驱动程序。它在请求库之上提供了一个小级别的抽象——足够的抽象有用,但并不能掩盖ElasticSearch伟大的[本地API](http://www.elasticsearch.org/guide/reference/api/)


支持
*节俭支持


>安装
>
u搜索',数据={
'查询':{
'匹配所有':{}
}
}
`````

``python
rawes.elastic(
url='http://localhost:9200',elasticsearch服务的协议、主机和端口。可以是主机列表。
有效协议:http、https、thrift
;默认协议为http,除非端口在9500-9600之间,否则thrift
;默认端口:http=9200、https=443、thrift=9500
超时=30,#超时(秒),
**kwrgs http(s)only:要传递给python"requests"库的附加参数(例如basic auth)
```



构造函数示例:

``python
`es=rawes.elastic();将连接到:http://localhost:9200
es=rawes.elastic('https://localhost')将连接到:https://localhost:443
es=rawes.elastic('thrift://localhost')将连接到:thrift://localhost:9500
es=rawes.elastic('https://example.org:8443',auth=('user','pass'))\https,基本auth连接到:https://example.org:8443
es=rawes.elastic(['http://host1:9200','http://host2:9200','http://host3:9200'])每个调用将被发送到不同的主机,默认为循环策略
`````

rawes.elastic('es'在本例中)的一个实例具有get、post、put、delete和head方法(对于每个http动词)。每个方法都采用以下参数(显示默认值):
``python
es.get(
path='',http url path
data='',http body。可以是字符串或python字典(将自动转换为json)
params={},作为python字典传递的http url params
headers={},作为python字典传递的http headers
**kwargs要传递到python"请求"库或thrift restriquest的其他参数
#示例:headers,basic auth


```


























>示例








```>twittetwitte类型为tweet类型为tweet类型为tweet类型为tweet类型的tweet创建一个新文档,id id为1
```python
>es.put('tweets/tweets/投入"blogs/post/2",data={
'user':'dan',
'post'post'post'post'post'post'post'post'post'post'post'2012-8-27t09:30:03z',
'title':'elasticsearch',
'body':'elasticsearch'blog'elasticsearch'
})
`````````



>搜索一个文档,指定http参数
```python
es.get.get('tweets/tweet/tweet/tweet/搜索',data={
'query'query>'全部匹配':{
}
}
}
}
}
```


>搜索带有json字符串的文档
``python
````python
es.get('tweetweets,blo博客`搜索',data=""
{
"查询":{
"全匹配":{{}

<<
<<<
<<<








/>python
es.put('someindex/sometype/123',data={
"值":100,
'other':'stuff'stuff'
}
es.post('someindex/sometype/123/u更新,data={
'script':'ctx.'source.value+=value',
'params':{
'value':50
}
>>
`````


删除一个文档,删除一个文档,删除一个文档,删除(tweets/tweet/tweet/1')
``


大容量加载index" : {}}
{"key":"value1"}
{"index" : {}}
{"key":"value2"}
{"index" : {}}
{"key":"value3"}
'''

es.post('someindex/sometype/_bulk', data=bulk_body)

bulk_list = [
{"index" : {}},
{"key":"value4"},
{"index" : {}},
{"key":"value5"},
{"index" : {}},
{"key":"val







bulk-body_2='\n'.join(map(json.dumps,bulk_list))+'\n'


es.post('someindex/sometype/sometype/some bulk,data=bulk-body_2)
`















替代设置es&;lt;http动词的第一个参数,而不是设置es&;lt;http动词的第一个参数。gt;校准l到http url路径,还可以使用python属性和项访问器来构建url路径。例如:
``python
es.post('tweets/tweet/tweet/',data={
'user':'dwnoble',
'post'post'u date':'2012-8-27t09:15:59',
'message':'更多关于elasticsearch的tweets'
`````


````` python
``python
`` es.tweets.tweet.post(data={
`` user':'dwnoble',
>es.post.tweets.tweet.post(data={
``````````"发布日期":"2012-8-27t09:15:59",
消息":"更多关于elasticsearch的tweets"
}"
````

>或使用项访问器([]符号)。这对于python属性中不允许的字符很有用:
``python
es['tweets']['tweet'].post(data={
'user':'dwnoble',
'post戥date':'2012-8-27t09:15:59',
'message':'more tweets about elasticsearch'
})
````

更多示例:

键入"tweets"
``python
es.tweets.tweets.tweet.\u search.get(data={'query':{'match'all'{{}}}})
````





>在"tweets"和"blogs"索引搜索"tweets"和"blogs"索引中使用json字符串的任何类型的文档
``python
``` tweets,blogs']```` tweets,blogs']```` python
>>```````````````` python
>>


/>
json编码默认情况下,rawes将对日期时间进行编码(需要时区!)在将json交给elasticsearch之前,以'second'精度发送到utc iso8601字符串。如果ElasticSearch未定义映射,则将导致默认的"DateOptionalTime"映射。此自动序列化需要时区:您可能需要使用Python DateUtil(仅限Python 2.x)或Pytz之类的Python模块,以简化您的生活。
``python
From DateTime Import DateTime
From DateUtil Import Tz
eastern'u timezone=tz.gettz('america/new'u york')



es.put('tweets/tweet/99'tweets/tweet/99',data={
'user''dwnoble',
'post'u date''post'post'datetime(2012、8、27、8、0、30,tzinfo=eastern时区东部时区),
'message':'tweets'tweets'tweets/tweet/99'[''''ussource'['post'post'post'post'u date''post'post'post'sut'date']]br/>退货:
U'2012-08-27T12:00:30Z'
```

>或者,可以使用json-encoder参数指定自定义json编码器:
``python
from datetime import datetime
from dateutil import tz
eastern-timezone=tz.gettz('america/new-york')

def encode自定义(obj):
如果存在(obj,datetime):
返回obj.astimezonstrftime('%y-%m-%d'%br/>raise typeerror(repr(obj)+"不是json可序列化的")


es.put('tweets/tweet/445',data={
‘用户’:'dwnoble',
‘post‘日期’:日期时间(2012、11、12、9、45、45、tzinfo=东部时区),
‘消息’:‘关于elasticsearch的tweeting’关于elasticsearch(2012、11、11、12、9、45、45、45、tzinfo=东部时区时区),
‘消息’:‘关于elasticsearch的tweeting’tweeting’
>>>>>>>
es.get('tweets/tweet/445')['''u source']['post撸date']
返回:
u'2012-11-12'
````

e在rawes.elastic构造函数中指定:

``python
import rawes
from datetime import datetime
from dateutil import tz
eastern撸u timezone=tz.gettz('america/new撸york')

def encode撸custom(obj):
if isinstance(obj,datetime):
return obj.astimezone(tz.tzutc()).strftime('%y-%m-%d')
raise typeerror(repr(obj)+"不是json可序列化的")


es.put('tweets/tweet/445',data={
'user':'dwnoble',
'post\date':日期时间(2012,11,12,9,45,45,tzinfo=东部时区),
"消息":"关于ElasticSearch的tweeting"


es.get('tweet s/tweet/445')[''u source']['post'u date']
返回:
u'2012-11-12'
````

json解码
----

指定用于分析ElasticSearch结果。这里的一个常见用例可能是将iso8601日期解析为python datetime对象。

使用iso8601格式的日期字符串索引文档:

``python
import rawes
es=rawes.elastic()

'user':'dan',
'post撸date':'2013-7-04t23:14:53z,
标题:'elasticsearch 2',
正文:'更多关于elasticsearch的博客'
})
````

定义自定义json解码器:
```python
导入json
导入pytz
导入dateutil.parser

iso8601jsondecoder类(json.json decoder):
" />自动解码ISO8601字符串,在UTC时区
"
def初始(self):
json.jsondecoder.\uu初始(self,object_u hook=self.dict_to_object)

def dict_to_object(self,d):
对于k,v in d.iteritems(:
如果k="post_date":
d[k]=dateutil.parser.parse(v)
返回d



iso8601_json_decoder=iso8601jsondecoder()

`````

>现在使用我们的json解码器检索此文档日期"]
返回:
U'2013-7-2013-7-04t23:14:53z'
es.get("blog/post/3",json解码器=iso8601_json解码器解码)["_source"]["post_date"]
返回:
datetime.datetime.datetime(2013,7,4,23,14,53,tzinfo=tzutUTC())
es默认解码器=rawes.弹性(2013,7,4,4,23,14,53,tzinfo=tzutUTC())
es/>es默认解码器=rawes.elastic json_decoder=ISO8601_json_decoder.decode)
es_decoder.get("blogs/post/3")["u source"]["post_date"]
返回:
datetime.datetime(2013,7,4,23,14,53,tzinfo=tzutc())
````

错误处理
----
从0.5版开始,rawes.elastic构造函数抛出rawes.elasticex异常。异常任何时候ElasticSearch返回的HTTP状态代码为400或更高。

``python
from rawes.elasticException import elasticException
es=rawes.elastic('localhost:9200')
try:
es.get('invalid_index/invalid_type/123')
elasticException as e:
si由于索引无效,此异常处理程序将运行
print e.result
仅适用于Python2.x版本。在使用rawes之前,必须先安装thrift python模块
``bash
$pip install thrift
````

如果在非标准端口上使用Thrift,请指定"thrift://"url
``pyth on
import rawes
es\thrift=rawes.elastic('thrift://localhost:8500')
``````

connection pooling
---------
rawes支持elasticsearch主机的连接池:

``pyth打开
导入rawes=rawes.elastic(['http://host1:9200','http://host2:9200','http://host3:9200'])
````


>类以循环的方式循环这些主机,每次调用请求方法。
可以通过将选项传递给"rawes.elastic"构造函数的"connection-pool"参数来更改连接池策略或其他连接池参数:

``python
import-rawes
es=rawes.elastic(['http://host1:9200','http://host2:9200','http://host3:9200',connection-pool\u-kwargs={
"selector-clas"s":rawes.connection_pool.randomselector
}
```


所使用的连接池实现来自[elasticsearch py](https://github.com/elasticsearch/elasticsearch-py/blob/master/elasticsearch/connection_pool.py)项目。


run unit tests
----
rawes的单元测试要求python thrift和mock模块运行:
``bash
$pip install thrift
$pip install mock
````

run tests:
``bash
$python-m unittests
```


不需要"节衣节俭"就可以了
``` bash
$python3-m unittest.py3k
````



许可证
----
apache 2.0许可证


请联系
----
>[@dwnoble(https://twtwitter.com/dwnoble)









<0.5.5.5.5(2014-1)<0.5.5.5(2014-1)

``````>




----
*已从setup.py中删除测试包(感谢[alisaifee](https://github.com/alisaifee))



>0.5.4(2014-1)
——————————————————【elasticsearch py】(https://github.com/elasticsearch/elasticsearch-py)添加的连接池(感谢【mishu-】(https://github.com/mishu-)

>0.5.3(2013-7-4)
————————————————————————————(感谢【adisbladis】(https://github.com/adisbladis))
*向rawes.elastic构造函数添加了json编码器参数


>0.5.2(2013-5-17)
----
*python 2.6兼容性修复(感谢[simon kelly](https://github.com/snopoke)!)

0.5.1(2013-5-15)
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————wes现在在http状态代码上抛出一个rawes.elastic exception.elasticexception,默认值为>;=400。删除了rawes.elastic"except_on_error"参数。

>0.4.0(2013-4-21)
---------
*通过删除connection_type参数并合并到url中简化了弹性构造函数(感谢[nvie](https://github.com/nvie)!)
*向弹性构造函数添加了可选的**kwargs。用于指定基本身份验证或特定头

0.3.6(2013-1-8)
---------
*固定请求>;1.0不兼容,将要求更改回"请求>;=0.11.1"


0.3.5(2013-1-8)
----
*http和thrift的超时错误修复

0.3.4(2013-1-7)
----------
*向rawes.elastic构造函数添加了"except_on_error"布尔选项。如果设置为true,则当ElasticSearch返回的状态代码为>;=400(即出现错误时)


0.3.3(2012-12-21)
----
*限制的"请求"要求从>;=0.11.1到>;=0.11.1,<;1.0.0'

0.3.2(2012-12-11)
----
*节俭错误修复(THANKS[anathomical](https://github.com/anathomical)!)
*放宽了"请求"要求,从=0.14.2到=0.11.1e编码(感谢[atkinson](https://github.com/atkinson)!)
*增加了对自定义json编码的支持

>0.2(2012-08-10)
----
*当前版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何使用SeleniumWebDriver捕获特定元素的屏幕截图而不是整个页面?   WAS 8.5中的java JAXWS异步IO异常   java Android Studio无法解析数组适配器中的符号项   swing Java在运行时添加和删除按钮   java Spring Boot 2.0。x禁用特定配置文件的安全性   java这是我的最后一个代码,我在“if(Users[B].substring(0,1)==“1”){//Find String Cut Char Function”上遇到错误   单击菜单选项后打开“活动”时发生java错误   java Tic Tac Toe NullPointerException   java如何做数学题。随机进入for循环   java Http状态404错误处理spring mvc   java循环依赖双向@OneToMany JPA关系   java我可以在不重新启动项目的情况下添加servlet吗   java在蓝色/绿色部署中如何管理共享数据库?   这是一个系统。currentTimeMillis()是Java中时间性能的最佳度量?