ubuntu 下 使用 elasticsearch5 同义词

前端之家收集整理的这篇文章主要介绍了ubuntu 下 使用 elasticsearch5 同义词前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 安装最新版 elasticsearch

参考:https://www.elastic.co/guide/en/elasticsearch/reference/5.2/deb.html

依次执行以下命令

  1. wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
  1. sudo apt-get install apt-transport-https
  1. echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list
  1. sudo apt-get update && sudo apt-get install elasticsearch

查看自己系统用的是 SysV 还是 systemd

  1. ps -p 1

我用的是systemd 执行

  1. sudo /bin/systemctl daemon-reload
  2. sudo /bin/systemctl enable elasticsearch.service

2. 启动elasticsearch

  1. sudo service elasticsearch start

3. 检查是否安装成功

  1. curl -XGET 'localhost:9200/?pretty'

如果返回结果类似以下内容,说明安装成功

  1. {
  2. "name" : "Cp8oag6","cluster_name" : "elasticsearch","cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA","version" : {
  3. "number" : "5.2.2","build_hash" : "f27399d","build_date" : "2016-03-30T09:51:41.449Z","build_snapshot" : false,"lucene_version" : "6.4.1"
  4. },"tagline" : "You Know,for Search"
  5. }

4. 配置同义词

elasticsearch5 的默认目录是/etc/elasticsearch,所以在/etc/elasticsearch 目录下创建同义词文件

  1. edit /etc/elasticsearch/analysis/synonyms.txt

输入同义词(每行一组),注意文件必须要是 utf-8 格式,英文逗号

  1. 中文,汉语,汉字,211卫,221卫=>二居室,111卫=>一居室,一室
  2. 321卫,三居室,

其中包含 => 的同义词是在analysis 时只生成 => 后面的词,应该比较省内存,不知道缺点是什么.

5. 安装分词插件

elasticsearch 默认是没有汉语词组的,所以要用这个插件

https://github.com/medcl/elasticsearch-analysis-ik/releases

下载和elasticsearch 匹配的版本,解压缩到elasticsearch 的 plugins 目录下的 ik 文件夹中,比如我的是/usr/share/elasticsearch/plugins/ik

6. 配置自定义词组

我们用到的词语有些是 ik 中已有的,比如 中文/汉语/汉字,有些是没有的,比如 一居室/二居室,没有的就要自己配置了

  1. gedit /usr/share/elasticsearch/plugins/ik/config/custom/mydict.dic

添加自定义词组到文件

  1. 211
  2. 221
  3. 二居室
  4. 111
  5. 一居室
  6. 321
  7. 三居室

安装/修改插件,需要重启elasticsearch 才会生效

  1. sudo service elasticsearch stop
  2. sudo service elasticsearch start

7. 检测自定义分词是否有效

  1. 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","tokenizer": "ik_smart","filter": [
  9. "by_tfr","by_sfr"
  10. ],"char_filter": [
  11. "by_cfr"
  12. ]
  13. },"by_max_word": {
  14. "type": "custom","tokenizer": "ik_max_word","char_filter": [
  15. "by_cfr"
  16. ]
  17. }
  18. },"filter": {
  19. "by_tfr": {
  20. "type": "stop","stopwords": [
  21. " "
  22. ]
  23. },"by_sfr": {
  24. "type": "synonym","synonyms_path": "analysis/synonyms.txt"
  25. }
  26. },"char_filter": {
  27. "by_cfr": {
  28. "type": "mapping","mappings": [
  29. "| => |"
  30. ]
  31. }
  32. }
  33. }
  34. }');
  35.  
  36. $mappings = json_decode('{
  37. "_default_":{
  38. "properties": {
  39. "shoujia": {
  40. "type":"double"
  41. }
  42. }
  43. },"xinfang":{
  44. "_source":{
  45. "enabled":true
  46. },"properties":{
  47. "huxing": {
  48. "type": "text","index": "analyzed","analyzer": "by_max_word","search_analyzer": "by_smart"
  49. }
  50. }
  51. }
  52. }');
  53.  
  54. $params = [
  55. 'index'=>'gj','body'=>[
  56. 'settings'=>$settings,'mappings'=>$mappings
  57. ]
  58. ];
  59. $client->indices()->create($params);

8. 名词解释

只用了几天elasticsearch,懂的地方不多,所以这些只是片面的理解.

_index 类似数据库中的 schema 名字

_type 类似数据表

properties 表中的字段

mappings 配置字段的分词规则(比如我们的ik分词),类型(integer,double,string,text等等)

analysis 分词的规则

猜你在找的Ubuntu相关文章