<?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);
分布式搜索引擎elasticsearch PHP API index curd
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.