ubuntu 下 使用 elasticsearch5 同义词

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中文,汉语,汉字, 2211,221=>二居室, 3111=>一居室,一室 4321,三居室,

其中包含 => 的同义词是在 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

添加自定义词组到文件中

121122213二居室 41115一居室 63217三居室

安装/修改插件,需要重启 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 分词的规则

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0