Elasticsearch之cur查询索引

本文介绍了使用curl命令针对Elasticsearch进行查询的具体方法,包括按ID查询文档、检索指定字段及查询索引库的所有数据等操作,并分享了作者的实际经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/6421241.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值