

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# コレクションを作成する (CLI)
<a name="serverless-create-cli"></a>

このセクションの手順では、 AWS CLIを使用して OpenSearch Serverless コレクションを作成します。

**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`は ARN `auto`ではなく です。

**重要**  
コレクション作成後は、データアクセスポリシーと一致しない限りアクセスできません。詳細については、「[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" 
                }
            }
        }
    }
}'
```