elasticsearch入门

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: centos7 安装elasticsearch-6.4.2版本 elasticsearch-head plugin插件安装 elasticsearch的基本概念

elasticsearch
本文基于elasticsearch-6.4.2版本

安装

1、安装JDK

2、安装es

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

tar -zxvf elasticsearch-6.4.2.tar.gz;
mv elasticsearch-6.4.2 /usr/local;

#创建用户elasticsearch 有elasticsearch 不能使用root账户运行,必须先创建用户
useradd elasticsearch;
chown -R elasticsearch:elasticsearch  /usr/local/elasticsearch-6.4.2/ ;

#启动运行
su elasticsearch
cd /usr/local/elasticsearch-6.4.2;
sh ./bin/elasticsearch;

启动成功后,如下所示
image

3、安装head_plugin插件
elasticsearch-6.x版本不支持使用elasticsearch-plugin install mobz/elasticsearch-head的方式安装。
官方支持的plugin安装的方式的插件为
image

#安装head_plugin插件
wget  https://github.com/mobz/elasticsearch-head/archive/master.zip
yum install -y unzip;
unzip master.zip;
mv elasticsearch-head-master/ /usr/local/

#安装node
wget https://npm.taobao.org/mirrors/node/v10.11.0/node-v10.11.0-linux-x64.tar.gz
tar -zxvf node-v10.11.0-linux-x64.tar.gz
mv node-v10.11.0-linux-x64 /usr/local/

vi /etc/profile
#set node
NODE_HOME=/usr/local/node-v10.11.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
"/etc/profile" 89L, 2079C written

[root@k8s-master-101 ~]# source /etc/profile
[root@k8s-master-101 ~]# node -v
v10.11.0

#elasticsearch配置文件中授权
node.name: node-rcl-1
cluster.name: my-application-rcl
network.host: 192.168.31.101
http.cors.enabled: true
http.cors.allow-origin: "*"

启动过程中的几个问题:

1.max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
2.max number of threads [1024] for user [lishang] likely too low, increase to at least [4096]
3.max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

分别修改

vi /etc/security/limits.conf
elasticsearch hard nofile 65536
elasticsearch sorf nofile 65536

vi /etc/security/limits.d/20-nproc.conf 
* soft nproc 4096
* hard nproc 4096
elasticsearch soft nproc 4096
elasticsearch hard nproc 4096
#需要重启操作系统生效,使用ulimit -a 查看线程限制数

vi /etc/sysctl.conf 
vm.max_map_count=262144

4、启动ES、head-plugin

#启动es
cd /usr/local/elasticsearch-6.4.2
./bin/elasticsearch -d


#启动head-plugin
cd /usr/local/elasticsearch-head-master
npm run start

5、访问
image

ES的基本概念

群集

集群是一个或多个节点(服务器)的集合,它们共同保存您的整个数据,并提供跨所有节点的联合索引和搜索功能。群集由唯一名称标识,默认情况下为“elasticsearch”。此名称很重要,因为如果节点设置为按名称加入群集,则该节点只能是群集的一部分。

确保不要在不同的环境中重用相同的群集名称,否则最终会导致节点加入错误的群集。例如,您可以使用logging-dev,logging-stage以及logging-prod 用于开发,登台和生产集群。

请注意,如果群集中只有一个节点,那么它是完全正常的。此外,您还可以拥有多个独立的集群,每个集群都有自己唯一的集群名称。

节点

节点是作为群集一部分的单个服务器,存储数据并参与群集的索引和搜索功能。就像集群一样,节点由名称标识,默认情况下,该名称是在启动时分配给节点的随机通用唯一标识符(UUID)。如果不需要默认值,可以定义所需的任何节点名称。此名称对于管理目的非常重要,您可以在其中识别网络中哪些服务器与Elasticsearch集群中的哪些节点相对应。

可以将节点配置为按群集名称加入特定群集。默认情况下,每个节点都设置为加入一个名为cluster的集群elasticsearch,这意味着如果您在网络上启动了许多节点并且假设它们可以相互发现 - 它们将自动形成并加入一个名为的集群elasticsearch。

在单个群集中,您可以拥有任意数量的节点。此外,如果您的网络上当前没有其他Elasticsearch节点正在运行,则默认情况下启动单个节点将形成一个名为的新单节点集群elasticsearch。

索引

索引是具有某些类似特征的文档集合。例如,您可以拥有客户数据的索引,产品目录的另一个索引以及订单数据的另一个索引。索引由名称标识(必须全部小写),此名称用于在对其中的文档执行索引,搜索,更新和删除操作时引用索引。

在单个群集中,您可以根据需要定义任意数量的索引。

文档

文档是可以编制索引的基本信息单元。例如,您可以为单个客户提供文档,为单个产品提供另一个文档,为单个订单提供另一个文档。该文档以JSON(JavaScript Object Notation)表示,JSON是一种普遍存在的互联网数据交换格式。

在索引/类型中,您可以根据需要存储任意数量的文档。请注意,尽管文档实际上驻留在索引中,但实际上必须将文档编入索引/分配给索引中的类型。

