php使用es 搜索关键词
public function es_search(Request $request){
$keywords = $request->param('keywords','','trim');
$page = $request->param('page');
$size = 20;
if($page){
$from = ($page-1)*20;
$pre = $page-1;
$next = $page+1;
}else{
$pre = 1;
$next = 2;
$from = ($pre-1)*20;
}
$search = [
'index'=>'goods',
'body'=>[
'query'=>[
'match'=>[
'goods_title'=>$keywords,
]
],
'from'=>$from,
'size'=>$size,
'highlight'=>[
'fields'=>[
'goods_title'=>new \stdClass() //new \stdClass()设置关键词高亮显示
],
'pre_tags'=>["<font style='color:red'>"],
'post_tags'=>['</font>']
]
]
];
$res = $this->client->search($search);
return view('list',['goods'=>$res['hits']['hits'],'keywords'=>$keywords,'pre'=>$pre,'next'=>$next]);
}
php es 索引操作
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use think\facade\Config;
use think\facade\Db;
use Elasticsearch\ClientBuilder;
class Index
{
public $client;
public function __construct()
{
$this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
/**
* 创建索引
*/
public function index()
{
$index = [
'index' => 'tp6_demo', //索引名称
'body' => [
'mappings' => [
'properties'=>[
'id' => [
'type' => 'integer'
],
'title' => [
'type' => 'text',
'analyzer' => 'ik_max_word' //指定IK分词器
]
]
]
]
];
$res = $this->client->indices()->create($index);
p($res);
}
/**
* 判断索引是否存在
*/
public function index_exists(){
$res = $this->client->indices()->exists(['index' => 'index_tp6']);
p($res, 1);
}
/**
* 删除索引
*/
public function del_index(){
$res = $this->client->indices()->delete(['index' => 'index_test']);
p($res, 1);
}
/**
* 获取索引结构
*/
public function getMapping(){
$res = $this->client->indices()->getMapping(['index' => 'ik_index']);
p($res);
}
/**
* 往索引中添加数据
*/
public function add_doc(){
$doc = [
'index' => 'index_tp6',
'id' => 1,
'body' => [
'name' => '张学友' . mt_rand(0,1000),
'age' => mt_rand(15,96),
'addr' => '湖北省武汉市洪山区'
]
];
$res = $this->client->index($doc);
p($res);
}
/**
* 批量往索引中添加数据
*/
public function all_doc(){
$doc['body'] = [
[
'index' => [
'_index' => 'index_tp6',
'_id' => 2
]
],
[
'name' => '刘德华' . mt_rand(1,1000),
'age' => mt_rand(1,100),
'addr' => '湖北省武汉市'
],
[
'index' => [
'_index' => 'index_tp6',
'_id' => 3
]
],
[
'name' => '刘德华' . mt_rand(1,1000),
'age' => mt_rand(1,100),
'addr' => '湖北省武汉市'
],
];
$res = $this->client->bulk($doc);
p($res);
}
/**
* 根据ID查询
*/
public function selectById(){
$id = [
'index' => 'index_tp6',
'id' => 3,
'_source' => ['name', 'age','addr'] // 只显示这两个字段
];
$res = $this->client->get($id);
p($res);
}
/**
* 根据ID删除数据
*/
public function deleteById(){
$id = [
'index' => 'index_tp6',
'id' => 1
];
$response = $this->client->delete($id);
p($response);
}
/**
* 根据Id 修改数据
*/
public function updateById(){
$doc = [
'index' => 'index_tp6',
'id' => 3,
'body'=>[
'doc' => [
'age' => 100
]
]
];
$response = $this->client->update($doc);
p($response);
}
/**
* 根据条件查询
*/
public function selectWhere(){
$param = [
'index' => 'tp6_demo',
'body' => [
'query' => [
'match' => [
]
]
]
];
$res = $this->client->search($param);
p($res);
}
}