需要下载对应elasticsearch版本的IK分词器和拼音分词器
IK分词器下载地址:https://github.com/medcl/elasticsearch-analysis-ik
pinyin分词器下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases
放到elasticsearch\plugins目录下
require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class ES{
private $client;
public function __construct(){
$params = array(
'127.0.0.1:9200'
);
$this->client = ClientBuilder::create()->setHosts($params)->build();
}
//是否存在索引
public function existsIndex($index_name = 'aki'){
$params = ['index' => $index_name];
if($this->client->indices()->exists($params)){
return true;
}
return false;
}
//创建索引
function createIndex($index_name = 'aki',$type_index_name="type_aki"){
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
]
];
return $this->client->indices()->create($params);
}
//创建索引和映射
function createIndexMapping($index_name = 'aki',$type_index_name="type_aki"){
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
,'mappings' => [
$type_index_name => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'title' => [
'type' => 'text',
'analyzer' => ['pinyin','ik_max_word']
],
'overview' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
]
]
]
]
]
];
return $this->client->indices()->create($params);
}
public function fenci(){
$params = [
'body' => [
//中文分词
// 'text' => "天天敲代码非常高兴",
// 'analyzer'=>'ik_max_word', //ik_max_word 最细粒度拆分 ik_smart最粗粒度拆分
//拼音分词
"text" => "张记麻辣香锅",
"analyzer" => "pinyin"
]
];
return $res = $this->client->indices()->analyze($params);
}
// 查看映射
public function get_mapping($type_name = 'users',$index_name = 'aki') {
$params = [
'index' => $index_name,
'include_type_name' => true,
'type' => $type_name
];
try {
$response = $this->client->indices()->getMapping($params);
} catch (\Exception $e) {
return false;
}
return $response;
}
//创建映射
public function createMappings($type_name = 'users',$index_name = 'aki',$properties =array()) {
// 整型 //integer text keyword......
$params = [
'index' => $index_name,
'include_type_name' => true,
'type' => $type_name,
'body' => [
$type_name => [
'_source' => [
'enabled' => true
],
'properties' => $properties
// 'properties' => [
// 'id' => [
// 'type' => 'integer'
// ],
// 'title' => [
// 'type' => 'text',
// 'analyzer' => 'pinyin'
// ],
// 'overview' => [
// 'type' => 'text'
// ]
// ]
]
]
];
try {
$response = $this->client->indices()->putMapping($params);
} catch (\Exception $e) {
return $e->getMessage();
}
return $response;
}
// 判断文档存在
public function exists_doc($id = 1,$type_name = 'users',$index_name = 'aki') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->exists($params);
return $response;
}
// 添加文档
public function add_doc($id,$doc,$type_name = 'users',$index_name = 'aki') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $doc
];
$response = $this->client->index($params);
return $response;
}
// 查询文档 (分页,排序,权重,过滤)
public function search_doc($keywords = "运维",$index_name = "aki",$type_name = "users",$from = 0,$size = 2) {
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => [
'query' => [
//模糊查询,bool查询里must和should同时使用需要设置minimum_should_match为1
'bool' => [
'should' => [
[
'match' => [
'title' => [
'query' => $keywords,
'boost' => 3, // 权重大
]
]
],
[
'match' => [
'overview' => [
'query' => $keywords,
'boost' => 2,
]
]
],
],
'minimum_should_match' =>1,
]
],
'track_total_hits' => true, //返回所有满足记录数
// 'sort' => [ //Sort 排序 $sort排序字段
// 'age'=>[
// 'order'=>'desc'
// ]
// ],
//Size 和 from 分页
'from' => $from,
'size' => $size
]
];
$results = $this->client->search($params);
return $results;
}
// 删除索引
public function delete_index($index_name = 'aki') {
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
//使用
public function search(){
//搜索值
//$search = input('search');
$search = "libai";
$isidnex = true;
$is_es = true;
$index_name = "aki_test";
$type_index_name = "type_aki_test";
try {
$isidnex = $this->existsIndex($index_name);
} catch (\Exception $e) {
// echo "未开启elasticcsearch服务";
$is_es = false;
}
// dump($this->fenci()); //测试IK分词
$searchResult = array();
if($search || !empty($search)){
if($is_es){
if($isidnex == false){
$this->createIndex($index_name,$type_index_name); //创建索引
$r2 = $this->get_mapping($type_index_name,$index_name);
if($r2 == false){
$properties = [
'id' => [
'type' => 'integer'
],
'title' => [
'type' => 'text',
// 'analyzer' => ['pinyin','ik_max_word']
'analyzer' => 'pinyin'
],
'overview' => [
'type' => 'text',
'analyzer' => 'ik_max_word' // 或ik_smart
]
];
//映射索引(相当于mysql表格新增字段)
$this->createMappings($type_index_name,$index_name,$properties);
}
$Brand_all = [];
$Brand_all[] = ['id'=>1,'title'=>'李白','overview'=>'酒仙刺客'];
$Brand_all[] = ['id'=>2,'title'=>'孙悟空','overview'=>'腾云驾雾的辜负了紫霞的刺客。'];
$Brand_all[] = ['id'=>3,'title'=>'胡歌','overview'=>'尽职励志,不错哦。'];
$Brand_all[] = ['id'=>4,'title'=>'王者荣耀','overview'=>'游戏就玩王者荣耀。'];
$Brand_all[] = ['id'=>5,'title'=>'鲁班','overview'=>'小短腿,谁都想灭。'];
$Brand_all[] = ['id'=>6,'title'=>'妲己','overview'=>'祸国殃民。'];
$Brand_all[] = ['id'=>7,'title'=>'吕布','overview'=>'方天画戟,后手放大'];
$Brand_all[] = ['id'=>8,'title'=>'水晶','overview'=>'保护我方水晶,进攻地方水晶。'];
foreach ($Brand_all as $key => $value) {
if($this->exists_doc($value['id'],$type_index_name,$index_name) == false){
$data = [
'id' => $value['id'],
'title' => $value['title'],
'overview' => $value['overview']
];
$this->add_doc($value['id'],$data,$type_index_name,$index_name); //3.添加文档
}
}
}
$result = $this->search_doc($search,$index_name,$type_index_name,$from = 0,$size = 2);
//4.搜索结果
var_dump($result);
}
}
}
}
查看映射
查询结果