Elasticsearch5 PHP Api
问:为什么官方已经有了Elasticsearch的PHP包,我还要写一个? 答:任性
composer require jaeger/e-search:dev-master
$es = new \Jaeger\Es([
//服务器地址
'server_url'=>'http://localhost:9200',
//索引
'index' => 'news',
//类型
'type' => 'article'
]);
//or
$es = (new \Jaeger\Es())->setIndex('news')->setType('article');
$result = $es->setMapping([
'title' => [
'type' => 'text',
'analyzer' => 'ik_smart'
],
'content' => [
'type' => 'text',
'analyzer' => 'ik_smart'
]
]);
$result = $es->index(1,[
'id' => 1,
'title' => 'This is title',
'content' => 'This is content'
]);
//delete document for id 1
$result = $es->delete(1);
//delete all documents of current type
$result = $es->delete();
$result = $es->count();
//or
$result = $es->request('GET','_count');
$result = $es->id(1);
//or
$result = $es->request('GET',1);
$result = $es->search($query);
$query
can be an array,JSON string, or string.
$query = [
'query' => [
'match' => [
'content' => 'this is content'
]
],
'highlight' => [
'fields' => [
//此处有坑
'content' => (object)[]
]
]
];
$query = '{
"query" : {
"match" : {
"content" : "this is content"
}
},
"highlight": {
"fields" : {
"content" : {}
}
}
}';
$query = 'this is content';
//or
$query = 'content:this is content';
/**
* send command 发送命令
* @param string $method GET,PUT,DELETE,etc
* @param string $command '_search','_count','_mapping',etc
* @param array|jsonString $data send command with data
*/
$result = $es->request($method,$command,$data);
//example
$result = $this->request('GET','_search',[
'query' => [
'match' => [
'content' => 'this is content'
]
]
]);
Jaeger JaegerCode@gmail.com