elasticsearch6.5.4批量插入数据
# -*- coding: utf-8 -*-
import json
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
ES_HOST = '127.0.0.1'
ES_USER = ''
ES_PASSWD = ''
ES_PORT = 9002
class ES():
def __init__(self):
self.es = Elasticsearch([ES_HOST], port=ES_PORT, timeout=300)
def create_index(self, index):
"""
创建索引
:param index: 索引名
:return:
"""
mappings = {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"book": {
"properties": {
"author": {"type": "text"},
"characters": {"type": "text"},
"copies": {"type": "long", "ignore_malformed": False},
"otitle": {"type": "text"},
"tags": {"type": "text"},
"title": {"type": "text"},
"year": {"type": "long", "ignore_malformed": False, "index": True},
"available": {"type": "boolean"}
}
}
}
}
rtn = self.es.indices.create(index=index, body=mappings, ignore=400)
return rtn
def insert_bulk(self, data_list):
"""
批量插入数据
:return:
"""
try:
success, _ = bulk(self.es, data_list, raise_on_error=True, request_timeout=300)
return success
except Exception, e:
print e
return False
if __name__ == '__main__':
es = ES()
# 创建索引
index = 'library'
print '---------'
es.create_index(index)
print '---------'
# 初始数据
data_list = [{
"_index": "library",
"_type": "book",
"_id": "1",
"title": "All Quiet on the Western Front",
"otitle": "Im Westen nichts Neues",
"author": "Erich Maria Remarque",
"year": 1929,
"characters": ["Paul Bäumer", "Albert Kropp", "Haie Westhus", "Fredrich Müller", "Stanislaus Katczinsky",
"Tjaden"
],
"tags": ["novel"],
"copies": 1,
"available": True,
"section": 3
}, {
"_index": "library",
"_type": "book",
"_id": "2",
"title": "Catch-22",
"author": "Joseph Heller",
"year": 1961,
"characters": ["John Yossarian", "Captain Aardvark",
"Chaplain Tappman", "Colonel Cathcart", "Doctor Daneeka"
],
"tags": ["novel"],
"copies": 6,
"available": False,
"section": 1
}, {
"_index": "library",
"_type": "book",
"_id": "3",
"title": "The Complete Sherlock Holmes",
"author": "Arthur Conan Doyle",
"year": 1936,
"characters": ["Sherlock Holmes", "Dr. Watson", "G. Lestrade"],
"tags": [],
"copies": 0,
"available": False,
"section": 12
}, {
"_index": "library",
"_type": "book",
"_id": "4",
"title": "Crime and Punishment",
"otitle": "Преступлéние инаказáние",
"author": "Fyodor Dostoevsky",
"year": 1886,
"characters": ["Raskolnikov", "Sofia Semyonovna Marmeladova"],
"tags": [],
"copies": 0,
"available": True
}]
# 批量插入数据
es.insert_bulk(data_list)
print '---------'