

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建集合（CLI）
<a name="serverless-create-cli"></a>

使用本节中的过程使用创建 OpenSearch 无服务器集合。 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-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>

使用以下步骤创建新的 OpenSearch Serverless 集合，其索引已配置为[自动语义丰富](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-semantic-enrichment.html)。该过程使用 OpenSearch 无服务器 [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" 
                }
            }
        }
    }
}'
```