ElasticSearch04批量操作

读取多份文档

显式声明索引名称和文档类型

from elasticsearch import Elasticsearch
from pprint import pprint

es = Elasticsearch(hosts=["192.168.1.132"])

# 根据索引名称、类型名称来获取多个文档
s = es.mget(
    index="megacorp",
    doc_type="employee",
    body={
        "docs": [
            {"_id": 1},
            {"_id": 2},
            {"_id": 3},
            {"_id": 4},
            {"_id": 5},
        ]
    }
)
pprint(s)

在body中声明索引名称和文档类型

from elasticsearch import Elasticsearch
from pprint import pprint

es = Elasticsearch(hosts=["192.168.1.132"])

s = es.mget(
    body={
        "docs": [
            {"_index": "megacorp", "_type": "employee", "_id": 1},
            {"_index": "megacorp", "_type": "employee", "_id": 2},
            {"_index": "megacorp", "_type": "employee", "_id": 3},
            {"_index": "website", "_type": "blog", "_id": 1},
            {"_index": "website", "_type": "blog", "_id": 2},
            {"_index": "website", "_type": "blog", "_id": 3},
            {"_index": "website", "_type": "blog", "_id": 4},
            {"_index": "website", "_type": "blog", "_id": 5},
            {"_index": "website", "_type": "blog", "_id": 6},
            {"_index": "website", "_type": "blog", "_id": 7},
        ]
    }
)

pprint(s)

 
 

写入多分文档

es.bulk

from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
from pprint import pprint

es = Elasticsearch(hosts=["192.168.1.132"])

s = es.bulk(
    index="website",
    doc_type="blog",
    body=[
        # {action: metadata}
        {"create": {"_id": 8}},
        # {request body}
        {
            "title": "created using es.bulk",
            "date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
            "text": "try to write a doc to es"
        },
        # {action: metadata}        
        {"create": {"_id": 19}},
        # {request body}
        {
            "title": "created using es.bulk",
            "date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
            "text": "try to write a doc to es"
        }
    ]
)

helps.bulk

from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
from pprint import pprint

es = Elasticsearch(hosts=["192.168.1.132"])

s = helpers.bulk(
    client=es,
    actions=[
        {
            # 备注: _op_type 默认采用的是index.
            # index和create的区别在于, index表示重建索引, version会自动+1, 而
            # create则表示创建, 当数据已存在时, 会报错.
            "_op_type": "index",  # create, delete, index, update
            "_index": "website",
            "_type": "blog",
            "_id": 11,
            "_source": {
                "title": "created using helpers.bulk",
                "date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
                "text": "try to write a doc to es"
            }
        },
        {
            "_op_type": "index",
            "_index": "website",
            "_type": "blog",
            "_id": 12,
            "_source": {
                "title": "created using helpers.bulk",
                "date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
                "text": "try to write a doc to es"
            }
        }
    ]
)

转载于:https://my.oschina.net/u/3579120/blog/1533359

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值