美文网首页
ES_Python使用ES的基本操作语句

ES_Python使用ES的基本操作语句

作者: 猪儿打滚 | 来源:发表于2020-03-18 16:40 被阅读0次

    前言

    原因:性能测试需要准备一个亿的基础数据,为了加快速度,直接操作ES往里面添加数据
    结果:所以初略学了下python操作es的基本操作,以后再系统去学习ES

    说明

    本文可以说是根据博文进行的,为了以后复盘方便,所以在实践过后,就“抄袭”过来(原文有个bug)

    操作语句(同个py文件,为了方便看,所以拆开)

    1、连接ES
    from elasticsearch import Elasticsearch
    from elasticsearch import helpers
    
    # elasticsearch集群服务器的地址
    ES = [
        '127.0.0.1:9200'
    ]
    
    # 创建elasticsearch客户端
    es = Elasticsearch(
        ES,
        # 启动前嗅探es集群服务器
        sniff_on_start=True,
        # es集群服务器结点连接异常时是否刷新es节点信息
        sniff_on_connection_fail=True,
        # 每60秒刷新节点信息
        sniffer_timeout=60
    )
    
    1、创建索引
    # 创建索引
    
    indexes_test = {
        "mappings": {
            "type_doc_test": {  # 对应doc_type
                "properties": {  # 内容
                    "id": {
                        "type": "long",  # 类型
                        "index": "false"  # 不创建索引
                    },
                    "name": {
                        "type": "keyword",  # keyword不会进行分词
                        "index": "false"
                    },
                    "hobby": {
                        "type": "text",  # text会进行分词
                        "index": "false"
                    },
                    "tags": {  # tags可存储json格式
                        "type": "object",
                        "properties": {  # 内容
                            "age": {"type": "integer", "index": False},
                            "desc": {"type": "keyword", "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"
                    }
                }
            }
        }
    }
    res = es.indices.create(index='index_test_3', body=indexes_test)
    print(res)
    
    2、往上面所创建的索引中添加数据
    # 2、往上面所创建的索引中添加数据
    # 2.1、添加单条数据
    test_data = {
        "id": "202003181455",
        "name": "测试_昵称",
        "hobby": "测试_爱好",
        # tags字段的格式
        "tags": {"age": "18", "desc": "自我介绍"},
        "createTime": "2020-03-18",
        "updateTime": "2020-03-18"
    }
    # test_data_rs = es.index(index="index_test_3", doc_type="type_doc_test", body=test_data)
    # print(test_data_rs)
    """
    {'_index': 'index_test_3', '_type': 'type_doc_test', '_id': '-UZ07HAB65XknjJ4CG9x', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
    """
    
    # 2.2、添加多条数据
    ACTIONS = []
    
    action1 = {
        "_index": "index_test_3",  # 索引
        "_type": "type_doc_test",  # doc_type
        "_id": "DS23HAB65XknjDSEA5R",  # ID,指定
        # 索引内容
        "_source": {
            "name": "小王",
            "hobby": "小王的爱好",
            # 注意添加多条数据的tags中内容的格式和添加单条数据的格式不一样
            "tags.age": "16",
            "tags.desc": "我是16岁的小王",
            "createTime": "2020-05-18",
            "updateTime": "2020-05-18"
        }
    }
    
     action2 = {
         "_index": "index_test_3",  # 索引
         "_type": "type_doc_test",  # doc_type
         # "_id": "DS23HAB65XknjDSEA5R",  # 不指定则会自动生成id数据
         # 索引内容
         "_source": {
             "name": "小红",
             "hobby": "小红的爱好",
             # 注意添加多条数据的tags中内容的格式和添加单条数据的格式不一样
             "tags.age": "14",
             "tags.desc": "我是14岁的小红",
             "createTime": "2020-05-17",
             "updateTime": "2020-05-17"
         }
     }
    
     ACTIONS.append(action1)
     ACTIONS.append(action2)
    
     res = helpers.bulk(es, ACTIONS)
     print(res)  # (2, [])
    
    3、查询所有数据
    datas = es.search(index="index_test_3", doc_type="type_doc_test")
    print(datas)
    
    4、查询一条数据,根据id查找
    res = es.get(index="index_test_3", doc_type="type_doc_test", id="DS23HAB65XknjDSEA5R")
    print(res)
    
    5、根据关键词查找数据
    search_doc = {
        # 根据id查询(还有很多,以后完善)
        "query": {
                "match": {
                    "_id": "DS23HAB65XknjDSEA5R"
                }
            }
    }
    res = es.search(index="index_test_3", doc_type="type_doc_test", body=search_doc)
    print(res)
    
    6、删除一条数据,根据id删除
    # 删除一条数据,根据id删除
    res = es.delete(index="index_test_3",doc_type="type_doc_test", id ="DS23HAB65XknjDSEA5R")
    print(res)
    """
    {'_index': 'index_test_3', '_type': 'type_doc_test', '_id': 'DS23HAB65XknjDSEA5R', '_version': 4, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 4, '_primary_term': 1}
    """
    
    
    res = es.get(index="index_test_3", doc_type="type_doc_test", id="DS23HAB65XknjDSEA5R")
    print(res)  # 该数据已被删除,报错
    

    相关文章

      网友评论

          本文标题:ES_Python使用ES的基本操作语句

          本文链接:https://www.haomeiwen.com/subject/ojlcyhtx.html