全文检索 - 3、ElasticSearch使用

上一章 全文检索 - 2、ElasticSearch安装(head插件、ik分词器安装)

一、 基本概念

索引 index
Elasticsearch的数据存储的地方,类似于关系型数据库中的数据库,可以向索引写入文档或从索引中读取文档。

文档 doc
文档是ElasticSearch中的主要实体,对ElasticSearch来说所有的搜索都可归结为对文档的搜索。文档由字段构成,每个字段有它的名字和一个或多个值。文档直接可能有不通的字段集合,并且文档没有固定的模式或强制的结构。文档存储的内容是一个JSON对象。

映射 mapping
映射即元数据信息,可以通过映射设置字段的类型、以及分词方式、格式化等信息。虽然ElasticSearch可以根据写入的内容自动添加映射信息,但大多数情况下还是需要手动设置字段的相关配置。

类型 type
ElasticSearch每个文档由对应的类型,允许一个索引中存储多中类型的文档(6.X一个索引只能创建一个类型),并为不同类型的文档提供不同映射。(ElasticSearch7.X已经去除该概念)。

节点 node
单个ElasticSearch服务实例成为节点,大多时候一个ElasticSearch已经满足简单应用,如果考虑集群容错和数据增长快,需要配置多节点ElasticSearch集群。

集群 cluster
当数据量或数据查询压力超过单机负载时,需要多个节点协同处理,这些节点成为集群。集群同时也是无间断提供服务的一种解决方案,及时节点中有机器宕机或者服务不可用,也不会影响ElasticSearch服务使用。

分片 shard
ElasticSearch将数据分散到多个物理Lucene索引上,这些索引被称为分片,而散布这些分片的过程叫分片处理(sharding)。ElasticSearch会自动分片。分片数量是在创建索引时定义好的,不能修改。

副本 replica
为每个分片创建冗余的副本,当访问压力时单机无法处理所有请求时,处理查询时可以把这些副本当做最初的主分片。即使某个分片所在节点宕机,ElasticSearch也可以使用其副本,从而不会造成数据丢失,支持随意时间添加或删除副本。

二、 CURL命令使用

1、cat命令

cat查询集群相关信息,命令如下:

/_cat/aliases            #查看集群中所有alias信息,路由配置等
/_cat/aliases/{alias}       #查看指定索引的alias信息
/_cat/allocation       #查看单节点的分片信息
/_cat/count            #查看文档数量
/_cat/count/{index}      #查看某个索引的文档数量
/_cat/fielddata          #查看当前集群各个节点的fielddata内存使用情况
/_cat/fielddata/{fields}     #查看某个属性的内存使用情况
/_cat/health            #查看集群状态
/_cat/indices          #查看所有索引的信息
/_cat/indices/{index}    #查看某个索引的信息
/_cat/master          #查看主节点信息
/_cat/nodeattrs           #查看单节点的自定义属性
/_cat/nodes           #查看所有节点信息
/_cat/pending_tasks      #查看当前集群的pending task
/_cat/plugins            #查看集群插件信息
/_cat/recovery          #查看正在进行和已完成的分片恢復的信息
/_cat/recovery/{index}   #查看某个索引正在进行和已完成的分片恢復的信息
/_cat/repositories         #输出集群中注册快照存储库
/_cat/segments        #查看各索引的segment信息
/_cat/segments/{index}  #查看某个索引的segment信息
/_cat/shards          #查看各分片的信息
/_cat/shards/{index}    #查看某个分片的信息
/_cat/templates          #输出当前正在存在的模板信息
/_cat/thread_pool        #查看集群线程池

常用cat命令:
1)、查看集群健康状态
curl -XGET 'http://192.168.3.27:8201/_cat/health?v&pretty'
返回

epoch      timestamp cluster    status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1591950304 08:25:04  es-cluster green           2         2     14   7    0    0        0             0                  -                100.0%

2)、查看节点信息
curl -XGET 'http://192.168.3.27:8201/_cat/nodes?v&pretty'
返回

ip          heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.3.7           19          28   0    0.00    0.01     0.05 dilm      -      master
192.168.3.8           49          45   0    0.00    0.02     0.05 dilm      *      node-1

3)、查看所有索引
curl -XGET 'http://192.168.3.27:8201/_cat/indices?v&pretty'
返回

