Elasticsearch 环境搭建
Elasticsearch 是基于lucene实现的搜索引擎,以restful接口和无模式json文档方式提供分布式、多租户全文检索引擎。本文简单介绍elasticsearch学习环境搭建,主要包括elasticsearch和kibana环境搭建,以及中文分词插件的安装。
安装elsticsearch
当前最新版本已经是7.2.0版本,本文采用7.1.1版本,下载地址为https://www.elastic.co/cn/downloads/past-releases#elasticsearch-oss,这里选择纯apache2.0协议的oss版本,避免一些功能不能使用。选择相应操作系统的链接,这里下载windows版本,下载文件为elasticsearch-oss-7.1.1-windows-x86_64.zip。
解压至你想要的文件夹,进入bin目录,启动命令为:elasticsearch.bat.
启动成功后,在浏览器中输入地址:localhost:9200,输出如下信息表示已经正常启动:
{
"name" : "PC-20151222PQYO",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Q1SudKMLT9KGNyyEDbjVQQ",
"version" : {
"number" : "7.1.1",
"build_flavor" : "oss",
"build_type" : "zip",
"build_hash" : "7a013de",
"build_date" : "2019-05-23T14:04:00.380842Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
下面介绍kibana的安装。
安装kibana
kibana是一个开源的可视化和分析工具。kibana可以对存储的数据进行搜索、查看、交互,然后以不同的图表、地图等方式进行展示。elasticsearch提供restful接口,使用kibana的开发工具可以非常方便测试elasticsearch的各种API。
下载与elasticsearch版本一直的kibana-oss版本,地址为:https://www.elastic.co/cn/downloads/past-releases#kibana-oss,选择7.1.1版本,选择对应操作系统的版本,本文选择windows版本。
下载文件为kibana-oss-7.1.1-windows-x86_64.zip,解压至目标文件夹,进入bin,启动命令为kibana.bat.
启动成功后,在浏览器中输入地址:localhost:5601,正常显示则启动成功。
点击开发工具,即可测试elasticsearch的API.
安装hanlp分词插件
Elasticsearch 默认对中文分词是按“字”进行分词的,当然不能达到理想的分词搜索要求。这里采用hanlp分词插件,其更新也很及时,安装配置简单。下载地址:https://github.com/KennFalcon/elasticsearch-analysis-hanlp。按照要求几步配置完毕:
a. 下载对应的release安装包
b. 将相关内容解压至ES_HOME/plugins/analysis-hanlp
c. 将插件中config目录下的文件移动至ES_HOME/config/analysis-hanlp
d. 解压出的data目录为词典目录
下面开始测试是否安装成功。
首先使用默认分词:
GET /_analyze?pretty
{
"text" : ["elasticsearch学习环境搭建"]
}
结果为:
{
"tokens" : [
{
"token" : "elasticsearch",
"start_offset" : 0,
"end_offset" : 13,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "学",
"start_offset" : 13,
"end_offset" : 14,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "习",
"start_offset" : 14,
"end_offset" : 15,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "环",
"start_offset" : 15,
"end_offset" : 16,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "境",
"start_offset" : 16,
"end_offset" : 17,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "搭",
"start_offset" : 17,
"end_offset" : 18,
"type" : "<IDEOGRAPHIC>",
"position" : 5
},
{
"token" : "建",
"start_offset" : 18,
"end_offset" : 19,
"type" : "<IDEOGRAPHIC>",
"position" : 6
}
]
}
使用hanlp插件测试:
GET /_analyze?pretty
{
"analyzer" : "hanlp",
"text" : ["elasticsearch学习环境搭建"]
}
返回结果:
{
"tokens" : [
{
"token" : "elasticsearch",
"start_offset" : 0,
"end_offset" : 13,
"type" : "nx",
"position" : 0
},
{
"token" : "学习",
"start_offset" : 13,
"end_offset" : 15,
"type" : "v",
"position" : 1
},
{
"token" : "环境",
"start_offset" : 15,
"end_offset" : 17,
"type" : "n",
"position" : 2
},
{
"token" : "搭建",
"start_offset" : 17,
"end_offset" : 19,
"type" : "v",
"position" : 3
}
]
}
两者对比,结果一目了然。
总结
本文介绍了elasticsearch环境搭建,包括kibana及hanlp分词插件的安装。
本文参考链接:https://blog.csdn.net/neweastsun/article/details/93755911