栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

使用python库elasticsearch操作es

使用python库elasticsearch操作es

1、单个写入

   from elasticsearch import Elasticsearch    
   es = Elasticsearch("192.168.0.11:9200")
   mappings = {
        "id": "11",
        "serial": "版本",
        "tags": {"comment": "标签3"},
        "status": "tag1"
    }
  
   es.index(index="index-test", doc_type="doc_test", body=mappings)  

2、批量写入

    from elasticsearch import Elasticsearch
    from elasticsearch import helpers
    es = Elasticsearch("192.168.0.11:9200")
    
    data_list = [{
        "_index": "index-test",
        "_type": "doc_test",
        "_source": {
            "id": "12",
            "serial": "版本",
            "tags": {"comment": "标签3"},
            "status": "tag1"
        },
    }, {
        "_index": "index-test",
        "_type": "doc_test",
        "_source": {
            "id": "13",
            "serial": "版本",
            "tags": {"comment": "标签3"},
            "status": "tag1"
        }
    }]
     
    helpers.bulk(es, data_list, request_timeout=1000)

3、单批次读(数据量大读取易超时)

from elasticsearch import Elasticsearch


def get_data_from_es():
    es = Elasticsearch(['192.168.0.11:9200'])

    body = {
              "query": {
                "match": {
                  "serial.keyword": "版本"
                }
              }
            }
    # 获取查询结果数量
    res_count = es.count(index='index-test', doc_type='doc_type', body=body, request_timeout=10000)
    # print(res_count)
    total_size = res_count['count']
    # print(total_size)
    query = es.search(index='index-test', doc_type='doc_type', body=body, request_timeout=10000, size=total_size)
    print(len(query['hits']['hits']))

4、循环读

from elasticsearch import Elasticsearch


def get_data_from_es():
    es = Elasticsearch(['192.168.0.11:9200'])

    body = {
              "query": {
                "match": {
                  "serial.keyword": "版本"
                }
              }
            }
    # Initialize the scroll
    page = es.search(index=index_name, body=body, scroll='5m', size=10000, request_timeout=1000)
    # 第一页结果,为list类型
    results = page['hits']['hits']

    sid = page['_scroll_id']
    scroll_size = page['hits']['total']['value']

    while scroll_size > 0:
        # print("Scrolling...")
        page = es.scroll(scroll_id=sid, scroll='2m')
        # Update the scroll ID
        sid = page['_scroll_id']

        # Get the number of results that we returned in the last scroll
        scroll_size = len(page['hits']['hits'])
        print("scroll size: " + str(scroll_size))

        # 叠加查询结果
        results += page['hits']['hits']
    return results

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号