|
@@ -0,0 +1,90 @@
|
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
|
+"""
|
|
|
|
+Created on Fri Apr 16 14:54:31 2021
|
|
|
|
+
|
|
|
|
+@author: kane
|
|
|
|
+"""
|
|
|
|
+import argparse
|
|
|
|
+
|
|
|
|
+from elasticsearch import Elasticsearch
|
|
|
|
+
|
|
|
|
+# http://192.168.20.72:9200/
|
|
|
|
+# 链接es服务
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+host = '192.168.20.69:30920'
|
|
|
|
+es = Elasticsearch([host])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def create(index, body=None):
|
|
|
|
+ """
|
|
|
|
+ 创建索引
|
|
|
|
+ :param body:
|
|
|
|
+ :param index: 索引名称
|
|
|
|
+ :return: {'acknowledged': True, 'shards_acknowledged': True, 'index': 'student1'}
|
|
|
|
+ """
|
|
|
|
+ if es.indices.exists(index=index):
|
|
|
|
+ es.indices.delete(index=index) # 删除索引
|
|
|
|
+ res = es.indices.create(index=index, body=body)
|
|
|
|
+ return res
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def delete(index):
|
|
|
|
+ """
|
|
|
|
+ 删除索引
|
|
|
|
+ :param index: 索引名称
|
|
|
|
+ :return: True 或 False
|
|
|
|
+ """
|
|
|
|
+ if not es.indices.exists(index):
|
|
|
|
+ return False
|
|
|
|
+ else:
|
|
|
|
+ res = es.indices.delete(index=index)
|
|
|
|
+ return res['acknowledged']
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def add(index, body, id=None):
|
|
|
|
+ """
|
|
|
|
+ (单条数据添加或更新)添加或更新文档记录,更新文档时传对应的id即可
|
|
|
|
+ 使用方法:
|
|
|
|
+ `
|
|
|
|
+ body = {"name": "long", "age": 11,"height": 111}
|
|
|
|
+ add(index=index_name,body=body)
|
|
|
|
+ 或
|
|
|
|
+ body = {"name": "long", "age": 11,"height": 111}
|
|
|
|
+ add(index=index_name,body=body,id=1)
|
|
|
|
+ `
|
|
|
|
+ :param index: 索引名称
|
|
|
|
+ :param body:文档内容
|
|
|
|
+ :param id: 是否指定id,如不指定就会使用生成的字符串
|
|
|
|
+ :return:{'_index': 'student1', '_type': '_doc', '_id': 'nuwKDXIBujABphC4rbcq', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
|
|
|
|
+ """
|
|
|
|
+ res = es.index(index=index, body=body, id=id)
|
|
|
|
+ return res['_id'] # 返回 id
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def search(index=None):
|
|
|
|
+ """
|
|
|
|
+ 查询记录:如果没有索引名称的话默认就会查询全部的索引信息
|
|
|
|
+ :param index:查询的索引名称
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ if not index:
|
|
|
|
+ return es.search()
|
|
|
|
+ else:
|
|
|
|
+ return es.search(index=index)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def main(args):
|
|
|
|
+ with open(args.index_file) as index_file:
|
|
|
|
+ source = index_file.read().strip()
|
|
|
|
+ print(source)
|
|
|
|
+ create(index=args.index_name, body=source)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
+ # parser = argparse.ArgumentParser(description='Creating elasticsearch index.')
|
|
|
|
+ # parser.add_argument('--index_file', default='index.json', help='Elasticsearch index file.')
|
|
|
|
+ # parser.add_argument('--index_name', default='fault_meter', help='Elasticsearch index name.')
|
|
|
|
+ # args = parser.parse_args()
|
|
|
|
+ # main(args)
|
|
|
|
+ delete('fault_meter')
|