

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

# 建立集合 (CLI)
<a name="serverless-create-cli"></a>

使用本節中的程序，使用 建立 OpenSearch Serverless 集合 AWS CLI。

**Topics**
+ [開始之前](#serverless-create-cli-before-you-begin)
+ [建立集合](#serverless-create-cli-creating)
+ [使用自動語意擴充索引建立集合](#serverless-create-cli-automatic-semantic-enrichment)

## 開始之前
<a name="serverless-create-cli-before-you-begin"></a>

使用 建立集合之前 AWS CLI，請使用下列程序來建立集合所需的政策。

**注意**  
在下列每個程序中，當您指定集合的名稱時，名稱必須符合下列條件：  
對您的帳戶和 是唯一的 AWS 區域
只能包含小寫字母 a-z、數字 0-9 和連字號 (-)
包含 3 到 32 個之間字元數

**建立集合的必要政策**

1. 開啟 AWS CLI 並執行下列命令，以建立資源模式符合集合預期名稱的[加密政策](serverless-encryption.md)。

   ```
   &aws opensearchserverless create-security-policy \
     --name policy name \
     --type encryption --policy "{\"Rules\":[{\"ResourceType\":\"collection\",\"Resource\":[\"collection\/collection name\"]}],\"AWSOwnedKey\":true}"
   ```

   例如，如果您計劃將集合命名為 *logs-application*，則可以建立如下所示的加密政策：

   ```
   &aws opensearchserverless create-security-policy \
     --name logs-policy \
     --type encryption --policy "{\"Rules\":[{\"ResourceType\":\"collection\",\"Resource\":[\"collection\/logs-application\"]}],\"AWSOwnedKey\":true}"
   ```

   如果您計劃將政策用於其他集合，則可以使規則的範圍更廣泛，例如 `collection/logs*` 或 `collection/*`。

1. 執行下列命令，使用網路[政策來設定集合的網路](serverless-network.md)設定。您可以在建立集合之後建立網路政策，但建議您事先執行此操作。

   ```
   &aws opensearchserverless create-security-policy \
     --name policy name \
     --type network --policy "[{\"Description\":\"description\",\"Rules\":[{\"ResourceType\":\"dashboard\",\"Resource\":[\"collection\/collection name\"]},{\"ResourceType\":\"collection\",\"Resource\":[\"collection\/collection name\"]}],\"AllowFromPublic\":true}]"
   ```

   使用先前的 *logs-application* 範例，您可建立下列網路政策：

   ```
   &aws opensearchserverless create-security-policy \
     --name logs-policy \
     --type network --policy "[{\"Description\":\"Public access for logs collection\",\"Rules\":[{\"ResourceType\":\"dashboard\",\"Resource\":[\"collection\/logs-application\"]},{\"ResourceType\":\"collection\",\"Resource\":[\"collection\/logs-application\"]}],\"AllowFromPublic\":true}]"
   ```

## 建立集合
<a name="serverless-create-cli-creating"></a>

下列程序使用 [CreateCollection](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_CreateCollection.html) API 動作來建立類型 `SEARCH`或 的集合`TIMESERIES`。如果您未在請求中指定集合類型，即會預設為 `TIMESERIES`。如需這些類型的詳細資訊，請參閱 [選擇集合類型](serverless-overview.md#serverless-usecase)。若要建立*向量搜尋*集合，請參閱 [使用向量搜尋集合](serverless-vector-search.md)。

如果您的集合使用 加密 AWS 擁有的金鑰，則 `kmsKeyArn``auto`不是 ARN。

**重要**  
建立集合後，除非該集合符合資料存取政策，否則您將無法存取它。如需詳細資訊，請參閱[Amazon OpenSearch Serverless 的資料存取控制](serverless-data-access.md)。

**建立集合**

1. 確認您已建立 中所述的必要政策[開始之前](#serverless-create-cli-before-you-begin)。

1. 執行下列命令。對於 ，`type`請指定 `SEARCH`或 `TIMESERIES`。

   ```
   &aws opensearchserverless create-collection --name "collection name" --type collection type --description "description"
   ```

## 使用自動語意擴充索引建立集合
<a name="serverless-create-cli-automatic-semantic-enrichment"></a>

使用下列程序，使用為[自動語意擴充](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-semantic-enrichment.html)設定的索引建立新的 OpenSearch Serverless 集合。程序使用 OpenSearch Serverless [CreateIndex](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_CreateIndex.html) API 動作。

**使用為自動語意擴充設定的索引建立新的集合**

執行下列命令來建立集合和索引。

```
&aws opensearchserverless create-index \
--region Region ID \
--id collection name --index-name index name \
--index-schema \
'mapping in json'
```

範例如下。

```
&aws opensearchserverless create-index \
--region us-east-1 \
--id conversation_history --index-name conversation_history_index \
--index-schema \ 
'{
    "mappings": {
        "properties": {
            "age": {
                "type": "integer"
            },
            "name": {
                "type": "keyword"
            },
            "user_description": {
                "type": "text"
            },
            "conversation_history": {
                "type": "text",
                "semantic_enrichment": {
                    "status": "ENABLED",
                    // Specifies the sparse tokenizer for processing multi-lingual text
                    "language_option": "MULTI-LINGUAL", 
                    // If embedding_field is provided, the semantic embedding field will be set to the given name rather than original field name + "_embedding"
                    "embedding_field": "conversation_history_user_defined" 
                }
            },
            "book_title": {
                "type": "text",
                "semantic_enrichment": {
                    // No embedding_field is provided, so the semantic embedding field is set to "book_title_embedding"
                    "status": "ENABLED",
                    "language_option": "ENGLISH"
                }
            },
            "abstract": {
                "type": "text",
                "semantic_enrichment": {
                    // If no language_option is provided, it will be set to English.
                    // No embedding_field is provided, so the semantic embedding field is set to "abstract_embedding"
                    "status": "ENABLED" 
                }
            }
        }
    }
}'
```