-
基本概念簡(jiǎn)介
index:索引巧勤,相當(dāng)于關(guān)系數(shù)據(jù)庫(kù)中的數(shù)據(jù)庫(kù)database
type:類型粘室,類似關(guān)系數(shù)據(jù)庫(kù)中table
document:文檔缺厉,相當(dāng)于表中行記錄row
field:列栓辜,相當(dāng)于table中的colum
shard:分片恋拍,每個(gè)index包含多個(gè)shard,每個(gè)shard是保存數(shù)據(jù)的lucene實(shí)例啃憎。shard又分為:primary shard(ps) 和replica shard(rs),即主分片和副本分片似炎,默認(rèn)ps= 5辛萍,type中的每個(gè)document只能存在其中一個(gè)ps以及和它對(duì)應(yīng)的rs中,ps和它對(duì)應(yīng)的rs不能在同一個(gè)節(jié)點(diǎn)內(nèi)(容錯(cuò)考慮)羡藐。
- 一個(gè)索引創(chuàng)建后贩毕,primary shard不可修改,但為了集群的高可用性和便于橫向擴(kuò)展replica shard可以動(dòng)態(tài)修改仆嗦。
PUT /users/_settings
{
"number_of_replicas" : 10 //修改副本分片數(shù)
}
-
基本操作舉例
es是完成基于RESTful web接口規(guī)范辉阶,以下基于rest風(fēng)格舉例
查看es狀態(tài)
[root@Eden666 ~]# curl -XGET 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "elasticsearch",
"status" : "yellow", //三種狀態(tài):green 完全可用,集群功能齊全,yellow:所有數(shù)據(jù)可用谆甜,但是有些副本尚未分配垃僚,red:部分?jǐn)?shù)據(jù)有丟失
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 5, //初始5個(gè)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、新增數(shù)據(jù)(POST)规辱,其中users就是index谆棺,coder是type,id就是field罕袋,若出現(xiàn)以下情況是因?yàn)閑s6.x版本需加入headers之Content-type(所有請(qǐng)求方式都一樣)改淑,詳情可參考官網(wǎng)(https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests)
[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)
[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)
[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),可以發(fā)現(xiàn)刪除查詢結(jié)果返回found=false
[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特定領(lǐng)域語(yǔ)言)方式
- 字符串查詢:具有局限性,不夠靈活
[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, //查詢毫秒數(shù)
"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的分?jǐn)?shù)掠河,分?jǐn)?shù)越高越容易檢索到
"_source" : {
"name" : "eden01",
"age" : 20
}
}
]
}
}
- DSL方式:可以建立復(fù)雜查詢條件亮元。match:匹配的字段值,size:指定返回document數(shù)唠摹,from:位移量爆捞,默認(rèn)是0
[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檢索
//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
}
}
]
}
}