创建集合(CLI)
通过 Amazon CLI,使用本节中的步骤创建 OpenSearch 无服务器集合。
开始前的准备工作
使用 Amazon CLI 创建集合之前,按照以下步骤为该集合创建所需策略。
注意
在以下每个过程中,为集合指定名称时,该名称必须满足以下条件:
-
对于您的账户和 Amazon Web Services 区域而言是唯一的
-
只包含小写字母 a-z、数字 0–9 和连字符(-)
-
包含 3 到 32 个字符
为集合创建所需策略
-
打开 Amazon CLI 并运行以下命令,以创建加密策略,其资源模式将与集合的预期名称相匹配。
aws opensearchserverless create-security-policy \ --namepolicy 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/*。 -
运行以下命令,以使用网络策略为集合配置网络设置。您可以在创建集合后创建网络策略,但我们建议您事先创建网络策略。
aws opensearchserverless create-security-policy \ --namepolicy 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}]"
创建集合
以下步骤使用 CreateCollection API 操作,以创建类型为 SEARCH 或 TIMESERIES 的集合。如果您在该请求中未指定集合类型,则其默认为 TIMESERIES。有关这些类型的更多信息,请参阅 选择集合类型。要创建向量搜索集合,请参阅 使用向量搜索集合。
如果您的集合使用 Amazon 拥有的密钥进行加密,则 kmsKeyArn 将是 auto 而非 ARN。
重要
在创建集合后,除非该集合与某一数据访问策略相匹配,否则您将无法访问它。有关更多信息,请参阅 Amazon OpenSearch 无服务器的数据访问控制。
创建集合
-
验证是否已创建 开始前的准备工作 所述的必需策略。
-
运行以下命令。对于
type,指定SEARCH或TIMESERIES。aws opensearchserverless create-collection --name "collection name" --typecollection type--description "description"
使用自动语义增强索引创建集合
使用以下步骤创建新的 OpenSearch 无服务器集合,其索引已配置为自动语义增强。该过程使用 OpenSearch 无服务器 CreateIndex API 操作。
使用配置为自动语义增强的索引创建新集合
运行以下命令创建集合和索引。
aws opensearchserverless create-index \ --regionRegion ID\ --idcollection name--index-nameindex 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" } } } } }'