LoginSignup
8
8

More than 5 years have passed since last update.

Amazon CloudSearch 検索ドメイン作成とデータ検索手順 (Amazon S3に格納したファイルからインデックス作成)

Last updated at Posted at 2016-09-24

はじめに

自分の勉強がてら、Amazon CloudSearchにデータを登録して、CloudSearchでデータを検索する手順について記します。

CloudSearchでデータを検索するには、検索用の元データを記載したドキュメントファイルをCloudSearchへ登録する必要があります。CloudSearchは登録したドキュメントファイルを元にして、データ検索用のインデックスを生成します。

このページでは、Amazon S3にCloudSearch検索用の元データとなるドキュメントファイル(jsonファイル)をアップロードして、TokyoリージョンにCloudSearch検索ドメインを作成し、CloudSearchでデータを検索する手順について記します。

参考資料

Amazon CloudSearch 用にデータを準備
https://docs.aws.amazon.com/ja_jp/cloudsearch/latest/developerguide/preparing-data.html

Amazon CloudSearch ドメインへの検索リクエストの送信
https://docs.aws.amazon.com/ja_jp/cloudsearch/latest/developerguide/submitting-search-requests.html

Amazon CloudSearch でのテキストの検索
https://docs.aws.amazon.com/ja_jp/cloudsearch/latest/developerguide/searching-text.html

Amazon CloudSearch のテキスト分析スキームの設定
https://docs.aws.amazon.com/ja_jp/cloudsearch/latest/developerguide/configuring-analysis-schemes.html

Amazon CloudSearch設定手順

(1) AWSマネジメントコンソールへログインします。

画面右上のTokyoリージョン「アジアパシフィック (東京)」をクリックします。

(2) AWSマネジメントコンソールの「CloudSearch」をクリックします。

(3)「Create a new search domain」をクリックします。

「Welcome to Amazon CloudSearch」画面が表示されます。
「Create a new search domain」をクリックします。

(4) CloudSearch検索用のドキュメントファイルを作成します。

以下を参考にして、CloudSearchの検索対象としたいドキュメントファイル(CloudSearchインデックスの元データ)を作成します。

json形式のドキュメントファイルを作成します。

[ec2-user@example-cloudsearch-client ~]$ vi example_document.json
example_document.json
{ "type": "add",
  "id":   "tt0484562",
  "fields": {
    "title": "The Seeker: The Dark Is Rising",
    "directors": ["Cunningham, David L."],
    "genres": ["Adventure","Drama","Fantasy","Thriller"],
    "actors": ["McShane, Ian","Eccleston, Christopher","Conroy, Frances",
              "Crewson, Wendy","Ludwig, Alexander","Cosmo, James",
              "Warner, Amelia","Hickey, John Benjamin","Piddock, Jim",
              "Lockhart, Emma"]
  }
}

jqコマンドをインストールして、作成したドキュメントファイルがjsonとして有効かチェックします。

[ec2-user@example-cloudsearch-client ~]$ sudo yum install jq
[ec2-user@example-cloudsearch-client ~]$ cat example_document.json | jq '.'
{
  "type": "add",
  "id": "tt0484562",
  "fields": {
    "title": "The Seeker: The Dark Is Rising",
    "directors": [
      "Cunningham, David L."
    ],
    "genres": [
      "Adventure",
      "Drama",
      "Fantasy",
      "Thriller"
    ],
    "actors": [
      "McShane, Ian",
      "Eccleston, Christopher",
      "Conroy, Frances",
      "Crewson, Wendy",
      "Ludwig, Alexander",
      "Cosmo, James",
      "Warner, Amelia",
      "Hickey, John Benjamin",
      "Piddock, Jim",
      "Lockhart, Emma"
    ]
  }
}
[ec2-user@example-cloudsearch-client ~]$

(5) CloudSearch検索用ドキュメントファイルを格納するAmazon S3バケットを作成します。

AWSマネジメントコンソールの「S3」をクリックします。

