一、查看info索引下的所有数据
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/getting-started-search-API.html
curl -X GET “localhost:9200/info/_search” -H ‘Content-Type: application/json’ -d’
{
“query”: { “match_all”: {} }
}
’
返回:
{
"took":3,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":5,
"max_score":1,
"hits":[
{
"_index":"info",
"_type":"student",
"_id":"4zAsZGsBtdy_0nUkziP5",
"_score":1,
"_source":{
"query":{
"match_all":{
}
}
}
},
{
"_index":"info",
"_type":"student",
"_id":"2",
"_score":1,
"_source":{
"name":"harold",
"age":"24",
"hobby":"hiking"
}
},
{
"_index":"info",
"_type":"student",
"_id":"6",
"_score":1,
"_source":{
"name":"Chris Paul"
}
},
{
"_index":"info",
"_type":"student",
"_id":"1",
"_score":1,
"_source":{
"name":"hassan",
"age":"24",
"hobby":"basketball"
}
},
{
"_index":"info",
"_type":"student",
"_id":"3",
"_score":1,
"_source":{
"name":"Draymond"
}
}
]
}
}
这些文档正是我们在上一篇中插入成功那几个文档。只取我们需要的字段:
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/getting-started-search.html
只显示name和age:
curl -X GET “localhost:9200/info/_search” -H ‘Content-Type: application/json’ -d’
{
“query”: { “match_all”: {} },
“_source”: [“name”, “age”]
}
’
返回:
{
"took":13,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":5,
"max_score":1,
"hits":[
{
"_index":"info",
"_type":"student",
"_id":"4zAsZGsBtdy_0nUkziP5",
"_score":1,
"_source":{
}
},
{
"_index":"info",
"_type":"student",
"_id":"2",
"_score":1,
"_source":{
"name":"harold",
"age":"24"
}
},
{
"_index":"info",
"_type":"student",
"_id":"6",
"_score":1,
"_source":{
"name":"Chris Paul"
}
},
{
"_index":"info",
"_type":"student",
"_id":"1",
"_score":1,
"_source":{
"name":"hassan",
"age":"24"
}
},
{
"_index":"info",
"_type":"student",
"_id":"3",
"_score":1,
"_source":{
"name":"Draymond"
}
}
]
}
}
看起来还是返回了很多东西,只不过过滤掉了一些字段,如果在我们的文档field比较多的时候,这个api会很有用。
二、插叙索引为info,类型为student,id为1的数据
curl “localhost:9200/info/student/1?pretty”
三、查询结果按照age倒排
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/getting-started-search-API.html
https://blog.csdn.net/wild46cat/article/details/62889554
curl -X GET "localhost:9200/info/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "age.keyword": "desc" }
]
}
'
curl -X GET "localhost:9200/info/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"age": {
"gte": 25
}
}
}
}
}
}
'
五、查询24岁的学生信息
curl -X GET "localhost:9200/info/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "age": "24" } }
}
'