LoginSignup
9
6

More than 5 years have passed since last update.

AWS ElasticSearch ServiceのデータをPHPで取得する

Last updated at Posted at 2018-06-20

ESデータがGETでフルオープンの場合の取得方法です。
ESのポリシーにIAM RoleのARN書きたいんじゃぁ〜って方はこちら=>IAM Roleを使ってAWS ElasticSearch ServiceのデータをLaravelで取得する

AWS側の設定

Amazon Elasticsearch Serviceポリシー例
外部からのGETのみ許可します。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:ESHttpGet",
      "Resource": "arn:aws:es:ap-northeast-1:000000000000:domain/xxx/*"
    }
  ]
}

ComposerでClientBuilderをインストール

ClientBuilderが必要なのでcomposer.jsonに追記します。

composer.json
"require": {
    "elasticsearch/elasticsearch": "~6.0"
}

$ composer update

準備が整ったのであともう一歩です。

PHP側から読み込み

xxx.php
use Elasticsearch\ClientBuilder;

$hosts = [
    'https://xxx.ap-northeast-1.es.amazonaws.com:443'
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    'index' => '*'
];

$response = $client->search($params);

$responseにデータ返ってきていると思います。

パラメーターの詳しい書き方はこちら
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html

9
6
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
9
6