前提,
Elasticsearch之curl創建索引庫
Elasticsearch之curl創建索引
Elasticsearch之curl創建索引庫和索引時注意事項
Elasticsearch之cur查詢索引
1、根據員工id查詢
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1'
{"_index":"zhouls","_type":"emp","_id":"1","_version":3,"found":true,"_source":{"name":"tom","age":25}}[hadoop@djt002 elasticsearch-2.4.3]$
[hadoop@djt002 elasticsearch-2.4.3]$
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"name" : "tom",
"age" : 25
}
}
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/2?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "2",
"found" : false
}
在任意的查詢字符串中添加pretty參數,es可以得到易於識別的json結果。查得到的結果,是在source里。
2、檢索文檔中的一部分,如果只需要顯示指定字段
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1?_source=name,age&pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"age" : 25,
"name" : "tom"
}
}
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1?_source=name&pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"name" : "tom"
}
}
[hadoop@djt002 elasticsearch-2.4.3]$
3、查詢指定索引庫指定類型所有數據
這里,指定emp類型。查詢它下的所有數據
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/_search?pretty'
{
"took" : 206, 代表消耗的時間,是206毫秒
"timed_out" : false, 代表是否超時,false代表沒有超時
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : { 是hits
"total" : 4,
"max_score" : 1.0, 打分,是1分
"hits" : [ { hits里面,還有hits,在這里面是一個數組
"_index" : "zhouls",
"_type" : "emp",
"_id" : "AVpdBaixcJQ8qYq5I_Es",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "AVpdAus-cJQ8qYq5I_Er",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "2_create",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
} ] 一共是4條數據
}
}
4、查詢指定索引庫所有數據
這里,指定zhouls索引庫。查詢它下的所有數據。一個索引庫下有很多種類型,我這里僅emp一種。自己可以新建多種出來進行測驗
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/_search?pretty'
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [ {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "AVpdBaixcJQ8qYq5I_Es",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "AVpdAus-cJQ8qYq5I_Er",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
}, {
"_index" : "zhouls",
"_type" : "emp",
"_id" : "2_create",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 25
}
} ]
}
}
總結:
結合自己的經驗,一般對於es的簡單查詢,用crul,對於es的復雜查詢,用java代碼去實現。