更新记录

更新单条记录,代码如下:

# 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

查看所有标签

失业的程序员

失业的程序员

沈逸 / 2014-5-1 / 39.00元

这是一个程序员从失业到自行创业的奋斗历程,虽然囧事连连、过程曲折,却充满了趣味。本书以作者的真实创业经历为主线,文字幽默诙谐,情节生动真实,包括了招聘、团队管理和用户公关,以及技术架构设计、核心代码编写、商务谈判、项目运作等场景经验。 从初期的创业伙伴、领路人,到商业竞争对手,各种复杂的关系在各个关键时刻却都发生了意想不到的逆转。在历经千辛万苦,眼看快要成功时,主人公却几乎再次失业。 ......一起来看看 《失业的程序员》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码