美文网首页
Elasticsearch的简单概念和使用

Elasticsearch的简单概念和使用

作者: 傻疯子 | 来源:发表于2021-12-20 17:27 被阅读0次

Elasticsearch简称es,是能实现海量数据的快速搜索的分布式引擎。

基本概念

es存储的结构可以分为index、type、document、field

index是索引库,用来存放一类数据或一种业务。
type在最新版本中已经取消。
document表示某一条数据。
field表示某个字段。

其次es还有一些重要概念

Cluster是集群,有一个主节点去负责管理集群的状态,节点的发现和删除,分片的状态和副本的状态。另外对于外部而言连接集群任何一个节点都是等价的。
Shard分片,可以把一个索引库水平拆到多个节点上,以进行分布式搜索。
Replica是副本,一个是用来提高容错性,一个是用来提高查询效率,实现负载均衡。
Recovery是恢复,在有节点变动时会进行重新负载均衡。

rest接口简单命令

查看所有节点

curl -X GET 'http://localhost:9200/_nodes/_all?pretty'

查看节点健康状态

curl -X GET 'http://sdc03.sefonsoft.com:9200/_cat/health?v'

查看所有索引库

curl -X GET 'http://localhost:9200/_cat/indices?v'

建立索引库

curl -X PUT 'localhost:9200/sfz'

删除索引库

curl -X DELETE 'localhost:9200/sfz'

添加数据

//指定索引
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/person/_doc/1' -d '{"name":"sfz","age":18}'
//不指定索引
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/person/_doc' -d '{"name":"sfz","age":19}'

查看数据

//查看指定索引数据
curl -XGET 'http://localhost:9200/person/_doc/1?pretty'
//查看所有数据
curl -XGET 'http://localhost:9200/person/_search?pretty'
//指定字段
curl -XGET 'http://localhost:9200/person/_doc/1?_source=name,age&pretty'
curl -XGET 'http://localhost:9200/person/_search?_source=name,age&pretty'

更新数据

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/person/_doc/1/_update' -d '{"doc":{"age":25}}'

删除数据

curl -XDELETE 'http://localhost:9200/person/_doc/1'

批量操作

curl -H "Content-Type: application/json"  -XPUT 'http://localhost:9200/person/_doc/_bulk' --data-binary @request

需要实现创建好request文件

{ "index" : { "_index" : "person", "_type" : "_doc", "_id" : "1" } }
{ "name" : "sfz1" }
{ "index" : { "_index" : "person", "_type" : "_doc", "_id" : "2" } }
{ "name" : "sfz2" }
{ "delete" : { "_index" : "person", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "person", "_type" : "_doc", "_id" : "3" } }
{ "name" : "sfz3" }
{ "update" : {"_index" : "person", "_type" : "_doc","_id" : "1" } }
{ "doc" : {"age" : "100"} }

其中index和create代表插入数据,但creata数据已经存在时无法执行,delete是删除,update时更新。
然后再指定索引库,type类型以及索引。
最好再非删除操作时放入数据。
大概如下形式:

{ "action" : { "_index" : "index_name", "_type" : "_doc", "_id" : "index_id" } }
{ "field_name" : "data_content" }

相关文章

网友评论

      本文标题:Elasticsearch的简单概念和使用

      本文链接:https://www.haomeiwen.com/subject/wmjzfrtx.html