一、开发需要的环境工具
- Python 建议直接使用anaconda集成,方便省事(注意环境变量的设置)
- elasticsearch服务,直接从官网下载安装,本文以2.4.4版本为例,cmd启动服务,默认端口9200,建议安装head工具,方便集群监控,数据浏览
- postman测试工具 ,es提供restful接口,可通过post等请求进行操作
二、通过Python与es服务建立连接
def connect_elasticsearch():
_es = None
_es = Elasticsearch([{'host':'localhost','port':9200}])
if _es.ping():
print('yay connected')
else:
print('not connected')
return _es`
三、通过Python创建索引及类型`在这里插入代码片
def create_index(es_object,index_name='recipes'):
created = False
settings = {
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"members":{
"dynamic":"strict",
"properties":{
"title":{
"type":"string"
},
"submitter":{