查询es中grade索引的数据量:
新建一个索引,将另外一个索引中的数据迁移到新索引中,并添加新的字段。
示例代码:
from elasticsearch import Elasticsearch
import time
es = Elasticsearch(hosts='http://192.168.124.49:9200')
print(es)
# 新建新的索引mapping,新增加了source字段
mapping = {
"mappings": {
"properties": {
"age": {
"type": "long"
},
"character": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"create_time": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"grade": {
"type": "long"
},
"id": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"subject": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"source": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
}
}
}
# 新建索引
new_index = es.indices.create(index='new_grade', body=mapping)
print(new_index)
# old索引中查询数据
query = {
"query": {
"match_all": {}
}
}
res = es.search(index='grade', body=query, size=10000)
# print(res)
print(res['hits']['hits'])
start = time.time()
# 向新索引中添加数据
count = 0
for hit in res['hits']['hits']:
data = hit["_source"]
data.update({"source": "Grade index"})
es.index(index="new_grade", id=data['id'], document=data)
print(count)
count += 1
print("耗时:", time.time() - start)
运行结果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142778.html