目次

目次

【連載】Amazon CloudSearchのCLI入門編 #1 (検索ドメインの作成)

アバター画像
山本 耕琢(コウタク)
アバター画像
山本 耕琢(コウタク)
最終更新日2017/06/20 投稿日2017/06/20

1.概要

システムのAWS化に合わせて、提供している検索エンジンをCloudSearchに置き換える事になりました。検証するために、CLI(コマンドラインインターフェイス)経由でCloudSearchを操作したので、 その手順をまとめて残したいと思います。

2.前提条件

クライアントは、Windows環境です。CLIのコマンドは同じなので、他のコマンドは環境に合わせて適宜読みかえて下さい。あと、アクセスキーとシークレットキーを発行してCloudSearchへ接続します。

今回、準備する検索ドメインは、以下の通りとなります。

フィールド名
id literal
artist_name_li literal
album_title_li literal
artist_name_te text
album_title_te text

※ 検証内容がliteral型とtext型のscore値の確認だったため、  それぞれの検索フィードを定義しました。 ※ その他、使える型は、以下のURLを参考にして下さい。   参考URL:公式:Amazon CloudSearch ドメインのインデックスフィールドの設定

3.事前準備

以下のコマンド実行して、アクセスキーとシークレットキーを登録します。

set AWS_DEFAULT_REGION=ap-northeast-1
set AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXX
set AWS_SECRET_ACCESS_KEY=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

aws iam list-account-aliases ★アカウント名を確認する。

{
    "AccountAliases": [
        "rc-aws-learn-system-arch"
    ]
}

4.検索ドメインの作成

手順としては、以下の通りとなります。 ① 検索ドメインの作成 ② 検索フィールドの定義 ③ インデックシング

まずは、検索ドメインを作成します。

aws cloudsearch create-domain --domain-name search-score-test

{
    "DomainStatus": {
        "DomainId": "540941733278/search-score-test",
        "Created": true,
        "Deleted": false,
        "SearchInstanceCount": 0,
        "DomainName": "search-score-test",
        "SearchService": {},
        "RequiresIndexDocuments": false,
        "Processing": false,
        "DocService": {},
        "ARN": "arn:aws:cloudsearch:ap-northeast-1:000000000000:domain/search-score-test",
        "SearchPartitionCount": 0
    }
}

※ 参考URL:公式:Amazon CloudSearch ドメインの作成

続いて、検索フィールドを定義します。

aws cloudsearch define-index-field^
 --domain-name search-score-test^
 --name id^
 --type literal^
 --search-enabled true^
 --return-enabled true^
 --facet-enabled false^
 --sort-enabled true^

{
    "IndexField": {
        "Status": {
            "PendingDeletion": false,
            "State": "RequiresIndexDocuments",
            "CreationDate": "2017-02-07T01:55:00.890Z",
            "UpdateVersion": 9,
            "UpdateDate": "2017-02-07T01:55:00.890Z"
        },
        "Options": {
            "LiteralOptions": {
                "FacetEnabled": false,
                "SortEnabled": true,
                "SearchEnabled": true,
                "ReturnEnabled": true
            },
            "IndexFieldType": "literal",
            "IndexFieldName": "id"
        }
    }
}

aws cloudsearch define-index-field 
 --domain-name search-score-test^
 --name artist_name_te~
 --type text^
 --return-enabled true^
 --sort-enabled true^
 --highlight-enabled false^
 --analysis-scheme _mul_default_

{
    "IndexField": {
        "Status": {
            "PendingDeletion": false,
            "State": "RequiresIndexDocuments",
            "CreationDate": "2017-02-07T01:57:00.890Z",
            "UpdateVersion": 28,
            "UpdateDate": "2017-02-07T01:57:00.890Z"
        },
        "Options": {
            "TextOptions": {
                "SortEnabled": true,
                "HighlightEnabled": false,
                "ReturnEnabled": true,
                "AnalysisScheme": "_mul_default_"
            },
            "IndexFieldType": "text",
            "IndexFieldName": "artist_name_te"
        }
    }
}

※ --nameパラメタを変更して他の検索フィードも定義する。 ※ 参考URL:公式:Amazon CloudSearch ドメインのインデックスフィールドの設定

検索ドメインの定義変更を実施した場合、インインデックシングをする必要があります。実施するまでドキュメントの登録や検索が出来ないので、インデクシングを実施します。

aws cloudsearch index-documents --domain-name search-score-test
{
    "FieldNames": [
        "album_title_li",
        "artist_name_li",
        "album_title_te",
        "artist_name_te"
    ]
}

aws cloudsearch describe-index-fields --domain-name search-score-test
{
    "IndexFields": [
        {
            "Status": {
                "PendingDeletion": false,
                "State": "Active", ★完了するとActiveとなる。
                "CreationDate": "2017-02-12T16:42:00.102Z",
                "UpdateVersion": 25,
                "UpdateDate": "2017-02-12T17:18:48.210Z"
            },
            "Options": {
                "LiteralOptions": {
                    "FacetEnabled": false,
                    "SortEnabled": true,
                    "SearchEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "album_title_li"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "PROCESSING", ★インデクシング中はPROCESSINGとなる。
                "CreationDate": "2017-02-19T07:54:44.172Z",
                "UpdateVersion": 29,
                "UpdateDate": "2017-02-19T07:54:44.172Z"
            },
            "Options": {
                "TextOptions": {
                    "SortEnabled": true,
                    "HighlightEnabled": false,
                    "ReturnEnabled": true,
                    "AnalysisScheme": "_mul_default_"
                },
                "IndexFieldType": "text",
                "IndexFieldName": "album_title_te"
            }
        },

                "IndexFieldName": "artist_name_li"
            }
        },
        {

                "IndexFieldName": "artist_name_te"
            }
        },
        {

                "IndexFieldName": "id"
            }
        }
    ]
}

5.最後に

次回は、ドキュメントの登録と検索を実施します。

6.参考URL

公式:CLIドキュメント

アバター画像

山本 耕琢(コウタク)

レコチョクの楽曲情報の検索や、決済/会員情報、 レコメンド情報などを扱う、レコチョクAPIの運用と改善を担当しています。主にプロジェクト管理をやっています。技術力は低めですが、気づきがあったことなどを更新していきます。

目次