Elasticsearch的批量操作就像 mget 允许我们一次性检索多个文档一样, bulk API允许我们使用单一请求来实现多个文档的 create 、 index 、 update 或 delete 。这对索引类似于日志活动这样的数据流非常有用,它们可以以成百上千的数据为一
个批次按序进行索引。
批量操作的行为(action)必须是以下几种:
行为 解释
create 当文档不存在时创建之。
index 创建新文档或替换已有文档。
update 局部更新文档。
delete 删除一个文档。
注意:
在索引、创建、更新或删除时必须指定文档的 _index 、 _type 、 _id 、(如果设置了routing,也必须指定)这些元数据
创建一个例子索引的mapping如下:
$mapping = [
'index' => 'my_index',
'body' => [
'mappings' => [
'my_type' => [
'_all' => [
'enabled' => 'false'
],
'_routing' => [ #设置routing(分片路由)
'required' => 'true'
],
'properties' => [
'name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
$client = ElasticsearchClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->indices()->create($mapping);
批量执行创建文档例子
$params['body'] = [];
$nameArr = ['赵','钱','孙','李','周','吴','郑','王'];
for ($i = 1; $i <= 100; $i++) {
$params['body'][] = [
'create' => [ #创建
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => $i,
'_routing' => mt_rand(1,100),
]
];
$params['body'][] = [
'name' => $nameArr[mt_rand(0,7)],
'age' => mt_rand(18,60)
];
}
$client = ElasticsearchClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->bulk($params);
批量执行更新文档操作例子
//查找指定文档
$client = ElasticsearchClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
'range' => [
'age' => [
'gt' => '25',
'lt' => '40'
]
]
]
]
],
'from' => 0,
'size' => 100,
'sort' => [
'age' => 'desc'
]
]
];
$info = $client->search($params);
$data = $info['hits']['hits'];
// 批量执行更新文档
$params['body'] = [];
$nameArr = ['赵','钱','孙','李','周','吴','郑','王'];
foreach ($data as $key => $value) {
$params['body'][] = [
'update' => [ #更新文档
'_index' => $value['_index'],
'_type' => $value['_type'],
'_id' => $value['_id'],
'_routing' => $value['_routing'],
]
];
$params['body'][] = [
'doc' => [ #更新指定的字段值
'name' => $nameArr[mt_rand(0,7)].$nameArr[mt_rand(0,7)]
]
];
}
$res = $client->bulk($params);
增删改批量执行操作例子
$params['body'] = [];
//创建或替换文档操作
$params['body'][] = [
'index' => [ #创建或替换
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 1,
'_routing' => 1,
]
];
$params['body'][] = [
'name' => '杨',
'age' => 23
];
//创建文档操作
$params['body'][] = [
'create' => [ #创建
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 2,
'_routing' => 2,
]
];
$params['body'][] = [
'name' => '郭',
'age' => 19
];
//局部更新文档操作
$params['body'][] = [
'update' => [ #局部更新
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 3,
'_routing' => 3,
]
];
$params['body'][] = [
'doc' => [
'age' => 19
]
];
//删除文档操作
$params['body'][] = [
'delete' => [ #删除
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => 4,
'_routing' => 4,
]
];
$client = ElasticsearchClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();