Elasticsearch
1 介绍
1.1 什么是Elasticsearch
Elasticsearch 是位于 Elastic Stack 核心的分布式搜索和分析引擎。Logstash 和 Beats 有助于收集、聚合和丰富您的数据并将其存储在 Elasticsearch 中。Kibana 使您能够以交互方式探索、可视化和共享对数据的洞察,并管理和监控堆栈。Elasticsearch 是索引、搜索和分析魔法发生的地方。
Elasticsearch 为所有类型的数据提供近乎实时的搜索和分析。无论您拥有结构化或非结构化文本、数值数据还是地理空间数据,Elasticsearch 都可以以支持快速搜索的方式高效地存储和索引它。您可以超越简单的数据检索和聚合信息来发现数据中的趋势和模式。随着您的数据和查询量的增长,Elasticsearch 的分布式特性使您的部署能够随之无缝增长。
虽然并非所有问题都是搜索问题,但 Elasticsearch 提供了在各种用例中处理数据的速度和灵活性:
- 向应用程序或网站添加搜索框
- 存储和分析日志、指标和安全事件数据
- 使用机器学习实时自动建模数据的行为
- 使用 Elasticsearch 作为存储引擎自动化业务工作流
- 使用 Elasticsearch 作为地理信息系统 (GIS) 管理、集成和分析空间信息
- 使用 Elasticsearch 作为生物信息学研究工具存储和处理遗传数据
我们不断对人们使用搜索的新颖方式感到惊讶。但是,无论您的用例是否与其中之一类似,或者您正在使用 Elasticsearch 来解决新问题,您在 Elasticsearch 中处理数据、文档和索引的方式都是相同的。
1.2 Elasticsearch的几个重要概念
Elasticsearch有几个核心概念。从一开始理解这些概念会对整个学习过程有莫大的帮助。
1.2.1 接近实时(NRT)
Elasticsearch是一个接近实时的搜索平台。这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒)。
1.2.2 集群(cluster)
一个集群就是由一个或多个节点组织在一起,它们共同持有你整个的数据,并一起提供索引和搜索功能。一个集群由一个唯一的名字标识,这个名字默认就是“elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集群的名字,来加入这个集群。在产品环境中显式地设定这个名字是一个好习惯,但是使用默认值来进行测试/开发也是不错的。
1.2.3 节点(node)
一个节点是你集群中的一个服务器,作为集群的一部分,它存储你的数据,参与集群的索引和搜索功能。和集群类似,一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的,因为在这个管理过程中,你会去确定网络中的哪些服务器对应于Elasticsearch集群中的哪些节点。
一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫做“elasticsearch”的集群中,这意味着,如果你在你的网络中启动了若干个节点,并假定它们能够相互发现彼此,它们将会自动地形成并加入到一个叫做“elasticsearch”的集群中。
在一个集群里,只要你想,可以拥有任意多个节点。而且,如果当前你的网络中没有运行任何Elasticsearch节点,这时启动一个节点,会默认创建并加入一个叫做“elasticsearch”的集群。
1.2.4 索引(index)
一个索引就是一个拥有几分相似特征的文档的集合。
比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。索引类似于关系型数据库中Database的概念。
在一个集群中,如果你想,可以定义任意多的索引。
1.2.5 类型(type)—7.0版本后被移除
在一个索引中,你可以定义一种或多种类型。
一个类型是你的索引的一个逻辑上的分类/分区,其语义完全由你来定。通常,会为具有一组共同字段的文档定义一个类型。比如说,我们假设你运营一个博客平台并且将你所有的数据存储到一个索引中。在这个索引中,你可以为用户数据定义一个类型,为博客数据定义另一个类型,当然,也可以为评论数据定义另一个类型。类型类似于关系型数据库中Table的概念。
1.2.6 文档(document)
一个文档是一个可被索引的基础信息单元。
比如,你可以拥有某一个客户的文档,某一个产品的一个文档,当然,也可以拥有某个订单的一个文档。文档以JSON(Javascript Object Notation)格式来表示,而JSON是一个到处存在的互联网数据交互格式。
在一个index/type里面,只要你想,你可以存储任意多的文档。注意,尽管一个文档,物理上存在于一个索引之中,文档必须被索引/赋予一个索引的type。文档类似于关系型数据库中Record的概念。实际上一个文档除了用户定义的数据外,还包括_index
、_type
和_id
字段。
1.2.7 映射(Mapping) —7.0版本后被移除
Mapping
是ES中的一个很重要的内容,它类似于传统关系型数据中table的schema,用于定义一个索引(index)中的类型(type)的数据的结构。
在ES中,我们可以手动创建type(相当于table)和mapping(相关与schema),也可以采用默认创建方式。在默认配置下,ES可以根据插入的数据自动地创建type及其mapping。 mapping中主要包括字段名、字段数据类型和字段索引类型。
1.2.8 分片和复制(shards & replicas)
一个索引可以存储超出单个结点硬件限制的大量数据。比如,一个具有10亿文档的索引占据1TB的磁盘空间,而任一节点都没有这样大的磁盘空间;或者单个节点处理搜索请求,响应太慢。
为了解决这个问题,Elasticsearch提供了将索引划分成多份的能力,这些份就叫做分片。
当你创建一个索引的时候,你可以指定你想要的分片的数量。每个分片本身也是一个功能完善并且独立的“索引”,这个“索引”可以被放置到集群中的任何节点上。
分片之所以重要,主要有两方面的原因:
- 允许你水平分割/扩展你的内容容量
- 允许你在分片(潜在地,位于多个节点上)之上进行分布式的、并行的操作,进而提高性能/吞吐量
至于一个分片怎样分布,它的文档怎样聚合回搜索请求,是完全由Elasticsearch管理的,对于作为用户的你来说,这些都是透明的。
在一个网络/云的环境里,失败随时都可能发生,在某个分片/节点不知怎么的就处于离线状态,或者由于任何原因消失了。这种情况下,有一个故障转移机制是非常有用并且是强烈推荐的。为此目的,Elasticsearch允许你创建分片的一份或多份拷贝,这些拷贝叫做复制分片,或者直接叫复制。复制之所以重要,主要有两方面的原因:
- 在分片/节点失败的情况下,提供了高可用性。因为这个原因,注意到复制分片从不与原/主要(original/primary)分片置于同一节点上是非常重要的。
- 扩展你的搜索量/吞吐量,因为搜索可以在所有的复制上并行运行
总之,每个索引可以被分成多个分片。一个索引也可以被复制0次(意思是没有复制)或多次。一旦复制了,每个索引就有了主分片(作为复制源的原来的分片)和复制分片(主分片的拷贝)之别。分片和复制的数量可以在索引创建的时候指定。在索引创建之后,你可以在任何时候动态地改变复制数量,但是不能改变分片的数量。
默认情况下,Elasticsearch中的每个索引被分片5个主分片和1个复制,这意味着,如果你的集群中至少有两个节点,你的索引将会有5个主分片和另外5个复制分片(1个完全拷贝),这样的话每个索引总共就有10个分片。一个索引的多个分片可以存放在集群中的一台主机上,也可以存放在多台主机上,这取决于你的集群机器数量。主分片和复制分片的具体位置是由ES内在的策略所决定的。
以上部分内容转自Elasticsearch基础教程,并对其进行了补充。
1.3 配置文件
Elasticsearch 有三个配置文件:
elasticsearch.yml
用于配置 Elasticsearchjvm.options
用于配置 Elasticsearch JVM 设置log4j2.properties
用于配置 Elasticsearch 日志记录
1.3.1 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 -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#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.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#
# ---------------------------------- Security ----------------------------------
#
# *** WARNING ***
#
# Elasticsearch security features are not enabled by default.
# These features are free, but require configuration changes to enable them.
# This means that users don’t have to provide credentials and can get full access
# to the cluster. Network connections are also not encrypted.
#
# To protect your data, we strongly encourage you to enable the Elasticsearch security features.
# Refer to the following documentation for instructions.
#
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.html
(1)cluster.name: elasticsearch
(2)node.name: “Franz Kafka”
(3)node.master: true
(4)node.data: true
(5)index.number_of_shards: 5
(6)index.number_of_replicas:
(7)path.conf: /path/to/conf
(8)path.data:/path/to/data1,/path/to/data2
(9)path.work:/path/to/work
(10)path.logs: /path/to/logs
(11)path.plugins: /path/to/plugins
(12)bootstrap.mlockall: true
(13)network.bind_host: 192.168.0.1
(14)network.publish_host: 192.168.0.1
(15)network.host: 192.168.0.1
(16)transport.tcp.port: 9300
(17)transport.tcp.compress: true
(18)http.port: 9200
(19)http.max_content_length: 100mb
(20)http.enabled: false
(21)gateway.type: local
(22)gateway.recover_after_nodes: 1
(23)gateway.recover_after_time: 5m
(24)gateway.expected_nodes: 2
(25)cluster.routing.allocation.node_initial_primaries_recoveries: 4
(26)cluster.routing.allocation.node_concurrent_recoveries: 2
(27)indices.recovery.max_size_per_sec: 0
(28)indices.recovery.concurrent_streams: 5
(29)discovery.zen.minimum_master_nodes: 1
(30)discovery.zen.ping.timeout: 3s
(31)discovery.zen.ping.multicast.enabled: false
(32)discovery.zen.ping.unicast.hosts: [“host1”, “host2:port”, “host3 [portX-portY] “]
1.3.2 jvm.options配置文件
1.3.3 log4j2.properties 配置文件
2 安装使用
2.1 安装前准备
操作系统要求:https://www.elastic.co/cn/support/matrix
JVM要求:https://www.elastic.co/cn/support/matrix#matrix_jvm
2.2 安装7.16版本
Windows:
下载页面https://www.elastic.co/cn/downloads/elasticsearch
MacOS:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-darwin-x86_64.tar.gz
tar -xzvf elasticsearch-7.16.2-darwin-x86_64.tar.gz
cd elasticsearch-7.16.2
./bin/elasticsearch
Linux:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-linux-x86_64.tar.gz
tar -xzvf elasticsearch-7.16.2-linux-x86_64.tar.gz
cd elasticsearch-7.16.2
./bin/elasticsearch
root@asdas # curl http://127.0.0.1:9200
{
"name" : "DESKTOP-QCCNVAA",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "jvvb9b1DS2OoxGIjrgd8bw",
"version" : {
"number" : "7.16.3",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "4e6e4eab2297e949ec994e688dad46290d018022",
"build_date" : "2022-01-06T23:43:02.825887787Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
2.3 安装Kibana7.16版本
mac:
curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-7.16.2-darwin-x86_64.tar.gz
tar xzvf kibana-7.16.2-darwin-x86_64.tar.gz
cd kibana-7.16.2-darwin-x86_64/
./bin/kibana
Windows:
https://www.elastic.co/cn/downloads/kibana
2.4 安装ElasticHD
https://github.com/qax-os/ElasticHD/releases
3 入门级使用
3.1 简单的增删改查
#在accounts这个index下的person这个type创建了一个id为1的文档。文档内容为JSON定义的内容。
POST /accounts/person/1
{
"name":"John",
"lastname":"Doe",
"job_description":"Linux Developer"
}
#读取文档
GET accounts/person/1
#更新文档
PUT /accounts/person/1
{
"doc": {
"job_description":"english teacher"
}
}
#删除文档
DELETE /accounts/person/1
DELETE /accounts
3.2 高级查询
#搜索的高级操作
#添加数据
POST /people/student/3
{
"no":3,
"name":"Tom",
"lastname":"Doe",
"job_description":"Linux Developer"
}
#
#高级操作
#
#查询全部
GET /people/student/_search
{
"query": { "match_all": {} }
}
#查询个数
GET /people/student/_search
{
"query": { "match_all": {} },
"size": 1
}
#结果集排序
GET /people/student/_search
{
"query": {"match_all": {}},
"sort": [
{
"no": {
"order": "desc"
}
}
],
"size": 2,
"from": 1
}
#字段查询
GET /people/student/_search
{
"query": { "match_all": {} },
"_source": ["name", "lastname"]
}
#关键字查询 7.0版本后需要加上.keyword
GET /people/student/_search
{
"query": {
"term": {
"name.keyword": {
"value": "Tom"
}
}
}
}
#范围查询
GET /people/student/_search
{
"query": {
"range": {
"no": {
"gte": 2,
"lte": 3
}
}
}
}
#前缀查询 前缀需要用小写(不区分大小写)
GET /people/student/_search
{
"query": {
"prefix": {
"name": {
"value": "t"
}
}
}
}
#通配符查询 用小写(不区分大小写)
GET /people/student/_search
{
"query": {
"wildcard": {
"name": {
"value": "*o*"
}
}
}
}
#多ID查询
GET /people/student/_search
{
"query": {
"ids": {
"values": ["1","3"]
}
}
}
#模糊查询
GET /people/student/_search
{
"query": {
"fuzzy": {
"name":{
"value": "aom"
}
}
}
}
#全文查询
# 返回全部5条数据
GET /people/student/_search
{
"query": {
"match": {
"name": "son"
}
}
}
参考:https://www.cnblogs.com/lonely-wolf/p/14975414.html