Python,ElasticSearch/Kibana 'kibanaSavedObjectMeta'可视化元数据索引替换

jexiocij  于 2023-01-08  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)

祝你有愉快的一天,我希望任何人都能帮助我找到一个解决这个问题的方法。这将是非常感谢的。我正在寻找一种方法来取代或更新一个弹性可视化元数据索引,我试图完成的是自动化与Python和更新每个可视化元数据索引。每个索引分别有一个特定数量的索引,对于每个可视化,我都需要更改这些特定索引的数量,例如,我可能遇到具有4或6个索引的可视化,我需要使用脚本自动替换或更新新索引,下面是我的脚本

from elasticsearch_dsl import Search
from elasticsearch import Elasticsearch
from common import handle_elasticsearch, finish_line, get_accounts, get_time
import creds
from elasticsearch_dsl import Q
import requests
from elasticsearch import helpers
import os, sys, json, unicodedata
import json


elastic_url = creds.elastic_url
elastic_auth = creds.elastic_http_auth
# def meta(loreal_instagram_contenido_bar):
es = handle_elasticsearch(elastic_url, elastic_auth)

def updater(es, visualization_name):
    
    query = {
     "query": {
       "bool": {
         "filter": [
           {
             "term": {
               "visualization.title": visualization_name
             }
           }
         ]
       }
     }
    }
    raw_recolectados = es.search(index=".kibana", body=query, size=1)
    data = json.dumps(raw_recolectados)
    data = json.loads(data)

 data = data.replace('indexname','newindexname')
    return data
    
recolectados = updater(es, "visualization_bar_chart")
print (recolectados)

我创建了一个函数,用于搜索可视化,将可视化名称作为参数传递,如果您可以看到JSON有两个索引“键”,正如我在上面所述,我可能会遇到两个以上的索引,我需要替换所有索引,以下响应只是所有JSON响应的一部分。在我的控制台

'kibanaSavedObjectMeta':{  
                     'searchSourceJSON':'{\n  "index": "index_name_to_replace",\n  "query": {\n    "query": {\n      "match_all": {}\n    },\n    "language": "lucene"\n  },\n  "filter": [\n    {\n      "meta": {\n        "index": "index_name_to_replace",\n        "negate": false,\n        "disabled": false,\n        "alias": null,\n        "type": "phrase",\n        "key": "social_network.keyword",\n        "value": "ig",\n        "params": {\n          "query": "ig",\n          "type": "phrase"\n        }\n      },\n      "query": {\n        "match": {\n          "social_network.keyword": {\n
        "query": "ig",\n            "type": "phrase"\n          }\n        }\n      },\n      "$state": {\n        "store": "appState"\n
  }\n    }\n  ]\n}'
                  }
               }
            }
         }
      ]
   }
}

但是当我尝试实现replace方法时,我得到了:

file "updater.py", line 37, in updater
    data = data.replace('indexname', 'indexname')
AttributeError: 'dict' object has no attribute 'replace'

如果有人对这种类型的问题/解决方案有快捷的解决方案,我将非常非常感激。

nqwrtyyt

nqwrtyyt1#

大家好,遇到这个问题的人我刚刚找到了一个简单的解决方案来修改元数据,但您需要摄取回Kibana(ES)

text = data['hits']['hits'][0]['_source']['visualization']['kibanaSavedObjectMeta']['searchSourceJSON'].replace('index_of_visualization', 'new_index_name')
data['hits']['hits'][0]['_source']['visualization']['kibanaSavedObjectMeta']['searchSourceJSON'] = text

相关问题