elasticsearch:查看文档(三)

本文介绍了Elasticsearch的相关操作,包括查看info索引下的所有数据,可只取所需字段;插叙索引为info、类型为student、id为1的数据;还提及查询结果按age倒排,以及查询大于25岁和24岁的学生信息等内容。

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

一、查看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" }
  ]
}
'

四、大于25岁的学生
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/getting-started-filters.html

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" } }
}
'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值