对Elasticsearch进行查询(PHP)
我会用PHP向这里创建的数据进行查询。
将数据投入到Elasticsearch中。
#! /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
)
*** 終了 ***