首页 > 解决方案 > 将 Kibana 查询(创建索引模式)转换为 Python 请求

问题描述

我正在尝试将此 Kibana 查询转换为 Python:

PUT /.kibana/_doc/index-pattern:tempindex
{
  "type": "index-pattern",
  "index-pattern": {
    "title": "tempindex",
    "timeFieldName": "sendTime"
  }
}

这是我到目前为止所拥有的:

HEADERS = {
    'Content-Type': 'application/json'
}

uri = "http://localhost:5601/_doc/index-pattern:tempindex"

query = json.dumps({
  "type": "index-pattern",
  "index-pattern": {
    "title": "tempindex",
    "timeFieldName": "sendTime"
  }
})

r = requests.put(uri, headers=HEADERS, data=query).json()
print(r)

但它给了我

{'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

我究竟做错了什么?

PS:Elastic 和 Kibana 服务器都是本地的(Windows 10)。

标签: pythonelasticsearchkibanaelasticsearch-py

解决方案


似乎只是改变uri了诀窍:

uri = "http://localhost:9200/.kibana/_doc/index-pattern:tempindex"

但我不确定HEADERS, 因为lupanoide指出的kbn-xsrf: true应该存在,但无论哪种方式它似乎都在工作,显然结果是相同的(我还没有发现差异)。

编辑:正如文档所说:

kbn-xsrf: true

默认情况下,您必须kbn-xsrf对所有 API 调用使用,但以下情况除外:

API 端点使用GETorHEAD操作使用server.xsrf.whitelist设置
将路径列入白名单XSRF 保护使用设置 禁用
server.xsrf.disableProtection

所以我认为它应该存在。


推荐阅读