首页 > 解决方案 > 如何在 Elasticsearch Kibana 中内置的 Python 脚本中成功编写和运行 SQL 查询?

问题描述

我一直在尝试在 ES Kibana 中运行的 python 脚本中实现 SQL 查询,但是当我运行它时,我遇到了错误。

以下查询在 Kibana 中工作

POST _opendistro/_sql?format=json
{
  "query": "select * from test_index where ['title_name.S'] like 'iron%' limit 10"
}

以下是我在 Python 脚本中使用的函数片段。

def sql(index):
    response = es.search(
        body ={
                "query":  "select * from " + index + " limit 10"
            }
        )
    return response

以下是我尝试运行脚本时遇到的错误。

在此处输入图像描述

是否有替代方法来解决或修复此问题?任何帮助将非常感激!

标签: pythonelasticsearchkibana

解决方案


assuming you're using the elasticsearch-py Python client, it seems you're using the search API, which issues GET requests to the _search endpoint of an index. What you want to send instead is a POST request to the _opendistro/_sql endpoint. You won't find an API for that in the Python client, but you can use e.g. the Python requests library to issue any HTTP request.


推荐阅读