<?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();
//創建索引/添加mapping/到批量添加數據
//bulking (批量添加數據)
$indexParam['index'] = 'info'; //info庫
// $indexParam['type'] = 'news';//新聞信息
// $indexParam['body']['settings'] = array(
// //設置setting信息
// 'number_of_shards' => 3,//1個索引分3片
// 'number_of_replicas' => true,//保留一個副本
// // 'refresh_interval' => -1
// );
//創建mapping
$mapParam = array(
'_source' => array(
'enable' => true
),
'properties' => array(
'title' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'score'=>array(
'type' => 'integer',
'index' => 'not_analyzed'
),
'url' => array(
'type' => 'string',
'index'=> 'not_analyzed'
)
)
);
$indexParam['body']['mappings']['news'] = $mapParam;
$res = $client -> indices() -> create($indexParam);
// p($indexParam);
$bulk = array('index'=>'info','type'=>'news');
//bulk批量生成
for($i = 1; $i <= 1000; $i ++) {
$bulk['body'][]=array(
'index' => array(
'_id'=>$i
),
);
$bulk['body'][]=array(
'title'=>RandStr(),
'score'=>mt_rand('0','101'),
'url'=>'http://www.2144.cn'
);
}
$res = $client->bulk($bulk);
p($res);
//測試函數
function RandStr()
{
$zm = range('aa','zz');
$zm = array_map(function($a){
$str = '';
for ($i = 0; $i < 10; $i ++) {
$str .= $a;
}
return $str;
}, $zm);
$num = range('1900','2000');
$merge = array_merge($zm,$num);
shuffle($merge);
return substr(str_shuffle(join('',$merge)),0,mt_rand(8,14));
}
分佈式搜索引擎elasticsearch PHP API index bulk 批量插入操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.