Elasticsearch环境搭建和介绍(Windows)
1 Elasticsearch介绍和安装
1.1 简介
1.1.1 Elastic
Elastic官网:https://www.elastic.co/cn/
Elastic有一条完整的产品线:Elasticsearch、Kibana、Logstash等,前面说的三个就是大家常说的ELK技术栈。
1.1.2 Elasticsearch
Elasticsearch官网:https://www.elastic.co/cn/products/elasticsearch
如上所述,Elasticsearch具备以下特点:
- 分布式,无需人工搭建集群(solr就需要人为配置,使用Zookeeper作为注册中心)
- Restful风格,一切API都遵循Rest原则,容易上手
- 近实时搜索,数据更新在Elasticsearch中几乎是完全同步的。
1.1.3 版本
目前Elasticsearch最新的版本是6.4.2,我这里使用的版本是6.2.4
安装Elasticsearch前提条件:JDK1.8及以上
1.2 安装和配置
1.2.1 下载
下载地址:https://www.elastic.co/downloads/past-releases
1.2.2 安装
Elasticsearch无需安装,解压即用。
1.3 运行
进入elasticsearch/bin目录,可以看到下面的执行文件:
双击运行
可以看到绑定了两个端口:
- 9300:Java程序访问的端口
- 9200:浏览器、postman访问的端口
我们在浏览器中访问:http://127.0.0.1:9200
我在浏览器上安装了一个插件所显示的效果
看到了上面的信息,说明你的Elasticsearch已经安装成功了,但是为了方便我们开发的时候查看数据,我推荐安装一个Elasticsearch的客户端工具。下面我讲述的是如何安装Head插件。
1.4 安装Head插件
1.4.1 什么是Head
Ealsticsearch只是后端提供各种API,那么怎么直观的使用它呢?Elasticsearch-head将是一款专门针对于Elasticsearch的客户端工具
Elasticsearch-head配置包,下载地址:https://github.com/mobz/elasticsearch-head
1.4.2 安装
注意:es5以上版本安装head需要安装node和grunt
第一步:从地址:https://nodejs.org/en/download/ 下载相应系统的msi,双击安装。
第二步:安装完成用cmd进入安装目录执行 node -v可查看版本号
能看到版本号说明node安装成功
第三步:执行 npm install -g grunt-cli 安装grunt ,安装完成后执行grunt -version查看是否安装成功,会显示安装的版本号
1.4.3 配置运行
第一步:进入Elasticsearch安装目录下的config目录,修改elasticsearch.yml文件.在文件的末尾加入以下代码
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
然后去掉network.host: 192.168.0.1的注释并改为network.host: 0.0.0.0,去掉cluster.name;node.name;http.port的注释(也就是去掉#)去掉注释cluster.initial_master_nodes: ["node-1", "node-2"]
# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please consult the documentation for further information on configuration options: # https://www.elastic.co/guide/en/elasticsearch/reference/index.html # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: my-application # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node-1 # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # #path.data: /path/to/data # # Path to log files: # #path.logs: /path/to/logs # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # # Make sure that the heap size is set to about half the memory available # on the system and that the owner of the process is allowed to use this # limit. # # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # #network.host: 192.168.0.1 network.host: 0.0.0.0 # # Set a custom port for HTTP: # http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # #discovery.seed_hosts: ["host1", "host2"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # cluster.initial_master_nodes: ["node-1", "node-2"] # # For more information, consult the discovery and cluster formation module documentation. # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # #gateway.recover_after_nodes: 3 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true
第二步:双击elasticsearch.bat重启Elasticsearch
第三步:在https://github.com/mobz/elasticsearch-head中下载head插件,选择下载zip
第四步:解压到指定文件夹下,D:\environment\elasticsearch-head-master 进入该文件夹,修改D:\environment\elasticsearch-head-master\Gruntfile.js 在对应的位置加上hostname:’*’、
第五步:打开cmd命令行窗口 ,在D:\environment\elasticsearch-head-master 下执行npm install 安装
,完成后执行grunt server 或者npm run start 运行head插件,如果运行不成功建议重新安装grunt。成功如下
1.4.4 成功
打开浏览器访问:http://127.0.0.1:9100
1.5 安装Ik分词器
ElasticSearch 默认采用的分词器, 是单个字分词 ,效果很差 ,所以我们需要安装一个更实用的分词器,这里采用IK分词器
搜索【IK Analyzer 3.0】
http://www.oschina.net/news/2660
Lucene的IK分词器早在2012年已经没有维护了,现在我们要使用的是在其基础上维护升级的版本,并且开发为Elasticsearch的集成插件了,与Elasticsearch一起维护升级,版本也保持一致,最新版本:6.4.2
1.5.1 下载
注意:你的Elasticsearch和IK分词器必须版本统一
源码下载地址:https://github.com/medcl/elasticsearch-analysis-ik/tree/6.2.x
jar包下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
1.5.2 安装
无需安装,解压即可使用
我们将其改名为ik
,并复制到Elasticsearch的解压目录,如下图所示
然后重启elasticsearch:
Ik分词器安装成功。
1.5.3 IK扩展词和停用词的简单介绍
扩展词和停用词文件:
1.5.4 测试
OK,到这里Elasticsearch环境搭建就成功了