碎片和副本

索引可能存储大量可能超过单个节点的硬件限制的数据。例如,占用1TB磁盘空间的十亿个文档的单个索引可能不适合单个节点的磁盘,或者可能太慢而无法单独从单个节点提供搜索请求。

为了解决这个问题,Elasticsearch提供了将索引细分为多个称为分片的功能。创建索引时,只需定义所需的分片数即可。每个分片本身都是一个功能齐全且独立的“索引”,可以托管在集群中的任何节点上。

分片很重要,主要有两个原因:

它允许您水平拆分/缩放内容量
它允许您跨分片(可能在多个节点上)分布和并行化操作,从而提高性能/吞吐量
分片的分布方式以及如何将其文档聚合回搜索请求的机制完全由Elasticsearch管理,对用户而言是透明的。

在可以随时发生故障的网络/云环境中,非常有用,强烈建议使用故障转移机制,以防分片/节点以某种方式脱机或因任何原因消失。为此,Elasticsearch允许您将索引的分片的一个或多个副本制作成所谓的副本分片或简称副本。

复制很重要,主要有两个原因:

它在碎片/节点发生故障时提供高可用性。因此,请务必注意,副本分片永远不会在与从中复制的原始/主分片相同的节点上分配。
它允许您扩展搜索量/吞吐量,因为可以在所有副本上并行执行搜索。
总而言之,每个索引可以拆分为多个分片。索引也可以复制为零(表示没有副本)或更多次。复制后,每个索引都将具有主分片(从中复制的原始分片)和副本分片(主分片的副本)。

可以在创建索引时为每个索引定义分片和副本的数量。创建索引后,您还可以随时动态更改副本数。您可以使用_shrink和_splitAPI 更改现有索引的分片数,但这不是一项简单的任务,并且预先计划正确数量的分片是最佳方法。

默认情况下,Elasticsearch中的每个索引都分配了5个主分片和1个副本,这意味着如果群集中至少有两个节点,则索引将包含5个主分片和另外5个副本分片(1个完整副本),总计为每个索引10个分片。

注意
每个Elasticsearch分片都是Lucene索引。单个Lucene索引中可以包含最大数量的文档。截止LUCENE-5843,限制是2,147,483,519(= Integer.MAX_VALUE - 128)文档。您可以使用_cat/shardsAPI 监控分片大小。

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html

索引管理

1、创建索引

PUT /index_name/

#参数配置
"settings": {
  "index": {
    "number_of_shards": "5",
    "number_of_replicas": "1"
  }
}
#number_of_shards为主分片数,默认为5;number_of_replicas为主分片对应的副本数,默认为1

2、mapping管理
创建索引myindex4,并添加类型log、添加字段year(字段类型为整形)
image

向已经存在的索引myindex3添加类型log、字段year(字段类型为整形)
image

2、删除索引

DELETE /my_index
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all

#禁止使用_all或者通配符*进行索引删除
action.destructive_requires_name: true

别名不能用于删除索引?

3、获取索引

GET /{myindex}/

4、打开、关闭索引

POST /{indexname}/_open
POST /{indexname}/_close

#settingscluster.indices.close.enable: false 默认为开启

5、索引别名
a、索引别名不能重复,也不能与索引名重复
b、可以对一个或者多个索引指定一个别名(多个索引可以对应一个别名);一个索引也可以添加多个别名
c、别名不能更新,只能删除后重建

GET /{myindex}/_alias/{alias}
GET /{myindex}/_alias/{*}
#添加索引
PUT /{myindex}/_alias/{myindexalias} 
#删除索引
DELETE /{myindex}/_alias/{alias}
POST /_aliases/
{
  "actions": [
    {
      "add": {
        "index": "myindex",
        "alias": "myindexalias"
      }
    },
    {
      "add": {
        "index": "myindex",
        "alias": "myindexalias2"
      }
    },
    {
      "add": {
        "index": "myindex2",
        "alias": "myindexalias"
      }
    },
    {
      "remove": {
        "index": "myindex",
        "alias": "myindexalias"
      }
    }
  ]
}

过滤索引别名

/*
"filter": {"term": {"year": 2018}}

#路由
"routing": ""
或者分开设置索引路由和查询路由
"index_routing": "2",
"search_routing": "1,2"

*/

6、获取、更新索引配置

GET /{index}/_settings/
#{index}的形式可以是:_all | *、xxx* | xxx,xxx,... 的形式

/**可以更新副本数量和添加分词(必须先关闭、添加、在打开索引)**/
PUT /{index}/_settings/

{"index": {"number_of_replicas": 4}}
{"analysis":{"analyzer":{"content":{"type":"custom","tokenizer":"whitespace"}}}}

2、elastic操作
2.1、集群状态监控

集群状态监控api如下所示
[root@localhost ~]# curl localhost:9200/_cat/
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

/_cat/health:集群状态
/_cat/thread_pool:线程池状态
/_cat/indices:索引状态
/_cat/nodes:节点状态

2.2、索引
创建索引

[root@localhost ~]# curl -XPUT localhost:9200/log?pretty 
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "log"
}

