class ElasticsearchSampler(): """Elasticsearchサンプルクラス """ def __init__(self): host = 'localhost' port = 9200 auth = ('admin', 'admin') # certs = 'esnode.pem' # Elasticsearchインタンスの作成 self.es = OpenSearch( hosts=[{'host': host, 'port': port}], http_auth=auth, use_ssl=True, verify_certs=False, # ca_certs=certs, ssl_assert_hostname=False, ssl_show_warn=False, ) def __del__(self): self.es.close() print("close elasticsearch instance--------------------------") def search(self, idx: str, query: str): """検索 """ result = self.es.search(index=idx, body=query) print('--[search]-------------------------------------------') pprint.pprint(result, sort_dicts=False) def bulk(self, index: str): """バルクインサート """ try: # iterableなオブジェクトであればよいので以下どちらも可能 # - ジェネレータで渡す success, failed = helpers.bulk(self.es, gendata3(index)) # - list型で渡す # success, failed = helpers.bulk(self.es, bulklist()) # except opensearchpy.ElasticsearchException as e: # pprint.pprint(e) except Exception as e: pprint.pprint(e) return print('--[bulk ]-------------------------------------------') pprint.pprint(success) pprint.pprint(failed) def delete_by_query(self, idx: str, query: str): """条件指定の削除 """ result = self.es.delete_by_query(index=idx, body=query) print(f'{type(result)}') print('--[delete_by_query]----------------------------------') pprint.pprint(result, sort_dicts=False)
class ElasticsearchSampler(): """Elasticsearchサンプルクラス """ def __init__(self): host = 'localhost' port = 9200 auth = ('admin', 'admin') certs = 'cert/root-ca.pem' # Elasticsearchインタンスの作成 self.es = OpenSearch( hosts=[{ 'host': host, 'port': port }], http_auth=auth, use_ssl=True, verify_certs=True, ca_certs=certs, ssl_assert_hostname=False, ssl_show_warn=False, ) def __del__(self): # ElasticsearchインスタンスのCLOSE self.es.close() print("close elasticsearch instance--------------------------") def search(self, idx: str, query: str): result = self.es.search(index=idx, body=query) print(f'{type(result)}') print('--[search]-------------------------------------------') pprint.pprint(result, sort_dicts=False) def dslusage(self, index): # 検索部分(Searchオブジェクト) s = Search(using=self.es, index=index) s = s.filter( 'range', **{ '@timestamp': { 'gte': '2020-10-01T00:00:00+09:00', 'lte': '2020-10-01T23:59:59+09:00', 'format': 'date_time_no_millis' } }) s = s.extra(size=0) # 集計部分(Aggregationオブジェクト) aggs_port = A("terms", field="destination.port", size=20) # Aggregation オブジェクトを Search オブジェクトに紐付ける s.aggs.bucket("port-count", aggs_port) result = s.execute() # 結果抽出(Attrlist型) res_bucket = result.aggregations['port-count'].buckets print(f'==res_bucket : {res_bucket}') for item in res_bucket: print(f'port_count : {item}')