Elasticsearch学习笔记一

 Elasticsearch

  Elasticsearch(如下简称ES)是一款Java语言开发的基于Lucene的高效全文搜索引擎。它提供了一个分布式多用户能力的基于RESTful web接口的全文搜索和分析服务,并做为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,可以实现实时搜索,能够搜索日志或者交易数据,用来分析商业趋势、搜集日志、分析系统瓶颈或者运行发展等等,能够提供预警功能(持续的查询分析某个数据,若是超过必定的值,就进行警告),分析商业信息,在百万级的大数据中轻松的定位关键信息,等等..html

  PS:Lucene不是一个完整的全文索引应用,而是一个用Java语言开发的全文索引引擎工具包,它能够方便的嵌入到各类应用中实现针对应用的全文索引/检索功能。详见Lucene:基于Java的全文检索引擎简介node

基本概念 

  想了解ES首先就要弄清楚下面的几个概念,这样能够更加方便的学习ES,也不会对ES产生一些误解:web

  近实时(NRT)数据库

  ES并非一个标准的数据库,它不像MongoDB,它侧重于对存储的数据进行搜索。所以要注意到它 不是 实时读写 的,这也就意味着,刚刚存储的数据,并不能立刻查询到。固然这里还要区分查询的方式,ES也有数据的查询以及搜索,这里的近实时强调的是搜索....json

  集群(Cluster)bootstrap

  在ES中,对用户来讲集群是很透明的。你只须要指定一个集群的名字(默认是elasticsearch),启动的时候,凡是集群是这个名字的,都会默认加入到一个集群中。你不须要作任何操做,选举或者管理都是自动完成的。服务器

  注意,若是群集中只有一个节点,那么它是彻底正常的。此外,您还能够拥有多个独立的集群,每一个集群都有本身惟一的集群名称。网络

  节点(Node)app

  节点是做为群集一部分的单个服务器,存储数据并参与群集的索引和搜索功能。就像集群同样,节点由名称标识,默认状况下,该名称是在启动时分配给节点的随机通用惟一标识符(UUID)。能够将节点配置为按群集名称加入特定群集。默认状况下,每一个节点都设置为加入一个名为cluster的集群elasticsearch,这意味着若是您在网络上启动了许多节点而且假设它们能够相互发现 - 它们将自动造成并加入一个名为的集群elasticsearchcurl

  注意,在单个群集中,您能够拥有任意数量的节点。此外,若是您的网络上当前没有其余Elasticsearch节点正在运行,则默认状况下启动单个节点将造成一个名为的新单节点集群elasticsearch

  索引(Index) 

  索引是具备某些相似特征的文档集合。索引由名称标识(必须所有小写),此名称用于在对其中的文档执行索引,搜索,更新和删除操做时引用索引。在单个群集中,您能够根据须要定义任意数量的索引。

  类型(Type)

  类型能够理解成一个索引的逻辑分区,用于标识不一样的文档字段信息的集合。可是因为ES仍是以索引为粗粒度的单位,所以一个索引下的全部的类型,都存放在一个索引下。这也就致使不一样类型相同字段名字的字段会存在类型定义冲突的问题。在6.0.0中已弃用。

  文档(Document)

  文档是存储数据信息的基本单元,使用json来表示。在索引/类型中,您能够根据须要存储任意数量的文档。

  注意,尽管文档实际上驻留在索引中,但实际上必须将文档编入索引/分配给索引中的类型。

  分片与副本(Share & Replicas)

  在ES中,索引会备份成分片,每一个分片是独立的lucene索引,能够完成搜索分析存储等工做。建立索引时,只需定义所需的分片数便可。每一个分片自己都是一个功能齐全且独立的“索引”,能够托管在集群中的任何节点上。在能够随时发生故障的网络/云环境中,很是有用,强烈建议使用故障转移机制,以防分片/节点以某种方式脱机或因任何缘由消失。为此,Elasticsearch容许您将索引的分片的一个或多个副本制做成所谓的副本分片或简称副本。

  总而言之,每一个索引能够拆分为多个分片。索引也能够复制为零(表示没有副本)或更屡次。复制后,每一个索引都将具备主分片(从中复制的原始分片)和副本分片(主分片的副本)。能够在建立索引时为每一个索引定义分片和副本的数量。建立索引后,您能够随时动态更改副本数,但不能在过后更改分片数。默认状况下,Elasticsearch中的每一个索引都分配了5个主分片和1个副本,这意味着若是群集中至少有两个节点,则索引将包含5个主分片和另外5个副本分片(1个完整副本),总计为每一个索引10个分片。

安装

  ES在开发整合时至少须要Java 8,因此在安装以前ES以前,须要确保安装了Java 8 并配置好环境变量。这里我很少介绍,能够查看我往期的博客Linux服务部署--Java(一)

  本文介绍的是单机版,Linux环境经常使用的wget下载elasticsearch-6.0.1.tar.gz,其余环境或方式能够参考官方文档

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.tar.gz

解压

tar -xvf elasticsearch-6.0.1.tar.gz

启动

cd  /elasticsearch-6.0.1
./bin/elasticsearch

若是这时报错"max virtual memory areas vm.maxmapcount [65530] is too low",运行下面的命令。

sudo sysctl -w vm.max_map_count=262144

修改集群和节点名称

./elasticsearch -Ecluster.name=my_cluster -Enode.name=my_node

默认状况下,Elasticsearch使用port 9200来提供对其REST API的访问。启动完成后,打开另外一个命令行窗口,请求该端口,会获得说明信息。

curl localhost:9200

{
  "name" : "my_node",
  "cluster_name" : "my_cluster",
  "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA",
  "version" : {
    "number" : "6.0.1",
    "build_hash" : "19c13d0",
    "build_date" : "2018-10-24T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

PS:默认状况下,ES 只容许本机访问,若是须要远程访问,能够修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改为0.0.0.0,让任何人均可以访问(线上环境别这么设置哦),而后从新启动 ES。固然,也能够在这里修改集群和节点名称。

# ======================== 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_cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node 节点名称:
#
node.name: my_node
#
# 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)绑定的Ip地址:
#
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 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: 3
#
# 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

固然,ES启动会出现各类各样的错误,好比内存分配不足,没法使用root用户启动,启动直接Killed等等..能够查看Elasticsearch启动错误 和 Centos7下避坑安装elasticsearch

 参考资料

相关文章
相关标签/搜索