health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   test_index          BIzNghzyQEqPlidrUlRkdw   2   1          0            0       920b           460b

4)、查看某个索引信息
curl -XGET 'http://192.168.3.27:8201/_cat/indices/test_index?v&pretty'
返回

health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   test_index BIzNghzyQEqPlidrUlRkdw   2   1          0            0       920b           460b

5)、查看某个索引的文档数量
curl -XGET 'http://192.168.3.27:8201/_cat/count/test_index?v&pretty'
返回

epoch      timestamp count
1591949968 08:19:28   0

2、index相关命令

1)、单独创建索引

curl -XPUT 'http://192.168.3.27:8201/test_index?pretty' \
    -H "Content-Type: application/json" \
    -d '{"settings":{"number_of_shards":2,"number_of_replicas":1}}'

返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

number_of_shards分片数 number_of_replicas备份数

2)、索引添加结构映射

curl -XPOST 'http://192.168.3.27:8201/test_index/_mapping?pretty' \
-H "Content-Type: application/json" \
-d '{"properties":{"content":{"type":"text"},"name":{"type": "keyword"},"record_date": {"type": "date"}}}'

返回

{
 "acknowledged" : true
}

3)、创建索引同时设置映射结构

curl -XPUT 'http://192.168.3.27:8201/test_index?pretty' \
    -H "Content-Type: application/json" \
   -d '{"settings":{"number_of_shards":2,"number_of_replicas":1},"mappings":{"properties":{"content":{"type":"text"},"name":{"type": "keyword"},"record_date": {"type": "date"}}}}'

返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

4)、查看索引详细信息
curl -XGET 'http://192.168.3.27:8201/test_index/_stats?pretty'

5)、删除索引
curl -XDELETE 'http://192.168.3.27:8201/test_index'
返回

{"acknowledged":true}

3、数据增删改查

1)、新增

  • post新增
    post新增不需要指定ID,会随机生成一个ID,不是幂等性接口,相同数据新增时会添加多条,只是ID不相同。
curl -XPOST 'http://192.168.3.27:8201/test_index/_doc?pretty' \
	-H "Content-Type: application/json" \
	-d '{"content":"我是中国人","name":"张三","record_date":"2020-06-12T16:40:00+08:00"}'

返回

 {
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "rpS4p3IBe0MfklyGKsRO",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
  • put新增
    put新增需要指定ID,具有幂等性。
curl -XPUT 'http://192.168.3.27:8201/test_index/_doc/1?pretty' \
    -H "Content-Type: application/json" \
    -d '{"content":"我是英国人","name":"ada","record_date":"2020-06-12T16:41:00+08:00"}'

返回

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

2)、查询
curl -XGET 'http://192.168.3.27:8201/test_index/_doc/1?pretty'
返回

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "content" : "我是英国人",
    "name" : "ada",
    "record_date" : "2020-06-12T16:41:00+08:00"
  }
}

3)、修改

curl -XPUT 'http://192.168.3.27:8201/test_index/_doc/1?pretty' \
    -H "Content-Type: application/json" \
    -d '{"content":"我是美国人","name":"ada","record_date":"2020-06-12T16:46:00+08:00"}'

返回

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

再查询
curl -XGET 'http://192.168.3.27:8201/test_index/_doc/1?pretty'

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "content" : "我是美国人",
    "name" : "ada",
    "record_date" : "2020-06-12T16:46:00+08:00"
  }
}

4)、删除
curl -XDELETE 'http://192.168.3.27:8201/test_index/_doc/1?pretty'
返回

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

再查询
curl -XGET 'http://192.168.3.27:8201/test_index/_doc/1?pretty'

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "found" : false
}

三、 head插件使用

浏览器输入:http://192.168.3.27:9100/打开head插件

1、连接ES
填入ES某个节点的IP和端口信息
在这里插入图片描述

2、查看索引状态和映射分片等信息
在这里插入图片描述

3、索引操作,可以刷新、关闭、删除索引
在这里插入图片描述

4、新建索引
在这里插入图片描述

5、查看索引数据,选中索引名可以查看不同索引的数据
在这里插入图片描述

6、基础查询
在这里插入图片描述

7、高级查询
选择不同http请求,可以用CURL中所有查询方法,例如添加数据
在这里插入图片描述
下一章 全文检索 - 4、检索接口开发(SpringBoot

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值