ElasticSearch初探之基本概念和CRUD(二)
2020-01-13 23:01
405 查看
-
基本概念简介
index:索引,相当于关系数据库中的数据库database
type:类型,类似关系数据库中table
document:文档,相当于表中行记录row
field:列,相当于table中的colum
shard:分片,每个index包含多个shard,每个shard是保存数据的lucene实例。shard又分为:primary shard(ps) 和replica shard(rs),即主分片和副本分片,默认ps= 5,type中的每个document只能存在其中一个ps以及和它对应的rs中,ps和它对应的rs不能在同一个节点内(容错考虑)。
- 一个索引创建后,primary shard不可修改,但为了集群的高可用性和便于横向扩展replica shard可以动态修改。
[code]PUT /users/_settings { "number_of_replicas" : 10 //修改副本分片数 }
-
基本操作举例
es是完成基于RESTful web接口规范,以下基于rest风格举例
查看es状态
[code][root@Eden666 ~]# curl -XGET 'http://localhost:9200/_cluster/health?pretty' { "cluster_name" : "elasticsearch", "status" : "yellow", //三种状态:green 完全可用,集群功能齐全,yellow:所有数据可用,但是有些副本尚未分配,red:部分数据有丢失 "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 5, //初始5个primary shard 分片 "active_shards" : 5, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 5, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 50.0 }
1、新增数据(POST),其中users就是index,coder是type,id就是field,若出现以下情况是因为es6.x版本需加入headers之Content-type(所有请求方式都一样),详情可参考官网(https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests)
[code][root@Eden666 ~]# curl -XPOST 'localhost:9200/users/coder/3?pretty=true' -d '{"name":"eden03","age":10}' { "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported", "status" : 406 } [root@Eden666 ~]# curl -H 'Content-type:application/json' -XPOST 'localhost:9200/users/coder/3?pretty=true' -d '{"name":"eden03","age":10}' { "_index" : "users", "_type" : "coder", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 2 }
2、修改(PUT)
[code][root@Eden666 ~]# curl -H 'Content-type:application/json' -XPUT 'localhost:9200/users/coder/3?pretty=true' -d '{"name":"eden03-new","age":100}' { "_index" : "users", "_type" : "coder", "_id" : "3", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 2 }
3、查看(GET)
[code][root@Eden666 ~]# curl -XGET 'localhost:9200/users/coder/3?pretty=true' { "_index" : "users", "_type" : "coder", "_id" : "3", "_version" : 2, "found" : true, "_source" : { "name" : "eden03-new", "age" : 100 } }
4、删除(DELETE),可以发现删除查询结果返回found=false
[code][root@Eden666 ~]# curl -XDELETE 'localhost:9200/users/coder/3?pretty=true' { "_index" : "users", "_type" : "coder", "_id" : "3", "_version" : 3, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 2 } [root@Eden666 ~]# curl localhost:9200/users/coder/3?pretty=true { "_index" : "users", "_type" : "coder", "_id" : "3", "found" : false }
5、其他检索方式,查询字符串方式和DSL(Domain Specific Language特定领域语言)方式
- 字符串查询:具有局限性,不够灵活
[code][root@Eden666 ~]# curl localhost:9200/users/coder/_search?pretty=true { "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "users", "_type" : "coder", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "eden02", "age" : 30 } }, { "_index" : "users", "_type" : "coder", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "eden01", "age" : 20 } } ] } } [root@Eden666 ~]# curl 'localhost:9200/users/coder/_search?q=name:eden01&pretty' { "took" : 14, //查询毫秒数 "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.2876821, "hits" : [ { "_index" : "users", "_type" : "coder", "_id" : "1", "_score" : 0.2876821, //检索的document的分数,分数越高越容易检索到 "_source" : { "name" : "eden01", "age" : 20 } } ] } }
- DSL方式:可以建立复杂查询条件。match:匹配的字段值,size:指定返回document数,from:位移量,默认是0
[code][root@Eden666 ~]# curl -H "Content-Type:application/json" -XGET 'localhost:9200/users/coder/_search?pretty' -d '{"query":{"match":{"name":"eden02"}},"size":1,"from":0}' { "took" : 9, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.2876821, "hits" : [ { "_index" : "users", "_type" : "coder", "_id" : "2", "_score" : 0.2876821, "_source" : { "name" : "eden02", "age" : 30 } } ] } }
- DSL查询之 or 和 and检索
[code]//or检索:"query":{"match":{"name":"eden02 eden01"}} [root@Eden666 ~]# curl -H "Content-Type:application/json" -XGET 'localhost:9200/users/coder/_search?pretty' -d '{"query":{"match":{"name":"eden02 eden01"}},"size":10,"from":0}' { "took" : 13, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.2876821, "hits" : [ { "_index" : "users", "_type" : "coder", "_id" : "2", "_score" : 0.2876821, "_source" : { "name" : "eden02", "age" : 30 } }, { "_index" : "users", "_type" : "coder", "_id" : "1", "_score" : 0.2876821, "_source" : { "name" : "eden01", "age" : 20 } } ] } } //and检索需使用到布尔查询 //{"bool": {"must": [ { "match": { "name": "eden01" } },{ "match": { "age": 20 } }]}} [root@Eden666 ~]# curl -H "Content-Type:application/json" -XGET 'localhost:9200/users/coder/_search?pretty' -d '{"query":{"bool": {"must": [ { "match": { "name": "eden01" } },{ "match": { "age": 20 } }]}},"size":10,"from":0}' { "took" : 19, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.287682, "hits" : [ { "_index" : "users", "_type" : "coder", "_id" : "1", "_score" : 1.287682, "_source" : { "name" : "eden01", "age" : 20 } } ] } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Elasticsearch系列---Elasticsearch的基本概念及工作原理
- ElasticSearch的基本概念
- elasticsearch的一些基本概念
- JavaScript初探系列之基本概念
- elasticsearch 开始 —— 基本概念
- OSGi基本概念初探
- Elasticsearch学习总结一 基本用法及概念
- lucene和ElasticSearch基本概念
- Elasticsearch(一) 基本概念
- ElasticSearch的安装部署及基本概念
- Elasticsearch 2.3.3基本概念介绍
- Elasticsearch基本概念介绍
- Elasticsearch上手——几个基本概念
- elasticsearch基本概念介绍
- elasticsearch笔记_基本概念(一)
- ElasticSearch 学习笔记二 基本概念
- ElasticSearch中Cluster,Node,Shard,Indices,replicas的基本概念与关系
- ElasticSearch5.2.2 基本概念和集群配置详解
- ElasticSearch学习-基本概念