二、Elastic5.5.2安装中文分词器教程及简单测试

来源:互联网 发布:豆腐软件 编辑:程序博客网 时间:2024/05/17 22:29

一、下载分词器安装包

首先进入各版本下载页面,选择相应的版本进行下载(和自己安装的ElasticSearch版本保持一致)。我这里安装的是5.5.2的ElasticSearch,所以选择对应的5.5.2软件包。

右键·复制下载链接·,在Linux系统中使用wget命令下载

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.2/elasticsearch-analysis-ik-5.5.2.zip

这一步有时候会下载不成功,那么可以使用浏览器本地电脑下载完成后,使用工具上传到服务器目录中。

二、解压并安装

1.移动elasticsearch-analysis-ik-5.5.2.zip到安装目录的plugins目录

[king@localhost soft]$ mv elasticsearch-analysis-ik-5.5.2.zip /soft/elasticsearch-5.5.2/plugins/

2.进入安装目录的plugins目录

[king@localhost soft]$ cd /soft/elasticsearch-5.5.2/plugins/[king@localhost plugins]$ lselasticsearch-analysis-ik-5.5.2.zip  x-pack

3.解压

[king@localhost plugins]$ unzip elasticsearch-analysis-ik-5.5.2.zip [king@localhost plugins]$ lselasticsearch  elasticsearch-analysis-ik-5.5.2.zip  x-pack

4.删除压缩包(非必须)

[king@localhost plugins]$ rm -rf elasticsearch-analysis-ik-5.5.2.zip 

按照官方说明,这时已经成功安装了,重启ElasticSearch即可。

三、测试

1.创建一个索引

curl -XPUT http://localhost:9200/index

2.创建一个映射

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'{        "properties": {            "content": {                "type": "text",                "analyzer": "ik_max_word",                "search_analyzer": "ik_max_word"            }        }}'

3.索引一些文档,可以认为就是插入一些信息

curl -XPOST http://localhost:9200/index/fulltext/1 -d'{"content":"美国留给伊拉克的是个烂摊子吗"}'curl -XPOST http://localhost:9200/index/fulltext/2 -d'{"content":"公安部:各地校车将享最高路权"}'curl -XPOST http://localhost:9200/index/fulltext/3 -d'{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'curl -XPOST http://localhost:9200/index/fulltext/4 -d'{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

4.查询,结果以高亮显示

查询

curl -XPOST http://localhost:9200/index/fulltext/_search  -d'{    "query" : { "match" : { "content" : "中国" }},    "highlight" : {        "pre_tags" : ["<tag1>", "<tag2>"],        "post_tags" : ["</tag1>", "</tag2>"],        "fields" : {            "content" : {}        }    }}'

结果

{    "took": 14,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "failed": 0    },    "hits": {        "total": 2,        "max_score": 2,        "hits": [            {                "_index": "index",                "_type": "fulltext",                "_id": "4",                "_score": 2,                "_source": {                    "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"                },                "highlight": {                    "content": [                        "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "                    ]                }            },            {                "_index": "index",                "_type": "fulltext",                "_id": "3",                "_score": 2,                "_source": {                    "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"                },                "highlight": {                    "content": [                        "均每天扣1艘<tag1>中国</tag1>渔船 "                    ]                }            }        ]    }}