索引新增记录

索引新增单条记录,代码如下:

# 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

查看所有标签

极致产品

极致产品

周鸿祎 / 中信出版社 / 2018-6 / 58.00

周鸿祎作为*知名的产品经理之一,一手打造了众多国民级的产品。他关于打造爆款的理念,比如刚需、高频、“小白”思维等,已成为网络热词而被广泛接受。 本书是周鸿祎首次系统总结其20年产品经理的心得,不仅将以往的理念进行总结、归纳,而且在与包括各方面创业者、产品经理的碰撞后,将其观念进一步升华,成为迄今为止首部将其产品理念倾囊相授的作品。一起来看看 《极致产品》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具