更新记录

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

# 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://www.codercto.com/courses/l/6.html

查看所有标签

C语言入门经典

C语言入门经典

霍顿 (Ivor Horton) / 清华大学出版社 / 2008-4-1 / 69.80元

本书是编程语言先驱者Ivor Horton的经典之作,是C语言方面最畅销的图书品种之一。本书集综合性、实用性为一体,是学习C语言的优秀入门教材,在世界范围内广受欢迎,口碑极佳。书中除了讲解C程序设计语言,还广泛介绍了作为一名C程序设计人员应该掌握的必要知识,并提供了大量的实用性很强的编程实例。本书的目标是使你在C语言程序设计方面由一位初学者成为一位称职的程序员。读者基本不需要具备任何编程知识,即可......一起来看看 《C语言入门经典》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码