/引入es搜索类
//require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Index
{
public function index()
{
/*$client = ClientBuilder::create()->setHosts($hosts)->build();*/
//实例化es类;在项目中引入自动加载文件,并且实例化一个客户端:
$client = ClientBuilder::create()->build();
try {
//将文档加入索引
$data = db::name('articles')->select();
//查询出多条数据添加索引
foreach ($data as $k => $v) {
$params = [
'index' => 'article_index',
'type' => 'article_type',
'id' => 'article_' . $v['id'],
'body' => [
'id' => $v['id'],
'title' => $v['title'],
'content' => $v['content'],
],
];
$response = $client->index($params);
var_dump($response);
}
//从索引中获取文档
$getparams = [
'index' => 'article_index',
'type' => 'article_type',
'id' => 'article_1'
];
$res = $client->get($getparams);
//从索引中删除文档
$delparams = [
'index' => 'article_index',
'type' => 'article_type',
'id' => 'article_1'
];
$res = $client->delete($delparams);
//删除索引
$params = [
'index' => 'articles_index'
];
$res = $client->indices()->delete($params);
print_r($res);
//搜索
$serparams = [
'index' => 'article_index',
'type' => 'article_type',
];
$serparams['body']['query']['match']['content'] = '文章内容6';
$resech = $client->search($serparams);
var_dump($resech);
// pp($data);
} catch (Exception $e) {
echo $e->getMessage();
}
}
网友评论