

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon OpenSearch Service 中的非同步搜尋
<a name="asynchronous-search"></a>

透過 Amazon OpenSearch Service 的非同步搜尋，您可以提交在背景中執行的搜尋查詢、監控請求進度以及在稍後階段擷取結果。在搜尋完成之前，您可以在部分結果變為可用時進行擷取。搜尋完成之後，儲存結果以供日後擷取和分析。

非同步搜尋需要 OpenSearch 1.0 或更高版本，或 Elasticsearch 7.10 或更高版本。

本文件提供非同步搜尋的簡短概觀。它還討論使用非同步搜尋搭配受管 Amazon OpenSearch Service 網域而非開放原始碼 OpenSearch 叢集的限制。如需非同步搜尋的完整文件，包括可用的設定、許可和完整的 API 參考，請參閱 OpenSearch 文件中的[非同步搜尋](https://docs.opensearch.org/latest/search-plugins/async/index/)。

## 搜尋呼叫範例
<a name="asynchronous-search-sample"></a>

若要執行非同步搜尋，請使用下列格式將 HTTP 請求傳送至 `_plugins/_asynchronous_search`：

```
POST opensearch-domain/_plugins/_asynchronous_search
```

**注意**  
如果您使用的是 Elasticsearch 7.10 而不是 OpenSearch 版本，請在所有非同步搜尋請求中將 `_plugins` 替換為 `_opendistro`。

您可以指定下列非同步搜尋選項：


| 選項 | Description | 預設值 | 必要 | 
| --- | --- | --- | --- | 
| wait\$1for\$1completion\$1timeout |  指定您計劃等待結果的時間量。您可以看到在這個時間內得到的任何結果與一般搜尋一樣。您可以根據 ID 來輪詢剩餘結果。最高值為 300 秒。  | 1 秒 | 否 | 
| keep\$1on\$1completion |  指定搜尋完成後是否要將結果儲存在叢集中。您可以在稍後時間檢查儲存的結果。  | false | 否 | 
| keep\$1alive |  指定在叢集中儲存結果的時間長度。例如：`2d` 表示結果在叢集中儲存 48 小時。儲存的搜尋結果會在此時段之後或取消搜尋時刪除。請注意，這包括查詢執行時間。如果此次查詢超限，處理程序會自動取消此查詢。  | 12 小時 | 否 | 

**請求範例**

```
POST _plugins/_asynchronous_search/?pretty&size=10&wait_for_completion_timeout=1ms&keep_on_completion=true&request_cache=false
{
  "aggs": {
    "city": {
      "terms": {
        "field": "city",
        "size": 10
      }
    }
  }
}
```

**注意**  
支援適用於標準 `_search` 查詢的所有請求參數。如果您使用的是 Elasticsearch 7.10 而不是 OpenSearch 版本，請將 `_plugins` 替換為 `_opendistro`。

## 非同步搜尋許可
<a name="asynchronous-search-permissions"></a>

非同步搜尋支援[精細存取控制](fgac.md)。如需混合和匹配許可以符合您的使用案例的詳細資訊，請參閱[非同步搜尋安全性](https://docs.opensearch.org/latest/search-plugins/async/security/)。

對於已啟用精細存取控制的網域，您需要角色的下列最低許可：

```
# Allows users to use all asynchronous search functionality
asynchronous_search_full_access:
  reserved: true
  cluster_permissions:
    - 'cluster:admin/opensearch/asynchronous-search/*'
  index_permissions:
    - index_patterns:
        - '*'
      allowed_actions:
        - 'indices:data/read/search*'

# Allows users to read stored asynchronous search results
asynchronous_search_read_access:
  reserved: true
  cluster_permissions:
    - 'cluster:admin/opensearch/asynchronous-search/get'
```

對於已停用精細存取控制的網域，請使用您的 IAM 存取權和秘密金鑰來簽署所有請求。您可以使用非同步搜尋 ID 來存取結果。

## 非同步搜尋設定
<a name="asynchronous-search-diff"></a>

使用 `_cluster/settings` API，OpenSearch 可讓您變更所有可用的[非同步搜尋設定](https://docs.opensearch.org/latest/search-plugins/async/settings/)。在 OpenSearch Service 中，您只能變更下列設定：
+ `plugins.asynchronous_search.node_concurrent_running_searches`
+ `plugins.asynchronous_search.persist_search_failures`

## 跨叢集搜尋
<a name="asynchronous-search-ccs"></a>

您可以在具有下列次要限制的叢集間執行非同步搜尋：
+ 您只能在來源網域上執行非同步搜尋。
+ 您無法將網路往返次數降到最低，作為跨叢集搜尋查詢的一部分。

如果在具有連線別名 `cluster_b` 的 `domain-a -> domain-b` 與具有連線別名 `cluster_c` 的 `domain-a -> domain-c` 之間設定連線，請非同步搜尋 `domain-a`、`domain-b` 和 `domain-c`，如下所示：

```
POST https://src-domain.us-east-1.es.amazonaws.com/local_index,cluster_b:b_index,cluster_c:c_index/_plugins/_asynchronous_search/?pretty&size=10&wait_for_completion_timeout=500ms&keep_on_completion=true&request_cache=false 
{
  "size": 0,
  "_source": {
    "excludes": []
  },
  "aggs": {
    "2": {
      "terms": {
        "field": "clientip",
        "size": 50,
        "order": {
          "_count": "desc"
        }
      }
    }
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [
    "@timestamp"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "status:404",
            "analyze_wildcard": true,
            "default_field": "*"
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": 1483747200000,
              "lte": 1488326400000,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  }
}
```

**回應**

```
{
  "id" : "Fm9pYzJyVG91U19xb0hIQUJnMHJfRFEAAAAAAAknghQ1OWVBczNZQjVEa2dMYTBXaTdEagAAAAAAAAAB",
  "state" : "RUNNING",
  "start_time_in_millis" : 1609329314796,
  "expiration_time_in_millis" : 1609761314796
}
```

如需詳細資訊，請參閱[Amazon OpenSearch Service 中的跨叢集搜尋](cross-cluster-search.md)。

## UltraWarm
<a name="asynchronous-search-ultrawarm"></a>

使用 UltraWarm 索引的非同步搜尋繼續運作。如需詳細資訊，請參閱[Amazon OpenSearch Service 的 UltraWarm 儲存](ultrawarm.md)。

**注意**  
您可以在 CloudWatch 中監控非同步搜尋統計數字。如需指標的完整清單，請參閱 [非同步搜尋指標](managedomains-cloudwatchmetrics.md#managedomains-cloudwatchmetrics-asynchronous-search)。