官方地址:https://github.com/medcl/elasticsearch-analysis-ik
两种安装方式:
1. 进入elasticsearch-6.5.0/plugins/然后
mkdir ik
cd ik
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.0/elasticsearch-analysis-ik-6.5.0.zip
unzip elasticsearch-analysis-ik-6.5.0.zip
rm -rf elasticsearch-analysis-ik-6.5.0.zip
2. 或者按照如下方式安装,进入elasticsearch-6.5.0
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.0/elasticsearch-analysis-ik-6.5.0.zip
安装完之后测试分词效果
$params = [
'body' => [
'text' => '天天敲代码非常高兴',
'analyzer'=>'ik_max_word' //ik_max_word 精细 ik_smart 粗略
]
];
$res = $esclient->indices()->analyze($params);
print_r($res);
使用分词插件:(php代码)
创建空索引(必须是新索引名称)
$params = [
'index' => 'my_index_user1',
];
$res = $esclient->indices()->create($params);
创建映射
$params = [
'index' => 'my_index_user1',
'type' => 'my_type_user1',
'body' => [
'my_type_user1' => [
'_source' => [
'enabled' => true
],
'properties' => [
'userinfo' => [
'type' => 'text', // 字符串型
'analyzer'=>'ik_max_word', //ik_max_word 最细粒度拆分 ik_smart最粗粒度拆分
'search_analyzer'=> 'ik_max_word'
]
]
]
]
];
$res = $esclient->indices()->putMapping($params);
生成索引(可以使用前面批量生成或者单个生成的方法)索引名必须为 my_index_user1
$params = [
'index' => 'my_index_user1',
'type' => 'my_type_user1'
];
$addpar=[];
foreach($data as $key=>$val){
$params['body'][]= ['index' => [ '_id' => $val->id]];
unset($val->id);
foreach($val as $k=>$v){
$addpar[$k]=$v;
}
$params['body'][]=$addpar;
}
$res = $esclient->bulk($params);
print_r($res);
搜索
$search_params = [
'index' => 'my_index_user1',
'type' => 'my_type_user1',
'body' => [
'query' => [
'match' => [
'userinfo' => '访问目录'
]
],
//设置高亮
"highlight" => [
"pre_tags" => [
'<span style="color:red">'
],
"post_tags" => [
'</span>'
],
'fields'=> [
'userinfo' => new \stdClass()
]
]
]
];
$res = $esclient->search($search_params);
print_r($res);
不同查询方式,精确匹配(匹配到所有的关键词)
修改match部分
//有一个关键词出现即可
'match' => [
'userinfo' => '访问目录'
]
//所有关键词都必须出现 可以不连续
'match' => [
'name' => ['query' => '大蛋糕','operator' => 'AND']
]
//必须完全匹配
'query' => [
'match_phrase' => [
'name' => '可以查看'
]
]
//通配符查询
'query' => [
'wildcard' => [
'name' => '*user*'
]
]
//分页查询
'body' => [
'from' => 4,
'size' => 3,
'query' => [
'match_phrase' => [
'name' => '可以查看'
]
//正则查询
'query' => [
'regexp' => [
'ints' => '135.*'
]
]