美文网首页
ElasticSearch04批量操作

ElasticSearch04批量操作

作者: 极光火狐狸 | 来源:发表于2017-08-23 11:52 被阅读106次

读取多份文档

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

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"
            }
        }
    ]
)

相关文章

网友评论

      本文标题:ElasticSearch04批量操作

      本文链接:https://www.haomeiwen.com/subject/oksddxtx.html