elasticsearch分詞外掛安裝
阿新 • • 發佈:2018-11-25
官方地址: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.*'
]
]