Elasticsearch 7:使用 from 、size 进行分页查询


#Elasticsearch 笔记


创建索引:

PUT student
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      },
      "age" : {
        "type" : "integer"
      }
    }
  }
}

使用 _bulk 创建文档

POST _bulk
{ "index" : { "_index" : "student", "_id" : "1" } }
{ "name" : "张三", "age": 12}
{ "index" : { "_index" : "student", "_id" : "2" } }
{ "name" : "李四", "age": 10 }
{ "index" : { "_index" : "student", "_id" : "3" } }
{ "name" : "王五", "age": 11 }
{ "index" : { "_index" : "student", "_id" : "4" } }
{ "name" : "陈六", "age": 11 }

使用 from、size 时,一般会先排序。

示例1:按照 _id 升序排序,取第0个数据:

GET student/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_id": {"order" : "asc"}
    }
  ],
  "from": 0,
  "size": 1
}

示例2:按照 _id 升序排序,取第1~2个数据:

GET student/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_id": {"order" : "asc"}
    }
  ],
  "from": 1,
  "size": 2
}

示例3:按照 _id 降序排序,取第0个数据:

GET student/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_id": {"order" : "desc"}
    }
  ],
  "from": 0,
  "size": 1
}


( 本文完 )