zoukankan      html  css  js  c++  java
  • ElasticSearch Python 基本操作

    创建索引

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch('192.168.149.96:9200')
    
    mappings = {
        "mappings": {
                "properties": {
                    "perName": {
                        "type": "keyword"
                    },
                    "schoolName": {
                        "type": "keyword"
                    },
                    "perGrade": {
                        "type": "double"
                    },
                    "position": {
                        "type": "keyword"
                    },
                    "proMonth": {
                        "type": "date",  # 可以接受如下类型的格式
                        "format": "yyyy-MM"
                    },
                    "detailedTime": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                    },
                    "projectName": {
                        "type": "keyword"
                    },
                    "targetOutput": {
                        "type": "double"
                    },
                    "actualOutput": {
                        "type": "double"
                    },
                    "yieldReachingRate": {
                        "type": "double"
                    },
                    "lateness": {
                        "type": "double"
                    },
                    "overtime": {
                        "type": "double"
                    },
                    "workingHours": {
                        "type": "double"
                    },
                    "groupRanking": {
                        "type": "double"
                    },
                    "qualityInspection": {
                        "type": "double"
                    },
                    "numberOfCustomer": {
                        "type": "double"
                    }
                }
        }
    }
    
    result = es.indices.create(index='new_source', body=mappings)
    print(result)
    

    删除索引

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch('192.168.149.96:9200')
    
    result = es.indices.delete('production_data')
    print(result)
    

    插入数据

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch('192.168.149.96:9200')
    
    action = {
        'perName': '张三',
        'schoolName': '清华',
        'perGrade': 101.0,
        'position': '职务',
        'proMonth': '2020-03',
        'detailedTime': '2020-03-09 00:00:00',
        'projectName': '画框转写',
        'targetOutput': 100.0,
        'actualOutput': 90.0,
        'yieldReachingRate': 0.9,
        'lateness': 1.0,
        'overtime': 1.0,
        'workingHours': 8.0,
        'groupRanking': 3.0,
        'qualityInspection': 2.0,
        'numberOfCustomer': 1.0
    }
    
    result = es.index(index="source_data", body=action)
    print(result)
    

    批量插入时 action 给成列表, 遍历即可

    查询所有数据

    from elasticsearch import Elasticsearch
    
    es_con = Elasticsearch([{'host': '192.168.149.96', 'port': 9200}])
    data_agg = es_con.search(
        index='base_data',
        size=1000,
        body={
            "query": {
                "match_all": {}
            },
        }
    )
    
    print(data_agg, '666666666')
    
    for data in data_agg.get('hits').get('hits'):
        list_data = data.get('_source')
        if '' in list(list_data.values()):
            print(list_data)
            id_ = data.get('_id')
            # es_con.delete(index='base_data', id=id_)
        else:
            pass
            # print(list_data)
    
  • 相关阅读:
    Github账户注册的过程
    目前流行的源程序版本管理软件和项目管理软件都有哪些, 各有什么优缺点?
    作业二:四则运算
    学习进度
    对构建之法的一些问题
    个人介绍
    对《软件工程》课程的总结
    作业八 更新版
    作业八
    冲刺总结博客
  • 原文地址:https://www.cnblogs.com/geoffreygao/p/13258018.html
Copyright © 2011-2022 走看看