ElasticSearch十大概念

916 阅读4分钟

ElasticSearch十大概念

1. 集群(Cluster)

一个或者多个安装了es节点的服务器组织在一起,就是集群,这些节点共同持有数据,共同提供搜索服务. 一个集群有一个名字,这个名字是集群的唯一标识,该名字成为cluster name,默认的集群名称是elasticsearch,具有相同名称的节点才会组成一个集群. 可以在config/elasticsearch.yml文件中配置集群名称:

cluster.name: es_001

在集群中,节点的状态有这三种:绿色,黄色,红色:

  • 绿色:节点运行状态为健康状态.所有的主分片和副分片都可以正常工作.
  • 黄色:标识节点运行状态为警告状态,所有的主分片目前都可以直接运行,但是至少有一个副本分片是不能正常工作的
  • 红色:表示集群无法正常工作

2. 节点(Node)

集群中的一个服务器就是一个节点,节点中会存储数据,同时参与集群的索引以及搜索功能.一个节点想要加入一个集群,只需要配置一下集群名即可.默认情况下,如果我们启动了多个节点,多个节点能够互相发现彼此,自动组成一个集群,但这种方式并不可靠,有可能会发生脑裂现象.所以实际生产中,要手动配置一下集群信息,配置如下

cluster.name: esxince
node.name: esxince_node1
discovery.seed_hosts: ["10.25.68.52:3901","10.25.68.52:3902","10.25.68.52:3903"]

3. 索引(Index)

  • 名词 具有相似特征文档的集合
  • 动词 索引数据以及对数据进行索引操作

4. 类型(Type)

类型是索引上的逻辑分类或者分区.在es6之前,一个索引中可以有多个类型,从es7开始,一个索引中,只能有一个类型

5. 文档(Document)

一个可以被索引的数据单元.都是JSON格式的

6. 分片(Shards)

索引都是存储在节点上的,但是受限于节点的空间大小以及数据处理能力,单个节点的处理效果可能不理想,此时我们可以对索引进行分片.当我们创建一个索引的时候,就需要指定分片的数量.每个分片本身也是一个功能完善并且独立的索引.

7. 副本(Replicas)

副本就是对主分片的一个备份

8. Settings

集群中对索引的定义信息,例如索引的分片数,副本数等等

9. Mapping

Mapping保存了定义索引字段的存储类型,分词方式,是否存储等信息

10. Analyzer

字段分词方式的定义

ElasticSeasrch vs 关系型数据库

关系型数据库ElasticSearch
数据库索引
类型
文档
字段
表结构Mapping
SQLDSL
select * from xxxGET http://
update xxx set xx=xxPUT http://
Delete xxxDELETE http://
索引全文索引
root@ubuntu:/data/es_2901/plugins/analysis-ik# curl -XPUT http://221.228.85.54:2901/index
{"acknowledged":true,"shards_acknowledged":true,"index":"index"}root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# curl -XPOST http://221.228.85.54:2901/index/_mapping -H 'Content-Type:application/json' -d'{
>         "properties": {
>             "content": {
>                 "type": "text",
>                 "analyzer": "ik_max_word",
>                 "search_analyzer": "ik_smart"
>             }
>         }
> 
> }'
{"acknowledged":true}root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# curl -XPOST http://221.228.85.54:2901/index/_create/1 -H 'Content-Type:application/json' -d'{       
        "content": "美国留给伊拉克的是个烂摊子吗"}'          
{"_index":"index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1}root@ubunt
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# curl -XPOST http://221.228.85.54:2901/index/_create/2 -H 'Content-Type:application/json' -d'{ 
        "content": "年轻人你耗子为汁"}'
{"_index":"index","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":1,"_primary_term":1}root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# 
root@ubuntu:/data/es_2901/plugins/analysis-ik# curl -XPOST http://221.228.85.54:2901/index/_analyze -H 'Content-Type:application/json' -d'{
        "analyzer": "ik_smart","text":"美国留给中国的是个狗东西吗"}'
{"tokens":[{"token":"美国","start_offset":0,"end_offset":2,"type":"CN_WORD","position":0},{"token":"留给","start_offset":2,"end_offset":4,"type":"CN_WORD","position":1},{"token":"中国","start_offset":4,"end_offset":6,"type":"CN_WORD","position":2},{"token":"的","start_offset":6,"end_offset":7,"type":"CN_CHAR","position":3},{"token":"是","start_offset":7,"end_offset":8,"type":"CN_CHAR","position":4},{"token":"个","start_offset":8,"end_offset":9,"type":"CN_CHAR","position":5},{"token":"狗东西","start_offset":9,"end_offset":12,"type":"CN_WORD","position":6},{"token":"吗","start_offset":12,"end_offset":13,"type":"CN_CHAR","position":7}]}root@ubuntu:/data/es_2901/plugins/analysis-ik# 
{"tokens":[{"token":"美国","start_offset":0,"end_offset":2,"type":"CN_WORD","position":0},{"token":"留给","start_offset":2,"end_offset":4,"type":"CN_WORD","position":1},{"token":"中国","start_offset":4,"end_offset":6,"type":"CN_WORD","position":2},{"token":"的","start_offset":6,"end_offset":7,"type":"CN_CHAR","position":3},{"token":"是","start_offset":7,"end_offset":8,"type":"CN_CHAR","position":4},{"token":"个","start_offset":8,"end_offset":9,"type":"CN_CHAR","position":5},{"token":"狗东西","start_offset":9,"end_offset":12,"type":"CN_WORD","position":6},{"token":"吗","start_offset":12,"end_offset":13,"type":"CN_CHAR","position":7}]}