|
1 |
| -import re, time |
| 1 | +import re, time, copy |
2 | 2 | from elasticsearch import Elasticsearch
|
3 | 3 | from enum import Enum
|
4 | 4 |
|
@@ -73,15 +73,26 @@ def find_item(items, check):
|
73 | 73 | return False
|
74 | 74 |
|
75 | 75 | @staticmethod
|
76 |
| - def api_index(es: Elasticsearch, doc_index, doc_type, doc_id, doc_body): |
| 76 | + def api_index(es: Elasticsearch, doc_index, doc_type, doc_id, doc_body, legacy_es=False): |
77 | 77 | # Kibana v8 requires _type to be in body in order to have doc_type defined
|
78 |
| - doc_body['_type'] = '_doc' |
79 |
| - return es.index( |
80 |
| - index=doc_index, |
81 |
| - doc_type=doc_type, |
82 |
| - id=doc_id, |
83 |
| - body=doc_body |
84 |
| - ) |
| 78 | + # while Kibana v7 requires _type not to be in body |
| 79 | + if legacy_es: |
| 80 | + legacy_doc_body = copy.deepcopy(doc_body) |
| 81 | + del legacy_doc_body['_type'] |
| 82 | + return es.index( |
| 83 | + index=doc_index, |
| 84 | + doc_type=doc_type, |
| 85 | + id=doc_id, |
| 86 | + body=legacy_doc_body |
| 87 | + ) |
| 88 | + else: |
| 89 | + doc_body['_type'] = '_doc' |
| 90 | + return es.index( |
| 91 | + index=doc_index, |
| 92 | + doc_type=doc_type, |
| 93 | + id=doc_id, |
| 94 | + body=doc_body |
| 95 | + ) |
85 | 96 |
|
86 | 97 | def __init__(self, es_host, es_user, es_password, index, token, retry=None):
|
87 | 98 | self.es_instance = self.init(es_host, es_user, es_password, retry)
|
@@ -120,8 +131,8 @@ def create(self, doc_type, doc_id, doc_body):
|
120 | 131 | else:
|
121 | 132 | print("%sINFO: Asset \"%s\" created. - %s" % (self.Colors.OKGREEN, doc_body['name'], doc_body['download_link']))
|
122 | 133 |
|
123 |
| - def update(self, doc_type, doc_id, doc_body): |
124 |
| - response = self.api_index(self.es_instance, self.index, doc_type, doc_id, doc_body) |
| 134 | + def update(self, doc_type, doc_id, doc_body, legacy_es=False): |
| 135 | + response = self.api_index(self.es_instance, self.index, doc_type, doc_id, doc_body, legacy_es) |
125 | 136 | if doc_type:
|
126 | 137 | if response['created'] and 'created' == response['result']:
|
127 | 138 | print("%sWARNING: Asset \"%s\" created instead of updating. - %s" % (self.Colors.WARNING, doc_body['name'], doc_body['download_link']))
|
|
0 commit comments