「バケットを作成」をクリックします。今回の例では以下のS3バケット( s3://example-search-document )を作成します。

設定項目 入力する内容
バケット名 example-search-document
リージョン Tokyo

(6) S3バケットに作成したCloudSearch検索用ドキュメントファイルをアップロードします。

作成したS3バケットに対して、前述の手順(4)で作成したCloudSearch検索用ドキュメントファイルをアップロードします。

[ec2-user@example-cloudsearch-client ~]$ aws s3 cp example_document.json s3://example-search-document/document/
upload: ./example_document.json to s3://example-search-document/document/example_document.json
[ec2-user@example-cloudsearch-client ~]$
[ec2-user@example-cloudsearch-client ~]$ aws s3 ls s3://example-search-document/document/example_document.json
2016-09-25 01:08:53        452 example_document.json
[ec2-user@example-cloudsearch-client ~]$

(7) 作成するCloudSearch検索ドメイン情報を入力します。

「Create New Search Domain」の「NAME YOUR DOMAIN」画面が表示されます。
今回の例では以下のように入力します。

設定項目 入力する内容
Search Domain Name example-search-domain
Desired Instance Type Use default
Desired Replication Count Use default

「Continue」をクリックします。

(8) CloudSearch検索用ドキュメントファイルの格納場所を指定します。

「CONFIGURE INDEX」画面が表示されます。

「How do you want to configure your index fields?」と表示されます。

「Analyze sample object(s) from Amazon S3」を選択します。S3の各設定項目については以下を選択します。

設定項目 選択する内容
S3 Bucket example-search-document
Prefix document/example_document.json

「Add」をクリックします。

「Continue」をクリックします。

(9) CloudSearchで検索するフィールドのインデックスを追加します。

「REVIEW INDEX CONFIGURATION」画面が表示されます。

「Suggested Index Field Configuration」と表示されます。

「Add Index Field」をクリックして、検索用インデックスに以下3つのフィールドを追加します。「Type」は「Text」、「Analysis Scheme」は「Eng」を選択してフィールドを追加します。

追加するIndex Fields Type Analysis Scheme
directors Text Eng
genres Text Eng
actors Text Eng

上記のフィールドを追加し、「Index Fields」を以下のように設定したら「Continue」をクリックします。

これにより、CloudSearchは前述(4)でS3バケットに格納したドキュメントファイル(json)を取り込んで、検索用のデータ(インデックス)が作成されます。

Name Type Search Facet Return Sort Highlight Analysis Scheme Default Value Source Field Remove
content Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
content_encoding Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
content_type Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
resourcename Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
directors Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
genres Text チェックをつける チェックをつける チェックをつける チェックをつける Eng
actors Text チェックをつける チェックをつける チェックをつける チェックをつける Eng

(10) CloudSearch検索ドメインに対するアクセス許可を設定します。

今回の例では特定IPアドレス(例:198.51.100.13)からのみCloudSearchのデータ検索を許可します。
この設定により、特定IPアドレス(例:198.51.100.13)以外からCloudSearch検索ドメインへ検索リクエストを送る事を禁止出来ます。

「SETUP ACCESS POLICIES」画面が表示されます。

「Current Policy」に以下のように入力します。

入力するPolicy内容
{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"search_only",
      "Effect":"Allow",
      "Principal":"*",
      "Action":["cloudsearch:search"],
      "Condition":{"IpAddress":{"aws:SourceIp":"198.51.100.13/32"}}
    }
  ]
}

「Continue」をクリックします。

(11) CloudSearch検索ドメインの作成を開始します。

「CONFIRM」画面が表示されます。
「Continue」をクリックします。

(12) CloudSearch検索ドメインの作成が完了するまで待ちます。

AWSマネジメントコンソールに以下のメッセージが表示されます。

Congratulations
You have successfully created the domain example-search-domain. The domain will take approximately 10 minutes to initialize. Once the domain is initialized, you may upload documents, and make any other changes to the domain.

AWSマネジメントコンソール右下の「OK」をクリックします。

AWSマネジメントコンソールの「CloudSearch」->「Dashboard」画面へ切り替わります。

CloudSearch検索ドメインの作成が完了するまで待ちます。

(13) CloudSearch検索ドメインの作成が完了した事を確認します。

AWSマネジメントコンソールの「CloudSearch」->「Dashboard」を確認します。

「Domain Status for example-search-domain」に「example-search-domain is: ACTIVE」と表示されている事を確認します。

「Dashboard」の「Search Endpoint」のCloudSearch検索ドメイン名を控えておきます。この「Search Endpoint」の検索ドメインを指定して、CloudSearchに登録したデータを検索出来ます。詳細は後述します。

Searchable Documents:   1
Index Fields:   7
Search Endpoint:    search-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com
Document Endpoint:  doc-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com
Domain ARN: arn:aws:cloudsearch:ap-northeast-1:464959028036:domain/example-search-domain
Engine Type:    CloudSearch (2013 API)

(14) CloudSearchのデータを検索を実行します。

キーワードを指定してCloudSearchからデータを検索します。

前述(13)の「Search Endpoint」に表示されているCloudSearch検索ドメイン名を指定して、データを検索します。

CloudSearch検索ドメイン例

search-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com

