插入单条数据的方法
<?php include '../vendor/Elasticsearch/autoload.php'; $a['hosts'] = array( #需要用户名时 http://user:password@URL:por 其他时候直接写ip:port 'ip:9200', ); $client = new \Elasticsearch\Client($a); #单条插入 $params = array(); $params['body'] = array( 'xzdfaf' => 'xfsa' ); $params['index'] = 'paopao'; $params['type'] = 'test'; // $params['id'] = 'w1231313'; $ret = $client->index($params);
批量插入的方法
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'] = 'paopao'; $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的方法
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'=> 'paopao', '_type'=> 'test' ), ); $params['body'][]=array( 'aa'=>$i ); } $res = $client->bulk($params);
ES的其它批量操作
POST /_bulk { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} { "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" } { "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" } { "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} } { "doc" : {"title" : "My updated blog post"} }
请注意
delete
动作不能有请求体,它后面跟着的是另外一个操作。谨记最后一个换行符不要落下。