使用python库elasticsearch操作es,
1、单个写入
from elasticsearch import Elasticsearch es = Elasticsearch("192.168.0.11:9200") mappings = { "id": "11", "serial": "版本", "tags": {"comment": "标签3"}, "status": "tag1" } es.index(index="index-test", doc_type="doc_test", body=mappings)
2、批量写入
from elasticsearch import Elasticsearch from elasticsearch import helpers es = Elasticsearch("192.168.0.11:9200") data_list = [{ "_index": "index-test", "_type": "doc_test", "_source": { "id": "12", "serial": "版本", "tags": {"comment": "标签3"}, "status": "tag1" }, }, { "_index": "index-test", "_type": "doc_test", "_source": { "id": "13", "serial": "版本", "tags": {"comment": "标签3"}, "status": "tag1" } }] helpers.bulk(es, data_list, request_timeout=1000)
3、单批次读(数据量大读取易超时)
from elasticsearch import Elasticsearch def get_data_from_es(): es = Elasticsearch(['192.168.0.11:9200']) body = { "query": { "match": { "serial.keyword": "版本" } } } # 获取查询结果数量 res_count = es.count(index='index-test', doc_type='doc_type', body=body, request_timeout=10000) # print(res_count) total_size = res_count['count'] # print(total_size) query = es.search(index='index-test', doc_type='doc_type', body=body, request_timeout=10000, size=total_size) print(len(query['hits']['hits']))
4、循环读
from elasticsearch import Elasticsearch def get_data_from_es(): es = Elasticsearch(['192.168.0.11:9200']) body = { "query": { "match": { "serial.keyword": "版本" } } } # Initialize the scroll page = es.search(index=index_name, body=body, scroll='5m', size=10000, request_timeout=1000) # 第一页结果,为list类型 results = page['hits']['hits'] sid = page['_scroll_id'] scroll_size = page['hits']['total']['value'] while scroll_size > 0: # print("Scrolling...") page = es.scroll(scroll_id=sid, scroll='2m') # Update the scroll ID sid = page['_scroll_id'] # Get the number of results that we returned in the last scroll scroll_size = len(page['hits']['hits']) print("scroll size: " + str(scroll_size)) # 叠加查询结果 results += page['hits']['hits'] return results
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。