

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

# 入門チュートリアル
<a name="location-indexing-tutorial"></a>

このチュートリアルでは、[フリートのインデックス作成](iot-indexing.md)を使用して[位置データのインデックス作成](location-indexing-geoquery.md)方法を示します。簡単にするために、デバイスを表すモノを作成し、位置データを名前付きシャドウに保存し、位置インデックス作成のモノインデックス設定を更新し、サンプルジオクエリを実行して半径境界内のデバイスを検索します。

このチュートリアルの完了には 15 分ほどかかります。

**Topics**
+ [前提条件](#location-indexing-tutorial-prerequisites)
+ [モノとシャドウを作成する](#location-indexing-create-resources)
+ [モノのインデックス作成を設定する](#location-indexing-update-configuration)
+ [ジオクエリの実行](#location-indexing-run-geoquery)

## 前提条件
<a name="location-indexing-tutorial-prerequisites"></a>
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) の最新バージョンをインストールします。
+ [位置インデックス作成とジオクエリ](https://docs.aws.amazon.com/iot/latest/developerguide/location-indexing-geoquery.html)、[モノのインデックス作成の管理](https://docs.aws.amazon.com/iot/latest/developerguide/managing-index.html)、および[クエリ構文](https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html)を理解しておいてください。

## モノとシャドウを作成する
<a name="location-indexing-create-resources"></a>

デバイスを表すモノと、その位置データを保存する名前付きシャドウを作成します (座標 47.61564、-122.33584)。

1. 次のコマンドを実行して、Bike-1 という名前のバイクを表すモノを作成します。を使用してモノを作成する方法の詳細については AWS CLI、「 *AWS CLI**リファレンス*」の[「Create-thing](https://docs.aws.amazon.com//cli/latest/reference/iot/create-thing.html)」を参照してください。

   ```
   aws iot create-thing --thing-name "Bike-1" \
   --attribute-payload '{"attributes": {"model":"OEM-2302-12", "battery":"35", "acqDate":"06/09/23"}}'
   ```

   このコマンドの出力は以下のようになります。

   ```
   {
       "thingName": "Bike-1",
       "thingArn": "arn:aws:iot:us-east-1:123456789012:thing/Bike-1",
       "thingId": "df9cf01d-b0c8-48fe-a2e2-e16cff6b23df"
   }
   ```

1. 次のコマンドを実行して、Bike-1 の位置データ (座標 47.61564、-122.33584) を保存する名前付きシャドウを作成します。を使用して名前付きシャドウを作成する方法の詳細については AWS CLI、「 *AWS CLI**リファレンス*」の[update-thing-shadow](https://docs.aws.amazon.com//cli/latest/reference/iot-data/update-thing-shadow.html)」を参照してください。

   ```
   aws iot-data update-thing-shadow \
   --thing-name Bike-1 \
   --shadow-name Bike1-shadow \
   --cli-binary-format raw-in-base64-out \
   --payload '{"state":{"reported":{"coordinates":{"lat": 47.6153, "lon": -122.3333}}}}' \
   "output.txt" \
   ```

   このコマンドでは、出力が生成されません。作成した名前付きシャドウを表示するには、[list-named-shadows-for-thing](https://docs.aws.amazon.com//cli/latest/reference/iot-data/list-named-shadows-for-thing.html) CLI コマンドを実行します。

   ```
   aws iot-data list-named-shadows-for-thing --thing-name Bike-1
   ```

   このコマンドの出力は以下のようになります。

   ```
   {
       "results": [
           "Bike1-shadow"
       ],
       "timestamp": 1699574309
   }
   ```

## モノのインデックス作成を設定する
<a name="location-indexing-update-configuration"></a>

位置データのインデックスを作成するには、モノのインデックス作成設定を更新して、位置データを含める必要があります。このチュートリアルでは、位置情報データは名前付きシャドウに保存されるため、`thingIndexingMode` を `REGISTRY` (最小要件で) に設定し、`namedShadowIndexingMode` を `ON` に設定して、位置情報データを設定に追加します。この例では、名前付きシャドウの名前とシャドウの位置データパスを `filter` に追加する必要があります。

1. コマンドを実行して、位置インデックスの作成のインデックス作成設定を更新します。

   ```
   aws iot update-indexing-configuration --cli-input-json '{
   "thingIndexingConfiguration": { "thingIndexingMode": "REGISTRY",
   "thingConnectivityIndexingMode": "OFF",
   "deviceDefenderIndexingMode": "OFF",
   "namedShadowIndexingMode": "ON",
   "filter": {
       "namedShadowNames": ["Bike1-shadow"],
       "geoLocations":[{
           "name":"shadow.name.Bike1-shadow.reported.coordinates"
       }]
   },
   "customFields": [
   { "name":"attributes.battery",
   "type":"Number"}] } }'
   ```

    コマンドでは、出力が生成されません。更新が完了するまで少し時間を要する場合があります。ステータスを確認するには、[describe-index](https://docs.aws.amazon.com//cli/latest/reference/iot/describe-index.html) CLI コマンドを実行します。`indexStatus` が `ACTIVE` と表示された場合は、モノのインデックスの更新が完了しました。

1. コマンドを実行して、インデックス作成の設定を確認します。この手順は省略可能です。

   ```
   aws iot get-indexing-configuration
   ```

   出力は次のようになります。

   ```
   {
       "thingIndexingConfiguration": {
           "thingIndexingMode": "REGISTRY",
           "thingConnectivityIndexingMode": "OFF",
           "deviceDefenderIndexingMode": "OFF",
           "namedShadowIndexingMode": "ON",
           "managedFields": [
               {
                   "name": "shadow.name.*.hasDelta",
                   "type": "Boolean"
               },
               {
                   "name": "registry.version",
                   "type": "Number"
               },
               {
                   "name": "registry.thingTypeName",
                   "type": "String"
               },
               {
                   "name": "registry.thingGroupNames",
                   "type": "String"
               },
               {
                   "name": "shadow.name.*.version",
                   "type": "Number"
               },
               {
                   "name": "thingName",
                   "type": "String"
               },
               {
                   "name": "thingId",
                   "type": "String"
               }
           ],
           "customFields": [
               {
                   "name": "attributes.battery",
                   "type": "Number"
               }
           ],
           "filter": {
               "namedShadowNames": [
                   "Bike1-shadow"
               ],
               "geoLocations": [
                   {
                       "name": "shadow.name.Bike1-shadow.reported.coordinates",
                       "order": "LatLon"
                   }
               ]
           }
       },
       "thingGroupIndexingConfiguration": {
           "thingGroupIndexingMode": "OFF"
       }
   }
   ```

## ジオクエリの実行
<a name="location-indexing-run-geoquery"></a>

これで、モノのインデックスの作成設定を更新して、位置データを含めました。ジオクエリを作成して実行し、目的の検索結果を取得できるかどうかを確認してください。ジオクエリは、[クエリ構文](query-syntax.md)に従う必要があります。[ジオクエリの例](location-indexing-geoquery.md#location-indexing-geoqueries) に有用なジオクエリの例があります。

次のコマンド例では、ジオクエリ `shadow.name.Bike1-shadow.reported.coordinates:geo_distance,47.6204,-122.3491,15.5km` を使用して、座標 (47.6204,-122.3491) を使用して中心点から 15.5 km の半径距離内にあるデバイスを検索します。

```
aws iot search-index --query-string "shadow.name.Bike1-shadow.reported.coordinates:geo_distance,47.6204,-122.3491,15.5km"
```

座標「lat」: 47.6153、「lon」: -122.3333 にデバイスがあり、これはは中心点から 15.5 km の距離以内となるため、このデバイス (Bike-1) を出力で確認できます。出力は次のようになります。

```
{
    "things": [
        {
            "thingName": "Bike-1",
            "thingId": "df9cf01d-b0c8-48fe-a2e2-e16cff6b23df",
            "attributes": {
                "acqDate": "06/09/23",
                "battery": "35",
                "model": "OEM-2302-12"
            },
            "shadow": "{\"reported\":{\"coordinates\":{\"lat\":47.6153,\"lon\":-122.3333}},\"metadata\":{\"reported\":{\"coordinates\":{\"lat\":{\"timestamp\":1699572906},\"lon\":{\"timestamp\":1699572906}}}},\"hasDelta\":false,\"version\":1}"
        }
    ]
}
```

詳細については、「[位置データのインデックス作成](location-indexing-geoquery.md)」を参照してください。