分佈式搜索引擎elasticsearch PHP API index curd

<?php
	require '../vendor/autoload.php';
	function p ($param) {
	if (!is_array($param) && !is_object($param))
	{
		echo $param;
		return true;
	}
		echo '<pre>';
		print_r($param);
		echo '</pre>';
	}

	$client = new Elasticsearch\Client();

	//索引即數據庫,index相當與關係數據庫的databases,type相當與table,_id 相當table的主鍵
	$indexParams['index']  = 'index_001';    //
	$r = $client->indices()->create($indexParams);
	// p($r);
	//打印結果
	//Array
	// (
	//     [acknowledged] => 1
	// )

	//
	//查看剛纔插入的index_001
	$ret = $client -> indices()->getSettings($indexParams);
	// 打印:p($ret);
	// Array
	// (
	//     [index_001] => Array
	//         (
	//             [settings] => Array
	//                 (
	//                     [index] => Array
	//                         (
	//                             [number_of_shards] => 5 #默認值
	//                             [number_of_replicas] => 1 #默認值
	//                             [version] => Array
	//                                 (
	//                                     [created] => 1020299
	//                                 )
	//                             [uuid] => KxmrZQg9T-qMFvjErqwVRQ
	//                         )
	//                 )
	//         )
	// )
	
	//The Put Settings API allows you to modify any index setting that is dynamic: //可以動態修改索引屬性
	//$indexParams['body']['settings']['index']['number_of_shards'] = 3; //分片變爲3個.//這個選項不要加上去. 對於一個索引來說,number_of_shards只能設置一次
	$indexParams['body']['settings']['index']['number_of_replicas'] = 0;//留存一個副本
	$indexParams['body']['settings']['index']['refresh_interval'] = -1;//刷新索引的頻率
	$ret = $client -> indices() -> putSettings($indexParams);
	p($ret);

	//刪除
	$indexParams['index'] = 'index_001';
	$ret = $client -> indices() -> delete($indexParams);
	p($ret);
	//打印結果
	//Array
	// (
	//     [acknowledged] => 1
	// )
	//

	//總結:創建索引
	// curl -i -XPOST "localhost:9200/index_name" -d '{
	// 	"number_of_replicas" : 0,
	// 	"refresh_interval" : -1,
	// 	"number_of_shards" : 3
	// }'
	$indexParams['index'] = 'index_name';
	$indexParams['body']['index'] = array(
											'number_of_shards' => 3,//只能設置一次,不能修改
										  	'refresh_interval' => -1,
										  	'number_of_replicas' => 0 //快照(副本)
										);
	$ret = $client -> indices() -> create($indexParams);

	p($ret);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章