python操作es 7.13.0

这篇博客介绍了如何使用Python的Elasticsearch库来连接ES集群,并创建自定义索引。创建索引时定义了不同字段的数据类型,如id、serial、tags等内容。同时,展示了如何上传单条及批量数据到索引中。注意在数据结构中,tags字段应以对象形式存储。最后,确保在操作完成后关闭连接。
摘要由CSDN通过智能技术生成

连接:

import elasticsearch
from elasticsearch import Elasticsearch
es = Elasticsearch(
    hosts=es_host,
    http_auth=(username, passwd),
    port=es_port,)
print(es.info())  #  这可以打印输出es的节点集群版本等信息

 创建自定义索引:(可以只指定名称, 不定义mappings)

mappings = {
    "mappings": {
            "properties": {
                "id": {
                    "type": "long",
                    "index": "false"
                },
                "serial": {
                    "type": "text",  # keyword不会进行分词,text会分词
                    "index": "false"  # 不建索引
                },
                # tags可以存json格式,访问tags.content
                "tags": {
                    "type": "object",
                    "properties": {
                        "content": {"type": "keyword", "index": True},
                        "dominant_color_name": {"type": "keyword", "index": True},
                        "skill": {"type": "keyword", "index": True},
                    }
                },
                "hasTag": {
                    "type": "long",
                    "index": True
                },
                "status": {
                    "type": "long",
                    "index": True
                },
                "createTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                },
                "updateTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
es.indices.create(index = 'test_test0000000000000000000',body =mappings)  # 创建索引,若不成功会报错

# 上传单条数据

action = {
    "id": "1111122222",
    "serial": "版本",
    # 以下tags.content是错误的写法
    # "tags.content" :"标签2",
    # "tags.dominant_color_name": "域名的颜色黄色",
    # 正确的写法如下:
    "tags": {"content": "标签3", "dominant_color_name": "域名的颜色黄色"},
    # 按照字典的格式写入,如果用上面的那种写法,会直接写成一个tags.content字段。
    # 而不是在tags中content添加数据,这点需要注意
    "tags.skill": "分类信息",
    "hasTag": "123",
    "status": "11",
    "createTime": "2018-02-02",
    "updateTime": "2018-02-03",
}
es.index(index="test_test0000000000000000000", doc_type="_doc", body=action)

# 上传多条

doc = [
    {"index": {}},
    {
        "id": "1111122222",
        "serial": "版本",
        "tags.content": "标签2",
        "tags.dominant_color_name": "域名的颜色黄色",
        "tags.skill": "分类信息",
        "hasTag": "123",
        "status": "11",
        "createTime": "2018-2-2",
        "updateTime": "2018-2-3",
    },
    {"index": {}},
    {
        "id": "1111122222",
        "serial": "版本",
        "tags.content": "标签2",
        "tags.dominant_color_name": "域名的颜色黄色",
        "tags.skill": "分类信息",
        "hasTag": "123",
        "status": "11",
        "createTime": "2018-2-2",
        "updateTime": "2018-2-3",
    },]
a = es.bulk(index='test_test0000000000000000000', doc_type='_doc', body=doc)

es.close()  #记得关闭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值