PHP使用elasticSearch进行分词

安装最行版本ElasticSearch,推荐使用rpm -ivh安装,下载地址:https://github.com/elastic/elasticsearch/releases

下载 medcl/elasticsearch-analysis-ik

安装方式两种:在线安装、离线安装

(1)在线安装:

Bash
cd /usr/share/elasticsearch/
./bin/elastic-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip
离线安装:需要用到maven,

    下载ik源码到磁盘,进入目录,

Bash
mvn package,
然后会下载依赖包,生成一个target文件夹,

    进入target/releases文件夹,把打包好的文件移动到plugins文件夹,并unzip解压,

    重启elasticsearch服务,

Bash
systemctrl status elasticsearch.service
确认下服务是否成功启动,如有故障进行 排查(点击看看常见错误),

4.PHP安装composer

5.composer安装

PHP
composer reuqire “elasticsearch/elasticsearch”

6.PHP代码调用:
$client=ClientBuilder::create()->setHosts([‘192.168.32.128’])->build();
$params[‘index’] = ‘index’;
$params[‘body’] = [
// ‘analyzer’ => ‘ik_max_word’,
‘analyzer’ => ‘ik_smart’,
// ‘text’ => ‘英雄联盟最强王者’
// ‘text’ => ‘中华人民共和国国歌’
‘text’ => ‘based index manager for Elasticsearch’
];
$res=$client ->indices()->analyze($params);
打印结果如下:

PHP
array(1) {
[“tokens”] => array(4) {
[0] => array(5) {
[“token”] => string(5) “based”
[“start_offset”] => int(0)
[“end_offset”] => int(5)
[“type”] => string(7) “ENGLISH”
[“position”] => int(0)
}
[1] => array(5) {
[“token”] => string(5) “index”
[“start_offset”] => int(6)
[“end_offset”] => int(11)
[“type”] => string(7) “ENGLISH”
[“position”] => int(1)
}
[2] => array(5) {
[“token”] => string(7) “manager”
[“start_offset”] => int(12)
[“end_offset”] => int(19)
[“type”] => string(7) “ENGLISH”
[“position”] => int(2)
}
[3] => array(5) {
[“token”] => string(13) “elasticsearch”
[“start_offset”] => int(24)
[“end_offset”] => int(37)
[“type”] => string(7) “ENGLISH”
[“position”] => int(3)
}
}
}

打赏 支付宝打赏 微信打赏

未经允许不得转载!

评论列表 0

访客
取消