0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elasticsearch の CRUD (PHP)

Posted at

ライブラリーは Requests を使います。

Create

elastic_create.php
# ! /usr/bin/php
<?php
//
//	elastic_create.php
//
//					Oct/12/2018
//
// ----------------------------------------------------------------
function dict_append_proc ($dict_aa,$id,$name,$population,$date_mod)
{
	$dict_unit = array ();
	$dict_unit['name'] = $name;
	$dict_unit['population'] = $population;
	$dict_unit['date_mod'] = $date_mod;
	$dict_aa[(string)$id]= $dict_unit;

	return	$dict_aa;
}

//
// ----------------------------------------------------------------
function data_prepare_proc ()
{
	$dict_aa = array ();

$dict_aa = dict_append_proc ($dict_aa,"t0921",'宇都宮',71345,'2002-8-15');
$dict_aa = dict_append_proc ($dict_aa,"t0922",'小山',68237,'2002-12-4');
$dict_aa = dict_append_proc ($dict_aa,"t0923",'佐野',52876,'2002-7-15');
$dict_aa = dict_append_proc ($dict_aa,"t0924",'足利',49142,'2002-6-22');
$dict_aa = dict_append_proc ($dict_aa,"t0925",'日光',81953,'2002-10-18');
$dict_aa = dict_append_proc ($dict_aa,"t0926",'下野',72537,'2002-9-21');
$dict_aa = dict_append_proc ($dict_aa,"t0927",'さくら',49816,'2002-8-24');
$dict_aa = dict_append_proc ($dict_aa,"t0928",'矢板',52681,'2002-9-21');
$dict_aa = dict_append_proc ($dict_aa,"t0929",'真岡',74536,'2002-8-5');
$dict_aa = dict_append_proc ($dict_aa,"t0930",'栃木',27146,'2002-6-17');
$dict_aa = dict_append_proc ($dict_aa,"t0931",'大田原',25371,'2002-8-15');
$dict_aa = dict_append_proc ($dict_aa,"t0932",'鹿沼',38179,'2002-9-17');
$dict_aa = dict_append_proc ($dict_aa,"t0933",'那須塩原',19436,'2002-7-11');
$dict_aa = dict_append_proc ($dict_aa,"t0934",'那須烏山',73912,'2002-8-24');

	return	$dict_aa;
}

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

include('Requests/library/Requests.php');
Requests::register_autoloader();


$dict_aa = data_prepare_proc ();

$url_base = "http://localhost:9200/cities/tochigi";

foreach ($dict_aa as $key => $value)
	{
	$json_str = json_encode($value);
	print $key . "\n";
	$url = $url_base . "/" . $key;

	$headers = array('content-type' => 'application/json');
	$request = Requests::put($url,$headers, $json_str);	
	};


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

Read

elastic_read.php
# ! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	php/read/elastic_read.php
//
//					Oct/12/2018
//
// ------------------------------------------------------------------
function dict_display_proc ($dict_aa)
{
	ksort ($dict_aa);

	foreach ($dict_aa as $key => $value)
		{
		$name = $value["name"];
		$population = $value['population'];
		$date_mod = $value['date_mod'];

		print "$key\t";
		print "$name\t";
		print "$population\t";
		print "$date_mod\n";
		}
}

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

include('Requests/library/Requests.php');
Requests::register_autoloader();

$url = "http://localhost:9200/cities/tochigi/_search?q=*&size=100";

$request = Requests::get($url, array('Accept' => 'application/json'));

$json_string = $request->body;

$data_aa = json_decode ($json_string,true);

// var_dump ($data_aa['hits']['hits']);

$count = count($data_aa['hits']['hits']);

print $count . "\n";

$dict_aa = array ();

foreach ($data_aa['hits']['hits'] as $unit)
	{
	$key = $unit['_id'];
	$dict_aa[$key] = $unit['_source'];
	}

dict_display_proc ($dict_aa);

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

Update

elastic_update.php
# ! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	update/elastic_update.php
//
//					Oct/12/2018
//
// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

date_default_timezone_set('Asia/Tokyo');
include('Requests/library/Requests.php');
Requests::register_autoloader();

$key_in = $argv[1];
$population_in = intval ($argv[2]);

echo	$key_in . "\t";
echo	$population_in . "\n";

$url = "http://localhost:9200/cities/tochigi/" . $key_in . "/_update";

print $url . "\n";

$str_date_mod = date ("Y-m-d");

$params = array("population" => $population_in,"date_mod" => $str_date_mod);
$script = array("source" => "ctx._source.population = params.population; ctx._source.date_mod = params.date_mod", "lang" => "painless", "params" => $params);
$args = array("script" => $script);
$json_str = json_encode ($args);

$headers = array('content-type' => 'application/json');
$request = Requests::post($url,$headers, $json_str);

// var_dump($request);
var_dump($request->status_code);

fputs (STDERR,"*** 終了 ***\n");

// ------------------------------------------------------------------
?>

Delete

elastic_delete.php
# ! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	elastic_delete.php
//
//					Oct/12/2018
//
// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
include('Requests/library/Requests.php');
Requests::register_autoloader();

$key_in = $argv[1];

echo	$key_in . "\n";

$url = "http://localhost:9200/cities/tochigi/" . $key_in;

$request = Requests::delete($url);

var_dump($request->status_code);

fputs (STDERR,"*** 終了 ***\n");

// ------------------------------------------------------------------
?>
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?