运行异步 SQL 搜索

编辑

默认情况下,SQL 搜索是同步的。它们会在返回响应之前等待完整的搜索结果。但是,对于大型数据集或冻结数据的搜索,结果可能需要更长时间。

为了避免长时间等待,请运行异步 SQL 搜索。将wait_for_completion_timeout设置为希望等待同步结果的持续时间。

resp = client.sql.query(
    format="json",
    wait_for_completion_timeout="2s",
    query="SELECT * FROM library ORDER BY page_count DESC",
    fetch_size=5,
)
print(resp)
response = client.sql.query(
  format: 'json',
  body: {
    wait_for_completion_timeout: '2s',
    query: 'SELECT * FROM library ORDER BY page_count DESC',
    fetch_size: 5
  }
)
puts response
const response = await client.sql.query({
  format: "json",
  wait_for_completion_timeout: "2s",
  query: "SELECT * FROM library ORDER BY page_count DESC",
  fetch_size: 5,
});
console.log(response);
POST _sql?format=json
{
  "wait_for_completion_timeout": "2s",
  "query": "SELECT * FROM library ORDER BY page_count DESC",
  "fetch_size": 5
}

如果搜索在此期间内未完成,则搜索将变为异步。API 将返回:

  • 搜索的id
  • is_partial值为true,表示搜索结果不完整。
  • is_running值为true,表示搜索仍在后台运行。

对于 CSV、TSV 和 TXT 响应,API 将这些值分别返回在Async-IDAsync-partialAsync-running HTTP 头中。

{
  "id": "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
  "is_partial": true,
  "is_running": true,
  "rows": [ ]
}

要检查异步搜索的进度,请使用获取异步 SQL 搜索状态 API和搜索 ID。

resp = client.sql.get_async_status(
    id="FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
)
print(resp)
response = client.sql.get_async_status(
  id: 'FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU='
)
puts response
const response = await client.sql.getAsyncStatus({
  id: "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
});
console.log(response);
GET _sql/async/status/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=

如果is_runningis_partial都为false,则异步搜索已完成并具有完整结果。

{
  "id": "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
  "is_running": false,
  "is_partial": false,
  "expiration_time_in_millis": 1611690295000,
  "completion_status": 200
}

要获取结果,请使用获取异步 SQL 搜索 API和搜索 ID。如果搜索仍在运行,请使用wait_for_completion_timeout指定希望等待的时间。您还可以指定响应format

resp = client.sql.get_async(
    id="FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
    wait_for_completion_timeout="2s",
    format="json",
)
print(resp)
response = client.sql.get_async(
  id: 'FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=',
  wait_for_completion_timeout: '2s',
  format: 'json'
)
puts response
const response = await client.sql.getAsync({
  id: "FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=",
  wait_for_completion_timeout: "2s",
  format: "json",
});
console.log(response);
GET _sql/async/FnR0TDhyWUVmUmVtWXRWZER4MXZiNFEad2F5UDk2ZVdTVHV1S0xDUy00SklUdzozMTU=?wait_for_completion_timeout=2s&format=json

更改搜索保留期限

编辑

默认情况下,Elasticsearch 会存储异步 SQL 搜索五天。在此期限之后,Elasticsearch 会删除搜索及其结果,即使搜索仍在运行。

resp = client.sql.query(
    format="json",
    keep_alive="2d",
    wait_for_completion_timeout="2s",
    query="SELECT * FROM library ORDER BY page_count DESC",
    fetch_size=5,
)
print(resp)
response = client.sql.query(
  format: 'json',
  body: {
    keep_alive: '2d',
    wait_for_completion_timeout: '2s',
    query: 'SELECT * FROM library ORDER BY page_count DESC',
    fetch_size: 5
  }
)
puts response
const response = await client.sql.query({
  format: "json",
  keep_alive: "2d",
  wait_for_completion_timeout: "2s",
  query: "SELECT * FROM library ORDER BY page_count DESC",
  fetch_size: 5,
});
console.log(response);
POST _sql?format=json
{
  "keep_alive": "2d",
  "wait_for_completion_timeout": "2s",
  "query": "SELECT * FROM library ORDER BY page_count DESC",
  "fetch_size": 5
}

您可以使用获取异步 SQL 搜索 API 的keep_alive参数来更改此保留期限。新的期限从请求运行后开始。

resp = client.sql.get_async(
    id="FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=",
    keep_alive="5d",
    wait_for_completion_timeout="2s",
    format="json",
)
print(resp)
response = client.sql.get_async(
  id: 'FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=',
  keep_alive: '5d',
  wait_for_completion_timeout: '2s',
  format: 'json'
)
puts response
const response = await client.sql.getAsync({
  id: "FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=",
  keep_alive: "5d",
  wait_for_completion_timeout: "2s",
  format: "json",
});
console.log(response);
GET _sql/async/FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=?keep_alive=5d&wait_for_completion_timeout=2s&format=json

使用删除异步 SQL 搜索 API删除keep_alive期限结束前的异步搜索。如果搜索仍在运行,Elasticsearch 将取消它。

resp = client.sql.delete_async(
    id="FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=",
)
print(resp)
response = client.sql.delete_async(
  id: 'FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI='
)
puts response
const response = await client.sql.deleteAsync({
  id: "FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=",
});
console.log(response);
DELETE _sql/async/delete/FmdMX2pIang3UWhLRU5QS0lqdlppYncaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQToxOTI=

存储同步 SQL 搜索

编辑

默认情况下,Elasticsearch 只存储异步 SQL 搜索。要保存同步搜索,请指定wait_for_completion_timeout并将keep_on_completion设置为true

resp = client.sql.query(
    format="json",
    keep_on_completion=True,
    wait_for_completion_timeout="2s",
    query="SELECT * FROM library ORDER BY page_count DESC",
    fetch_size=5,
)
print(resp)
response = client.sql.query(
  format: 'json',
  body: {
    keep_on_completion: true,
    wait_for_completion_timeout: '2s',
    query: 'SELECT * FROM library ORDER BY page_count DESC',
    fetch_size: 5
  }
)
puts response
const response = await client.sql.query({
  format: "json",
  keep_on_completion: true,
  wait_for_completion_timeout: "2s",
  query: "SELECT * FROM library ORDER BY page_count DESC",
  fetch_size: 5,
});
console.log(response);
POST _sql?format=json
{
  "keep_on_completion": true,
  "wait_for_completion_timeout": "2s",
  "query": "SELECT * FROM library ORDER BY page_count DESC",
  "fetch_size": 5
}

如果is_partialis_running都为false,则搜索是同步的并返回了完整的结果。

{
  "id": "Fnc5UllQdUVWU0NxRFNMbWxNYXplaFEaMUpYQ05oSkpTc3kwZ21EdC1tbFJXQTo0NzA=",
  "is_partial": false,
  "is_running": false,
  "rows": ...,
  "columns": ...,
  "cursor": ...
}

您可以稍后使用搜索 ID 和获取异步 SQL 搜索 API获取相同的结果。

保存的同步搜索仍然受keep_alive保留期限的约束。当此期限结束时,Elasticsearch 将删除搜索结果。您也可以使用删除异步 SQL 搜索 API删除保存的搜索。