1. 安装最新版 elasticsearch
参考: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/deb.html
依次执行以下命令
1wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - 2 3sudo apt-get install apt-transport-https 4 5echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list 6 7sudo apt-get update && sudo apt-get install elasticsearch
查看自己系统用的是 SysV 还是 systemd
ps -p 1
我用的是 systemd 执行
1sudo /bin/systemctl daemon-reload 2sudo /bin/systemctl enable elasticsearch.service
2. 启动 elasticsearch
sudo service elasticsearch start
3. 检查是否安装成功
curl -XGET 'localhost:9200/?pretty'
如果返回结果类似以下内容,说明安装成功
1{ 2 "name" : "Cp8oag6", 3 "cluster_name" : "elasticsearch", 4 "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA", 5 "version" : { 6 "number" : "5.2.2", 7 "build_hash" : "f27399d", 8 "build_date" : "2016-03-30T09:51:41.449Z", 9 "build_snapshot" : false, 10 "lucene_version" : "6.4.1" 11 }, 12 "tagline" : "You Know, for Search" 13}
4. 配置同义词
elasticsearch5 的默认目录是 /etc/elasticsearch,所以在 /etc/elasticsearch 目录下创建同义词文件
edit /etc/elasticsearch/analysis/synonyms.txt
输入同义词(每行一组),注意文件必须要是 utf-8 格式,英文逗号
1中文,汉语,汉字, 22室1厅1卫,2室2厅1卫=>二居室, 31室1厅1卫=>一居室,一室 43室2厅1卫,三居室,
其中包含 => 的同义词是在 analysis 时只生成 => 后面的词,应该比较省内存,不知道缺点是什么.
5. 安装分词插件
elasticsearch 默认是没有汉语词组的,所以要用这个插件
https://github.com/medcl/elasticsearch-analysis-ik/releases
下载和 elasticsearch 匹配的版本,解压缩到 elasticsearch 的 plugins 目录下的 ik 文件夹中,比如我的是 /usr/share/elasticsearch/plugins/ik
6. 配置自定义词组
我们用到的词语有些是 ik 中已有的,比如 中文/汉语/汉字,有些是没有的,比如 一居室/二居室,没有的就要自己配置了
gedit /usr/share/elasticsearch/plugins/ik/config/custom/mydict.dic
添加自定义词组到文件中
12室1厅1卫 22室2厅1卫 3二居室 41室1厅1卫 5一居室 63室2厅1卫 7三居室
安装/修改插件,需要重启 elasticsearch 才会生效
1sudo service elasticsearch stop 2sudo service elasticsearch start
7. 检测自定义分词是否有效
curl -XGET 'http://localhost:9200/gj/_analyze?pretty&analyzer=by_smart' -d '{"text":"一居室"}
8. 创建 index
准备工作已经做好了,现在可以在代码中使用同义词了,这里用 php 演示一下
1 $client = ClientBuilder::create()->build(); 2 3 // 创建 index 4 $settings = json_decode('{ 5 "analysis": { 6 "analyzer": { 7 "by_smart": { 8 "type": "custom", 9 "tokenizer": "ik_smart", 10 "filter": [ 11 "by_tfr", 12 "by_sfr" 13 ], 14 "char_filter": [ 15 "by_cfr" 16 ] 17 }, 18 "by_max_word": { 19 "type": "custom", 20 "tokenizer": "ik_max_word", 21 "filter": [ 22 "by_tfr", 23 "by_sfr" 24 ], 25 "char_filter": [ 26 "by_cfr" 27 ] 28 } 29 }, 30 "filter": { 31 "by_tfr": { 32 "type": "stop", 33 "stopwords": [ 34 " " 35 ] 36 }, 37 "by_sfr": { 38 "type": "synonym", 39 "synonyms_path": "analysis/synonyms.txt" 40 } 41 }, 42 "char_filter": { 43 "by_cfr": { 44 "type": "mapping", 45 "mappings": [ 46 "| => |" 47 ] 48 } 49 } 50 } 51 }'); 52 53 $mappings = json_decode('{ 54 "_default_":{ 55 "properties": { 56 "shoujia": { 57 "type":"double" 58 } 59 } 60 }, 61 "xinfang":{ 62 "_source":{ 63 "enabled":true 64 }, 65 "properties":{ 66 "huxing": { 67 "type": "text", 68 "index": "analyzed", 69 "analyzer": "by_max_word", 70 "search_analyzer": "by_smart" 71 } 72 } 73 } 74 }'); 75 76 $params = [ 77 'index'=>'gj', 78 'body'=>[ 79 'settings'=>$settings, 80 'mappings'=>$mappings 81 ] 82 ]; 83 $client->indices()->create($params);
8. 名词解释
只用了几天 elasticsearch,懂的地方不多,所以这些只是片面的理解.
_index 类似数据库中的 schema 名字
_type 类似数据表
properties 表中的字段
mappings 配置字段的分词规则(比如我们的ik分词),类型(integer,double,string,text等等)
analysis 分词的规则