查询索引

curl localhost:9200/_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   log      zm0ho3xFR1KaPFwHWfsX3Q   5   1          0            0       810b           810b
yellow open   customer lpfXum5qQoi3Mj9PJZB8Ug   5   1          1            0        4kb            4kb

关于重做索引、删除索引,索引之间如何平滑切换,后面详细讲解

2.3、文档
创建文档

[root@localhost ~]# curl -XPUT localhost:9200/log/authlog/1?pretty -d '{"name":"rcl","detail":"blacklist"}'
{
  "_index" : "log",
  "_type" : "authlog",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}

更新文档

[root@localhost ~]# curl -XPUT localhost:9200/log/authlog/1?pretty -d '{"name":"amao","detail":"blacklist"}'
{
  "_index" : "log",
  "_type" : "authlog",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}

查询文档

[root@localhost ~]# curl localhost:9200/log/authlog/1?pretty      
{
  "_index" : "log",
  "_type" : "authlog",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "amao",
    "detail" : "blacklist"
  }
}

此处name已被更新为amao,有此可见更新是有效的
删除文档

[root@localhost ~]# curl -XDELETE localhost:9200/log/authlog/1?pretty 
{
  "found" : true,
  "_index" : "log",
  "_type" : "authlog",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
[root@localhost ~]# curl localhost:9200/log/authlog/1?pretty         
{
  "_index" : "log",
  "_type" : "authlog",
  "_id" : "1",
  "found" : false
}

2.4、DSL查询
示例

[root@localhost ~]# curl -XPOST localhost:9200/log/_search -d '{"query":{"match":{"name":"amao"}}}'     
    
{"took":38,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"log","_type":"authlog","_id":"1","_score":0.2876821,"_source":{"name":"amao","detail":"blacklist"}}]}}

kibana

1、安装

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.2-linux-x86_64.tar.gz
tar -zxvf kibana-6.4.2-linux-x86_64.tar.gz
mv kibana-6.4.2-linux-x86_64 /usr/local
cd /usr/local
chown -R elasticsearch:elasticsearch kibana-6.4.2-linux-x86_64
./bin/kibana

Filebeat

./filebeat -e -c filebeat.yml

filebeat.yml配置如下
cat filebeat.yml | grep -v '#' | grep -v '^$'

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/elastic-data/aaaaaa*
  fields:
    logtype: filelog
    logsource: log4j
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
output.logstash:
  hosts: ["192.168.31.101:5044"]

Logstash

1、安装

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.4.2.tar.gz
tar -zxvf logstash-6.4.2.tar.gz
mv logstash-6.4.2 /usr/local
cd /usr/local
chown -Relasticsearch:elasticsearch logstash-6.4.2


bin/logstash -f first-pipeline.conf --config.reload.automatic

first-pipeline.conf
在logstash home目录下新建first-pipeline.conf

# comments to describe your configuration.
input {
    beats {
        host => "192.168.31.101"
        port => "5044"
    }
}
# The filter part of this file is commented out to indicate that it is
# optional.
# filter {
#
# }
output {
    elasticsearch {
        hosts => ["192.168.31.101:9200"]
    }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
3月前
|
安全 Linux 开发工具
Elasticsearch 搜索入门技术之一
Elasticsearch 搜索入门技术之一
227 1
|
4月前
|
JSON 自然语言处理 数据库
数据库-ElasticSearch入门(索引、文档、查询)
数据库-ElasticSearch入门(索引、文档、查询)
285 0
|
7月前
|
监控 搜索推荐 Java
elasticsearch入门实战
elasticsearch入门实战
62 1
|
6月前
|
存储 关系型数据库 数据库
ElasticSearch深度解析入门篇:高效搜索解决方案的介绍与实战案例讲解,带你避坑
ElasticSearch深度解析入门篇:高效搜索解决方案的介绍与实战案例讲解,带你避坑
ElasticSearch深度解析入门篇:高效搜索解决方案的介绍与实战案例讲解,带你避坑
|
2月前
|
存储 关系型数据库 MySQL
ElasticSearch 入门
【2月更文挑战第7天】ElasticSearch 入门 简介 ElasticSearch 的基本概念 ElasticSearch 的查询流程 ElasticSearch 的更新流程
36 2
|
2月前
|
存储 自然语言处理 搜索推荐
ElasticSearch入门篇
ElasticSearch入门篇
|
6月前
|
存储 JSON 定位技术
Elasticsearch入门
Elasticsearch入门
|
7月前
|
存储 JSON 搜索推荐
Elasticsearch(一)基础入门
Elasticsearch 是一个实时的分布式搜索分析引擎, 它能让你以前所未有的速度和规模,去探索你的数据。 它被用作全文检索、结构化搜索、分析以及这三个功能的组合:
36 0
|
4月前
|
存储 Java 网络架构
Spring Data Elasticsearch基础入门详解
Spring Data Elasticsearch基础入门详解
119 0
|
4月前
|
存储 数据挖掘 Java
Elasticsearch基础入门与安装部署
Elasticsearch基础入门与安装部署
168 0

热门文章

最新文章