

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

# 在 Amazon OpenSearch Service 中建立資料索引
<a name="indexing"></a>

由於 Amazon OpenSearch Service 使用 REST API，因此有不少方法都適用於為文件建立索引。您可以使用標準用戶端，例如 [ Curl ](https://curl.haxx.se/) 或任何可以傳送 HTTP 請求的程式設計語言。為了進一步簡化互動程序，OpenSearch Service 擁有許多程式設計語言的用戶端。進階使用者可以直接跳到[將資料串流載入至 Amazon OpenSearch Service](integrations.md)。

我們強烈建議您使用 Amazon OpenSearch Ingestion 擷取資料，這是在 OpenSearch Service 中建置的全受管資料收集器。如需詳細資訊，請參閱 [Amazon OpenSearch Ingestion](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ingestion.html)。

如需索引的簡介，請參閱 [OpenSearch 文件](https://docs.opensearch.org/latest/opensearch/index-data/)。

## 索引的命名限制
<a name="indexing-naming"></a>

OpenSearch Service 索引具有以下命名限制：
+ 所有字母必須小寫。
+ 索引名稱的最開頭不可以是 `_` 或 `-`。
+ 索引名稱不可以包含空格、逗號、`:`、`"`、`*`、`+`、`/`、`\`、`|`、`?`、`#`、`>` 或 `<`。

請勿在索引、類型和文件 ID 名稱中包含敏感資訊。OpenSearch Service 會在其統一資源識別符 (URI) 中使用這些名稱。伺服器和應用程式通常會記錄 HTTP 請求，如果 URI 包含敏感資訊，這會導致不必要的資料暴露：

```
2018-10-03T23:39:43 198.51.100.14 200 "GET https://{{opensearch-domain}}/dr-jane-doe/flu-patients-2018/202-555-0100/ HTTP/1.1"
```

即使您沒有檢視相關 JSON 文件的[許可](ac.md)，您可以從此仿造日誌列推斷出 Doe 醫生的其中一名病人 (其電話號碼為 202-555-0100) 在 2018 年患有流感。

如果 OpenSearch Service 在索引名稱中偵測到真實或存在的 IP 地址 (例如，`my-index-12.34.56.78.91`)，則會遮罩 IP 地址。呼叫 `_cat/indices` 產生下列回應︰

```
green open my-index-x.x.x.x.91    soY19tBERoKo71WcEScidw 5 1 0 0   2kb  1kb
```

為防止不必要的混淆，請避免在索引名稱中包含 IP 地址。

## 縮減回應大小
<a name="indexing-size"></a>

由 `_index` 和 `_bulk` API 而來的回應包含相當多的資訊。這類資訊或許有助於對請求進行故障診斷或實作重試邏輯，但難免會耗用大量頻寬。在此範例中，對 32 個位元組的文件編製索引將產生 339 個位元組的回應 (包括標頭)：

```
PUT {{opensearch-domain}}/more-movies/_doc/1
{"title": "Back to the Future"}
```

**回應**

```
{
  "_index": "more-movies",
  "_type": "_doc",
  "_id": "1",
  "_version": 4,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}
```

此回應大小可能看起來很小，但是如果您每天索引 1,000,000 份文件 (大約每秒 11.5 份文件)，則每個回應 339 位元組的運作量達到每月 10.17 GB 的下載流量。

如果您擔心資料傳輸費用，請使用 `filter_path` 參數縮減 OpenSearch Service 回應的大小，但應留意切勿篩除需要用於識別或重試失敗請求的欄位。這類欄位因用戶端而異。`filter_path` 參數適用於所有 OpenSearch Service REST API，但對您經常呼叫的 API 尤其實用，例如 `_index` 和 `_bulk` API：

```
PUT {{opensearch-domain}}/more-movies/_doc/1?filter_path=result,_shards.total
{"title": "Back to the Future"}
```

**回應**

```
{
  "result": "updated",
  "_shards": {
    "total": 2
  }
}
```

除了納入欄位，您還可以使用 `-` 字首排除欄位。`filter_path` 也支援萬用字元：

```
POST {{opensearch-domain}}/_bulk?filter_path=-took,-items.index._*
{ "index": { "_index": "more-movies", "_id": "1" } }
{"title": "Back to the Future"}
{ "index": { "_index": "more-movies", "_id": "2" } }
{"title": "Spirited Away"}
```

**回應**

```
{
  "errors": false,
  "items": [
    {
      "index": {
        "result": "updated",
        "status": 200
      }
    },
    {
      "index": {
        "result": "updated",
        "status": 200
      }
    }
  ]
}
```

## 索引轉碼器
<a name="indexing-codecs"></a>

索引轉碼器會決定如何壓縮索引上的存放欄位並存放在磁碟上。索引轉碼器是由靜態`index.codec`設定所控制，其會指定壓縮演算法。此設定會影響索引碎片大小和操作效能。

如需支援的轉碼器及其效能特性的清單，請參閱 OpenSearch 文件中的[支援的轉碼器](https://opensearch.org/docs/latest/im-plugin/index-codecs/#supported-codecs)。

當您選擇索引轉碼器時，請考慮下列事項：
+ 為了避免變更現有索引的編解碼器設定的挑戰，請在非生產環境中測試代表性工作負載，然後再使用新的編解碼器設定。如需詳細資訊，請參閱[變更索引轉碼器](https://opensearch.org/docs/latest/im-plugin/index-codecs/#changing-an-index-codec)。
+ 您無法將 [Zstandard 壓縮轉碼器](https://opensearch.org/docs/latest/im-plugin/index-codecs/) (`"index.codec": "zstd"` 或 `"index.codec": "zstd_no_dict"`) 用於 [k-NN](https://opensearch.org/docs/latest/search-plugins/knn/index/) 或 [Security Analytics](https://opensearch.org/docs/latest/security-analytics/index/) 索引。