CloudSearchのデータ検索については、CloudSearch検索ドメインへのアクセスを許可したIPアドレス(198.51.100.13/32)から以下の検索リクエストを送信する事で実行します。

[ec2-user@example-cloudsearch-client ~]$ /sbin/ifconfig eth0 | grep 'inet addr'
          inet addr:198.51.100.13  Bcast:198.51.100.255  Mask:255.255.240.0
[ec2-user@example-cloudsearch-client ~]$

(14-1) CloudSearchから'McShane'という文字列が含まれているデータを検索します。

以下のコマンドを実行し、CloudSearchから'McShane'という文字列が含まれているデータを検索します。

CloudSearch検索ドメインへ正常にアクセス出来る場合、以下のようにCloudSarchからの検索レスポンスの「Hit」に検索結果が表示されます。

前述(4)でS3バケットに格納したドキュメントファイル(s3://example-search-document/document/example_document.json)を元にして、CloudSearch検索用のインデックスを作成しているので、検索結果には以下のように表示されています。

[ec2-user@example-cloudsearch-client ~]$ aws cloudsearchdomain --region ap-northeast-1 --endpoint-url http://search-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com search --search-query McShane
{
    "status": {
        "timems": 1,
        "rid": "jr+w6/UqlAEKeR92"
    },
    "hits": {
        "found": 1,
        "hit": [
            {
                "fields": {
                    "content": [
                        "{ \"type\": \"add\",   \"id\":   \"tt0484562\",   \"fields\": {     \"title\": \"The Seeker: The Dark Is Rising\",     \"directors\": [\"Cunningham, David L.\"],     \"genres\": [\"Adventure\",\"Drama\",\"Fantasy\",\"Thriller\"],     \"actors\": [\"McShane, Ian\",\"Eccleston, Christopher\",\"Conroy, Frances\",               \"Crewson, Wendy\",\"Ludwig, Alexander\",\"Cosmo, James\",               \"Warner, Amelia\",\"Hickey, John Benjamin\",\"Piddock, Jim\",               \"Lockhart, Emma\"]   } }"
                    ],
                    "resourcename": [
                        "example_document.json"
                    ],
                    "content_encoding": [
                        "ISO-8859-1"
                    ],
                    "content_type": [
                        "application/json"
                    ]
                },
                "id": "example-search-document/document/example_document.json"
            }
        ],
        "start": 0
    }
}
[ec2-user@example-cloudsearch-client ~]$

(14-2) CloudSearchからactoresフィールドに'McShane'という文字列が含まれているデータを検索します。

以下のコマンドを実行し、CloudSearchからactoresフィールドに'McShane'という文字列が含まれているデータを検索します。

[ec2-user@example-cloudsearch-client ~]$ aws cloudsearchdomain --region ap-northeast-1 --endpoint-url http://search-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com search --search-query "actores:'McShane'"
{
    "status": {
        "timems": 5,
        "rid": "nZCy6/UqlgEKeR92"
    },
    "hits": {
        "found": 1,
        "hit": [
            {
                "fields": {
                    "content": [
                        "{ \"type\": \"add\",   \"id\":   \"tt0484562\",   \"fields\": {     \"title\": \"The Seeker: The Dark Is Rising\",     \"directors\": [\"Cunningham, David L.\"],     \"genres\": [\"Adventure\",\"Drama\",\"Fantasy\",\"Thriller\"],     \"actors\": [\"McShane, Ian\",\"Eccleston, Christopher\",\"Conroy, Frances\",               \"Crewson, Wendy\",\"Ludwig, Alexander\",\"Cosmo, James\",               \"Warner, Amelia\",\"Hickey, John Benjamin\",\"Piddock, Jim\",               \"Lockhart, Emma\"]   } }"
                    ],
                    "resourcename": [
                        "example_document.json"
                    ],
                    "content_encoding": [
                        "ISO-8859-1"
                    ],
                    "content_type": [
                        "application/json"
                    ]
                },
                "id": "example-search-document/document/example_document.json"
            }
        ],
        "start": 0
    }
}
[ec2-user@example-cloudsearch-client ~]$
[ec2-user@example-cloudsearch-client ~]$ aws cloudsearchdomain --region ap-northeast-1 --endpoint-url http://search-example-search-domain-**************************.ap-northeast-1.cloudsearch.amazonaws.com search --search-query "actores:'McShane'" --return _no_fields
{
    "status": {
        "timems": 42,
        "rid": "lfve6vUqbgp5H3Y="
    },
    "hits": {
        "found": 1,
        "hit": [
            {
                "id": "example-search-document/document/example_document.json"
            }
        ],
        "start": 0
    }
}
[ec2-user@example-cloudsearch-client ~]$

以上になります。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8