php使用Elasticsearch之批量操作(bulk)
批量插入 ,需要指定id
<?php
include '../vendor/Elasticsearch/autoload.php';
$a['hosts'] = array(
#需要用户名时 http://user:password@URL:por 其他时候直接写ip:port
'ip:9200',
);
$client = new \Elasticsearch\Client($a);
#bulk批量生成
$params['index'] = 'article';
$params['type'] = 'test';
for($i = 21; $i <= 30; $i ++) {
$params['body'][]=array(
'create' => array( #注意create也可换成index
'_id'=>$i
),
);
$params['body'][]=array(
'aa'=>$i
);
}
$res = $client->bulk($params);
批量插入, 用默认的id
<?php
include '../vendor/Elasticsearch/autoload.php';
$a['hosts'] = array(
#需要用户名时 http://user:password@URL:por 其他时候直接写ip:port
'ip:9200',
);
$client = new \Elasticsearch\Client($a);
#bulk批量生成
for ($i = 41; $i <= 50; $i++) {
$params['body'][] = array(
'index' => array(
'_index' => 'article',
'_type' => 'test',
),
);
$params['body'][] = array(
'aa' => $i,
);
}
$res = $client->bulk($params);