配置安装
安装es服务端
下载解压:https://www.elastic.co/downloads/elasticsearch
开启服务:
Copy # 执行服务文件
/usr/local/var/elasticsearch/bin/elasticsearch
# 后台运行
./elasticsearch -d -p '/tmp/es_pid.log'
# 关闭服务
kill -9 ` cat /tmp/es_pid.log `
访问:http://127.0.0.1:9200/,显示json信息即为安装成功。
安装head插件
Copy # 下载
git clone https://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head && npm install
配置nginx站点,指向/usr/local/var/elasticsearch/plugins/elasticsearch-head
目录。
支持跨域,修改es配置文件config/elasticsearch.yml
:
Copy # 添加2行配置
http.cors.enabled: true
http.cors.allow-origin: "*"
安装中文分词插件IK
Copy ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip
重启es服务
分词器:
ik_smart: 常规(粗粒度)拆分。
ik_max_word: 细粒度拆分,会穷尽所有可能,常用。
安装中分分词之后,需要给字段设置分词器:
Copy // 给main字段设置ik_smart分词器
$client -> indices () -> putMapping ( [
'index' => 'index' ,
'type' => 'fulltext' ,
'body' => [
'properties' => [
'main' => [
'type' => 'text' ,
'analyzer' => 'ik_smart' ,
'search_analyzer' => 'ik_smart' ,
] ,
] ,
] ,
] ) ;
分词测试:
Copy # 如果测试失败,再在上index参数
$ret = $client -> indices () -> analyze ( [
'body' => [
'text' => '中华人民共和国国歌' ,
'tokenizer' => 'ik_max_word' ,
] ,
] ) ;
安装php api
Copy // composer.json 添加配置
{
"require" : {
"elasticsearch/elasticsearch" : "~6.0"
}
}
单机多节点
一个机器开启多个es节点的方法:复制整个es目录,再启动就行。
注意,如果提示node错误,注意清空新目录下的data文件夹。
概念释义
路径:/megacorp/employee/1 即为索引 megacorp
下的 employee
类型的id为1的文档。
基础用法
index
索引(动词)文档,与名词的索引
区分,即为添加一个文档索引,添加一条数据。
Copy $ret = $client -> index ( [
'index' => 'megacorp' ,
'type' => 'employee' ,
'id' => 3 ,
'body' => [
'first_name' => 'Douglas' ,
'last_name' => 'Fir' ,
'age' => 35 ,
'about' => 'I like to build cabinets' ,
'interests' => [ 'forestry' ] ,
] ,
] ) ;
注意 :如果重发索引同一条数据(index,type,id相同),则会覆盖原数据,实现整条数据的更新。
get
获取数据
Copy $ret = $client -> get ( [
'index' => 'megacorp' ,
'type' => 'employee' ,
'id' => 1 ,
] ) ;
delete
删除
Copy $ret = $client -> get ( [
'index' => 'megacorp' ,
'type' => 'employee' ,
'id' => 1 ,
] ) ;
update
局部更新
Copy // 更新main字段
$client -> update ( [
'index' => 'index' ,
'type' => 'fulltext' ,
'id' => 1 ,
'body' => [
'doc' => [
'main' => 123 ,
] ,
] ,
] ) ;
搜索
轻量搜索
Copy # 搜索last_name为smith的文档
$ret = $client -> search ( [
'index' => 'megacorp' ,
'type' => 'employee' ,
'q' => [ 'last_name:smith' ] ,
] ) ;
print_r ( $ret ) ;
相关性
在搜索rock climbing
词组时,可能返回两个文档,一个匹配rock climbing
另一个rock albums
,返回结果里面有_score
字段,即为相关性得分。
相关性得分
:es会查询跟目标有相关性的文档,但不一定完全和需要查询的文字一致,所以会有相关性分值表示文档和查询的关键词的相似度。
查询类型
math_phrase 短语搜索,精确匹配搜索短语。
高亮搜索
highlight
参数:
Copy // 高亮about字段
'body' => [
'highlight' => [
'fields' => [
'about' => new \ stdClass () , // 空对象占位以使用默认高亮样式
]
]
]