SQL 转换 API

编辑

SQL 转换 API 接收 JSON 文档中的 SQL,并将其转换为原生的 Elasticsearch 查询。 例如:

resp = client.sql.translate(
    query="SELECT * FROM library ORDER BY page_count DESC",
    fetch_size=10,
)
print(resp)
response = client.sql.translate(
  body: {
    query: 'SELECT * FROM library ORDER BY page_count DESC',
    fetch_size: 10
  }
)
puts response
const response = await client.sql.translate({
  query: "SELECT * FROM library ORDER BY page_count DESC",
  fetch_size: 10,
});
console.log(response);
POST /_sql/translate
{
  "query": "SELECT * FROM library ORDER BY page_count DESC",
  "fetch_size": 10
}

返回结果:

{
  "size": 10,
  "_source": false,
  "fields": [
    {
      "field": "author"
    },
    {
      "field": "name"
    },
    {
      "field": "page_count"
    },
    {
      "field": "release_date",
      "format": "strict_date_optional_time_nanos"
    }
  ],
  "sort": [
    {
      "page_count": {
        "order": "desc",
        "missing": "_first",
        "unmapped_type": "short"
      }
    }
  ],
  "track_total_hits": -1
}

这是 SQL 将运行以提供结果的请求。 在这种情况下,SQL 将使用 scroll API。 如果结果包含聚合,则 SQL 将使用正常的 search API

请求体接受与 SQL 搜索 API 相同的参数,但不包括 cursor