连接:
import elasticsearch
from elasticsearch import Elasticsearch
es = Elasticsearch(
hosts=es_host,
http_auth=(username, passwd),
port=es_port,)
print(es.info()) # 这可以打印输出es的节点集群版本等信息
创建自定义索引:(可以只指定名称, 不定义mappings)
mappings = {
"mappings": {
"properties": {
"id": {
"type": "long",
"index": "false"
},
"serial": {
"type": "text", # keyword不会进行分词,text会分词
"index": "false" # 不建索引
},
# tags可以存json格式,访问tags.content
"tags": {
"type": "object",
"properties": {
"content": {"type": "keyword", "index": True},
"dominant_color_name": {"type": "keyword", "index": True},
"skill": {"type": "keyword", "index": True},
}
},
"hasTag": {
"type": "long",
"index": True
},
"status": {
"type": "long",
"index": True
},
"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"
}
}
}
}
es.indices.create(index = 'test_test0000000000000000000',body =mappings) # 创建索引,若不成功会报错
# 上传单条数据
action = {
"id": "1111122222",
"serial": "版本",
# 以下tags.content是错误的写法
# "tags.content" :"标签2",
# "tags.dominant_color_name": "域名的颜色黄色",
# 正确的写法如下:
"tags": {"content": "标签3", "dominant_color_name": "域名的颜色黄色"},
# 按照字典的格式写入,如果用上面的那种写法,会直接写成一个tags.content字段。
# 而不是在tags中content添加数据,这点需要注意
"tags.skill": "分类信息",
"hasTag": "123",
"status": "11",
"createTime": "2018-02-02",
"updateTime": "2018-02-03",
}
es.index(index="test_test0000000000000000000", doc_type="_doc", body=action)
# 上传多条
doc = [
{"index": {}},
{
"id": "1111122222",
"serial": "版本",
"tags.content": "标签2",
"tags.dominant_color_name": "域名的颜色黄色",
"tags.skill": "分类信息",
"hasTag": "123",
"status": "11",
"createTime": "2018-2-2",
"updateTime": "2018-2-3",
},
{"index": {}},
{
"id": "1111122222",
"serial": "版本",
"tags.content": "标签2",
"tags.dominant_color_name": "域名的颜色黄色",
"tags.skill": "分类信息",
"hasTag": "123",
"status": "11",
"createTime": "2018-2-2",
"updateTime": "2018-2-3",
},]
a = es.bulk(index='test_test0000000000000000000', doc_type='_doc', body=doc)
es.close() #记得关闭