ElasticSearch04批量操作
ElasticSearch04批量操作
AllenOR灵感 发表于8个月前
ElasticSearch04批量操作
  • 发表于 8个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

读取多份文档

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

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"
            }
        }
    ]
)
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 8
博文 2139
码字总数 82983
×
AllenOR灵感
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: