python操作elasticsearch

2022/4/22 20:13:19

本文主要是介绍python操作elasticsearch,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import time

import elasticsearch.helpers
from faker import Faker
from elasticsearch import Elasticsearch

f = Faker(locale='zh_CN')
es = Elasticsearch("http://ip:port")

start_time = time.time()


def re_list(start: int, setup: int, header=False):
    l = [{
        '_op_type': 'index',
        '_index': "user_info",  # index
        "_source": {
            "ID": i + start * setup,
            "Name": f.name(),
            "Address": f.address(),
            "Ssn": f.ssn(),
            "Company": f.company(),
            "Job": f.job(),
            "Email": f.email(),
            "Url": f.domain_name(),
            "Card": f.credit_card_number(),
            "PhoneNumber": f.phone_number(),
            "UserName": f.user_name(),
            "Password": f.password(),
            "HeadImg": f.image_url(),
        }
    } for i in range(0, setup)]

    # print(l)

    # 插入多条数据
    elasticsearch.helpers.bulk(es, l)

    # 插入一条数据
    # document = {'ID': 0, 'Name': '张芳', 'Address': '吉林省雷市锡山潘街H座 777298', 'Ssn': '350524196101092684',
    #             'Company': '晖来计算机传媒有限公司', 'Job': '技术总监/经理', 'Email': 'pgu@example.net', 'Url': 'xiaozhu.cn',
    #             'Card': '378592230159964', 'PhoneNumber': '13927464009', 'UserName': 'mpan', 'Password': '$@_4FJ7vf0',
    #             'HeadImg': 'https://placeimg.com/839/937/any'}
    # es.index(index="user_info", document=document)

    # 查询数据
    # resp = es.search(index="user_info", query={"multi_match": {"query": "777298"}})
    # resp = es.search(index="user_info", query={"match_all": {}})
    # for hit in resp["hits"]["hits"]:
    #     if hit:
    #         print(hit["_source"])


if __name__ == '__main__':
    for i in range(0, 1):
        re_list(i, 1)

elasticsearch-py官网
faker-py官网



这篇关于python操作elasticsearch的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程