LoginSignup
0
1

More than 5 years have passed since last update.

Elasticsearch への問い合わせ (PHP)

Posted at

こちらで作成したデータに PHP で問い合わせをします。
Elasticsearch へのデータ投入

elastic_query.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//  elastic_query.php
//
//                  Oct/04/2018
//
// ------------------------------------------------------------------
function curl_get_proc ($url)
{
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_PROXY, "");

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
}

// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

$url = "http://localhost:9200/_search?q=tags:apple,moon";

$json_string = curl_get_proc ($url);

$dict_aa = json_decode ($json_string,true);

$llx = count($dict_aa['hits']['hits']);

print("count = " . strval($llx) . "\n");

foreach ($dict_aa['hits']['hits'] as $unit)
    {
    $ss = $unit['_source'];
    print($ss['name'] . "\n");
    print($ss['title'] . "\n");
    print($ss['content'] . "\n");
    print_r($ss['tags']);
    }

fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>

実行結果

$ ./elastic_query.php
*** 開始 ***
count = 2
渡辺五郎
My Name Is Watanabe
I love fish
Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)
田中康夫
My Name Is Tanaka
I love cat
Array
(
    [0] => Earth
    [1] => Moon
    [2] => Mars
)
*** 終了 ***
0
1
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
0
1