全文搜索引擎Elasticsearch

Elasticsearch简介

Elasticsearch是一个基于Apache Lucene(TM)的开源、分布式、RESTful接口的全文搜索引擎。Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,Elasticsearch通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
Github地址:https://github.com/elastic/elasticsearch
官方网站:https://www.elastic.co/products/elasticsearch
英文文档:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
中文社区:https://elasticsearch.cn/

Elasticsearch特点:

1.速度快:可以在极短的时间内存储、搜索和分析大量的数据。通常作为具有复杂搜索场景情况下的核心发动机。
2.可拓展性强:Elasticsearch还是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,Elasticsearch能够横向扩展至数以百计的服务器存储以及处理PB级的数据。
3.弹性,高度可用:Elasticsearch检测到故障,以确保集群和数据安全可用。通过跨集群复制,辅助集群可以作为热备份运行。
4.灵活性:适用于多种数据类型

Elasticsearch下载与安装

下载地址:https://www.elastic.co/downloads/elasticsearch
根据自己的操作系统进行选择

image.png

下载完解压即可
bin目录中双击elasticsearch.bat即可

image.png

Elasticsearch启动监听两个端口,9300和9200
9300端口是使用tcp客户端连接使用的端口
9200端口是通过http协议连接Elasticsearch使用的端口
读取的配置文件是config目录下的elasticsearch.yml

image.png

建议注册成系统服务,执行命令elasticsearch-service.bat install

配置文件目录config

image.png

elasticsearch.yml 主配置文件
jvm.options jvm参数配置文件
log4j2.properties 日志配置文件

主配置文件elasticsearch.yml内容
# ======================== 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
#
# 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 new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery 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
Cluster设置集群的

cluster.name集群的名称,用于区分不同的集群,系统默认为elasitcsearch

Node主要是设置节点

node.name节点名称
node.attr.rack指定节点的部落属性,这是一个比集群更大的范围
node.master是否允许作为主节点,默认值为true
node.data是否存储数据,即存储索引片段,默认值为true

Paths关于数据和日志的存放路径的,这两个设置十分重要,因为比如要进行版本升级,如果程序与数据分离,将非常容易实现。程序的崩溃也不影响数据。

path.data数据存储位置
path.logs日志文件的路径
path.work临时文件位置
path.plugins插件安装位置

Memory内存设置

bootstrap.memory_lock启动后是否锁定内存,提高Elasticsearch的性能

Network有关网络的设置,比如RESTful接口,包括curl、浏览器、Kibana等HTTP连接过来的,都是通过这里设置

network.host设置绑定的ip地址,可以是ipv4ipv6的,默认本地回环
http.port设置对外服务的http端口,默认为9200

Discovery设置集群的节点之间的连接的

discovery.zen.ping.unicast.hosts设置集群内节点的主机
discovery.zen.minimum_master_nodes设置一个集群中主节点的数量该值可在2-4之间
discovery.zen.ping.timeout设置ping其他节点时的超时时间
discovery.zen.ping.multicast.enabled禁止当前节点发现多个集群节点,默认值为true

Gateway网关,支持多种类型的Gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。

gateway.recover_after_nodes设置集群中N个节点启动时进行数据恢复,默认为1
gateway.recover_after_time设置初始化恢复过程的超时时间
gateway.expected_nodes设置该集群中可存在的节点上限
gateway.type网关允许在所有集群重启后持有集群状态,集群状态的变更都会被保存下来,当第一次启用集群时,可以从网关中读取到状态,默认网关类型(也是推荐的)是local

Various其他

action.destructive_requires_name 在删除索引时,是否需要明确指定名称,该值为false时,将可以通过正则或_all进行索引 删除
node.max_local_storage_nodes在一个系统上禁用启动多个节点

详细可以参考官方说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules.html

启动完在浏览器中输入http://localhost:9200/

image.png

看到Elasticsearch的版本信息即启动成功

Elasticsearch的可视化界面

插件模式:推荐安装Elasticsearch的head插件

前提:安装好Node.js和配置好Npm,可以参考:https://www.jianshu.com/p/96f2f01a4f3e
插件前端代码的Github地址:https://github.com/mobz/elasticsearch-head
1.把代码从Github拉到本地
2.用npm对elasticsearch-head进行打包:npm install
3.安装grunt包:npm install –g grunt–cli

image.png

如果发现grunt不是内部命令的,在环境变量path中加入node_global文件夹的路径

image.png

4.在Elasticsearch配置文件elasticsearch.yml中加入

http.cors.enabled: true
http.cors.allow-origin: "*"

5.用grunt命令启动elasticsearch-head插件:grunt server

image.png

在浏览器访问http://localhost:9100/
image.png

注:如果显示没连接可以点击连接

官方工具:Elasticsearch常用可视化管理工具kibana

简介:

Kibana是一个为Elasticsearch平台分析和可视化的开源平台

特点:

1.通过 Kibana,能够对 Elasticsearch 中的数据进行可视化并在 Elastic Stack 进行操作
2.一张图片胜过千万行日志,Kibana 能够自由地选择如何呈现数据。Kibana 核心产品搭载了一批经典功能:柱状图、线状图、饼图、旭日图,等等。不仅如此,还可以使用 Vega 语法来设计独属于自己的可视化图形。所有这些都利用 Elasticsearch 的完整聚合功能。
3.将地理数据融入任何地图,利用 Elastic Maps Service 来实现地理空间数据的可视化,或者发挥创意,在地图上实现自定义位置数据的可视化。
4.时间序列,借助精选的时序性 UI,对 Elasticsearch 中的数据执行高级时间序列分析。也可以利用功能强大、简单易学的表达式来描述查询、转换和可视化图形
4.数据分析:凭借搜索引擎的相关性功能,结合 Graph 关联分析,揭示 Elasticsearch 数据中极其常见的关系
5.异常情况分析:借助非监督型 Machine Learning 功能来检测隐藏在 Elasticsearch 数据中的异常情况并探索那些对它们有显著影响的属性。
6.通过 Canvas,发挥无限创意: Canvas 能够基于实时数据发挥无限创意,而且此功能还支持 SQL

Github地址:https://github.com/elastic/kibana
官方网站:https://www.elastic.co/products/kibana
官方文档:https://www.elastic.co/guide/en/kibana/current/index.html

下载地址:https://www.elastic.co/downloads/kibana
根据自己的操作系统进行选择

image.png

下载完解压,到bin目录双击kibana.bat启动kibana

image.png

监听端口为5601,在浏览器中访问http://localhost:5601

image.png

进行集群健康检查

需要使用 _cat API,在Kibana控制台中运行命令 GET /_cat/health?v

image.png

可以看到名为 elasticsearch 的集群已经处于绿色状态。
每当我们查询集群健康情况时,接口可能会返回greenyellowred状态
green意味着一切良好(集群所有的功能都正常)。
yellow意味着所有的数据都是可用的,但是一些复制分片可能没有正确分发(集群的所有功能还是正常的)
red意味着因为某些原因导致有些数据不能使用。注意,即使集群状态是red,它仍然可以运行一部分的功能

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354