附文件目录

index.php文件代码:
<php
use Vcurl\Vcurl;
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';
require 'vcurl/vcurl.php';
$params = array(
'http://localhost:9200'
);
error_reporting(7);
$client = ClientBuilder::create()->setHosts($params)->build();
class es_class{
/*
* 创建索引文档
*/
public function add($index,$my_type){
global $client;
$params = [
'index' => $index,
'type' => $my_type,
'id' => 'mys',
'body' => ['wew' => '234']
];
$response = $client->index($params);
print_r($response);
}
/*
* 查询索引
*/
public function get_es($index,$my_type){
global $client;
$params = [
'index' => $index,
'type' => $my_type,
'id' => '4wYAMWwBFSKaJdl1QFEp'
];
$response = $client->get($params);
print_r($response);
}
/*
* 检索
*/
public function search_es($index,$my_type){
global $client;
$pagesize=20;//每页多少条记录
$page=1;//第几页
$params = [
'index' => $index,
'type' => $my_type,
'size' => '20',
'from' => ($page-1)*$pagesize,
// 'body' => [
// 'query' => [
// 'match' => [
// 'testField' => '陈'
// ]
// ]
// ]
];
$response = $client->search($params);
print_r($response);
}
/*
* 删除文档
*/
public function delete_es($index,$my_type){
global $client;
$params = [
'index' => $index,
'type' => $my_type,
'id' => 'my_id'
];
$response = $client->delete($params);
print_r($response);
}
/*
* 删除索引
*/
public function delete_index($index){
global $client;
$deleteParams = [
'index' => $index
];
$response = $client->indices()->delete($deleteParams);
print_r($response);
}
/*
* 创建索引
*/
public function create_index($index,$type){
global $client;
$params = [
'index' => 'cjf_es',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
}
/*
* 返回索引和类型的映射细节
*/
public function find_es_info($index){
global $client;
$params = [
'index' => 'cjf_index',
'type' => 'my_type'
];
$response = $client->indices()->getMapping($params);
print_r($response);
}
/*
* 批量索引bulk
*/
public function bulk_es($index,$type){
global $client;
$params = ['body' => []];
for ($i = 1; $i <= 20; $i++) {
$params['body'][] = [
'index' => [
'_index' => 'my_index',
'_type' => 'my_type',
'_id' => $i
]
];
$params['body'][] = [
'my_field' => 'my_value',
'second_field' => 'some more values'
];
// Every 1000 documents stop and send the bulk request
if ($i % 1000 == 0) {
$responses = $client->bulk($params);
// erase the old bulk request
$params = ['body' => []];
// unset the bulk response when you are done to save memory
unset($responses);
}
}
// Send the last batch if it exists
if (!empty($params['body'])) {
$responses = $client->bulk($params);
}
}
/*
* 批量更新文档字段bulk
*/
public function update_bulk_es($index,$type){
global $client;
$update_arr=[];
$action_arr = $_GET['action'] ? $_GET['action'] : [];
if(!empty($action_arr))
{
foreach($action_arr as $key => $value)
{
$update_arr[$key] = $value;
}
}
if($_GET['article_ids']){
foreach(explode(",",$_GET['article_ids']) as $id){
$params['body'][] = [
'update' => [
'_index' => $index,
'_type' => $type,
'_id' => $id
]
];
$params['body'][] = [
'doc' => $update_arr
];
}
}
//print_r($params);die;
$responses = $client->bulk($params);
print_r($responses);
}
/*
* es字段部分更新
*/
public function update_part_words($index,$type){
global $client;
$params = [
'index' => $index,
'type' => $type,
'id' => '4wYAMWwBFSKaJdl1QFEp',
'body' => [
'doc' => [
'new_field '=> 'abc11221'
]
]
];
//print_r($params);die;
$response = $client->update($params);
print_r($response);
}
/*
* 删除es文档
*/
public function delete_doc($index,$type){
global $client;
$params = [
'index' => $index,
'type' => $type,
'id' => 'my_id'
];
$response = $client->delete($params);
print_r($response);
}
}
$obj=new es_class();
$obj->search_es('cjf_es','my_type');
?>