索引新增记录
Python 操作 ElasticSearch 简明教程
· 2019-01-22 17:40:16
索引新增单条记录,代码如下:
# encoding:utf-8
#!/usr/bin/python
from elasticsearch import Elasticsearch
class ElasticObj:
def __init__(self, index_name, index_type, ip ="127.0.0.1"):
'''
:param index_name: 索引名称
:param index_type: 索引类型
'''
self.index_name =index_name
self.index_type = index_type
# 无用户名密码状态
self.es = Elasticsearch([ip])
#用户名密码状态
# self.es = Elasticsearch([ip],http_auth=('elastic', 'password'),port=9200)
'''
索引数据
'''
def Index_Data(self):
list = [
{
"id":1,
"url":'https://www.codercto.com/courses/d/61.html',
"title": "Eloquent: 入门"
},
{
"id":2,
"url":'https://www.codercto.com/courses/d/62.html',
"title": "Eloquent: 模型关联"
},
]
for item in list:
res = self.es.index(index=self.index_name, doc_type=self.index_type, body=item, id=item['id'])
print(res)
if __name__ == "__main__":
obj =ElasticObj("test", "test_type", ip ="127.0.0.1")
obj.Index_Data()
执行结果:
{u'_type': u'test_type', u'_seq_no': 0, u'_shards': {u'successful': 1, u'failed': 0, u'total': 2}, u'_index': u'test', u'_version': 1, u'_primary_term': 1, u'result': u'created', u'_id': u'1'}
{u'_type': u'test_type', u'_seq_no': 0, u'_shards': {u'successful': 1, u'failed': 0, u'total': 2}, u'_index': u'test', u'_version': 1, u'_primary_term': 1, u'result': u'created', u'_id': u'2'}
点击查看所有 Python 操作 ElasticSearch 简明教程 文章: https://codercto.com/courses/l/6.html
新媒体革命——在线时代的媒体、公关与传播
仇勇 / 电子工业出版社 / 2016-2-1 / CNY 50.00
这既是传统媒体的大裂变年代,也是在线媒体开启的新闻业的黄金时代。 信息流动的新法则不仅改变了媒体业,也在重塑公关、传播和商业的面貌。总之,这个世界的连接方式不仅不再相同,而且这一改变不可逆转。在这个全新重启的在线时代里,无论是信息的获取还是商业本身,信任都变得比以往更重要。 从告别传统媒体的那一刻起,我就有着两个小小的“野心”:一是探寻适合在线时代的媒体生产方式;二是让优质内容有权获得......一起来看看 《新媒体革命——在线时代的媒体、公关与传播》 这本书的介绍吧!