更新记录
Python 操作 ElasticSearch 简明教程
· 2019-01-22 18:03:02
更新单条记录,代码如下:
# 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 Upate_Data_By_Id(self, id, data):
print self.es.update(index=self.index_name, doc_type=self.index_type, id=id, body=data)
if __name__ == "__main__":
obj =ElasticObj("test", "test_type", ip ="127.0.0.1")
obj.Upate_Data_By_Id(1, {"doc":{"title":"更新后的标题"}})
执行结果:
{u'_type': u'test_type', u'_seq_no': 1, u'_shards': {u'successful': 1, u'failed': 0, u'total': 2}, u'_index': u'test', u'_version': 2, u'_primary_term': 1, u'result': u'updated', u'_id': u'1'}
查看数据:
更新前:
[root@centos7php7 elasticsearch-6.4.2]# curl -X GET 'localhost:9200/test/test_type/1?pretty'
{
"_index" : "test",
"_type" : "test_type",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"url" : "https://www.codercto.com/courses/d/61.html",
"id" : 1,
"title" : "Eloquent: 入门"
}
}
更新后:
[root@centos7php7 elasticsearch-6.4.2]# curl -X GET 'localhost:9200/test/test_type/1?pretty'
{
"_index" : "test",
"_type" : "test_type",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"url" : "https://www.codercto.com/courses/d/61.html",
"id" : 1,
"title" : "更新后的标题"
}
}
点击查看所有 Python 操作 ElasticSearch 简明教程 文章: https://codercto.com/courses/l/6.html
父与子的编程之旅
桑德 (Warren Sande)、桑德 (Carter Sande) / 苏金国、易郑超 / 人民邮电出版社 / 2014-10-1 / CNY 69.00
本书是一本家长与孩子共同学习编程的入门书。作者是一对父子,他们以Python语言为例,详尽细致地介绍了Python如何安装、字符串和操作符等程序设计的基本概念,介绍了条件语句、函数、模块等进阶内容,最后讲解了用Python实现游戏编程。书中的语言生动活泼,叙述简单明了。 为了让学习者觉得编程有趣,本书编排了很多卡通人物及场景对话,让学习者在轻松愉快之中跨入计算机编程的大门。 第 2 版新增内......一起来看看 《父与子的编程之旅》 这本书的介绍吧!