from elasticsearch import Elasticsearch
from elasticsearch import helpers
import xlrd
# es连接
es = Elasticsearch(["10.202.42.1:9200","10.202.40.2:9200"],http_auth=('账号', '密码'), timeout=60)
# es查询:查询某索引下的所有文档
body = {'query': {'match_all': {}}}
q1 = es.search(index="wml_test01", body=body)
print(q1)
# 新建索引(常用类型字段) 并批量添加数据
# mappings = {
# "aliases": {
# "wml_test1": {}
# },
# "mappings": {
# "dynamic": "false",
#
# "properties": {
# "my_text": {
# "type": "text"
# },
# "my_keyword": {
# "type": "keyword"
# },
# "my_long": {
# "type": "long"
# },
# "my_integer": {
# "type": "integer"
# },
# "my_short": {
# "type": "short"
# },
# "my_byte": {
# "type": "byte"
# },
# "my_double": {
# "type": "double"
# },
# "my_float": {
# "type": "float"
# },
# "my_half_float": {
# "type": "half_float"
# },
# "my_boolean": {
# "type": "boolean"
# },
# "my_date": {
# "type": "date"
# },
# "my_integer_range": {
# "type": "integer_range"
# },
# "my_float_range": {
# "type": "float_range"
# },
# "my_long_range": {
# "type": "long_range"
# },
# "my_double_range": {
# "type": "double_range"
# },
# "my_date_range": {
# "type": "date_range"
# },
# "my_object": {
# "type": "object",
# "properties": {
# "prop_text": {
# "type": "text"
# },
# "long": {
# "type": "long"
# },
# "boolean": {
# "type": "boolean"
# },
# "date": {
# "type": "date"
# }
# }
# },
# "createTime": {
# "type": "date",
# "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
# },
# "updateTime": {
# "type": "date",
# "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
# }
# }
# }
# }
# res = es.indices.create(index = 'wml_test_1w',body =mappings)
# 批量构造数据
dataList =[]
for i in range(1,10001):
data = {
"_index": "wml_test_1w",
"_type": "_doc",
"_id": i,
"_source":{
"my_text": "my_text"+str(i),
"my_keyword": "my_keyword"+str(i),
"my_long": i,
"my_integer": i,
"my_short": 123,
"my_byte": 1,
"my_double": 1234567890.123456789,
"my_float": 1234567890.123456789,
"my_half_float": 1234.12,
"my_boolean": False,
"my_date": "2015-10-30",
"my_integer_range": {
"gte": 1,
"lte": 10
},
"my_float_range": {
"gte": 10.123,
"lte": 20.789
},
"my_long_range": {
"gte": 123456789,
"lte": 987654321
},
"my_double_range": {
"gte": 123456789.123456,
"lte": 987654321.789
},
"my_date_range": {
"gte": "2001-01-01",
"lte": "2011-01-01"
},
"my_object": [{
"prop_text": "prop_text1",
"long": i,
"boolean": True,
"date": "2015-11-01"
}],
"createTime": "2015-11-01",
"updateTime": "2015-11-01"
}
}
dataList.append(data)
# 插入数据
helpers.bulk(es,actions=dataList)