打赏

Elasticsearch之cur查询索引

 

前提,

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代码去实现。

posted @ 2017-02-20 20:05  大数据和AI躺过的坑  阅读(4991)  评论(0编辑  收藏  举报