【Elasticsearch】检索排序 & 分页

检索排序 & 分页

1.测试数据准备

首先,我们创建一个名为 blog_posts 的索引,并插入一些测试数据:

json 复制代码
PUT /blog_posts
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "author": { "type": "keyword" },
      "views": { "type": "integer" },
      "publish_date": { "type": "date" },
      "tags": { "type": "keyword" }
    }
  }
}
json 复制代码
POST /blog_posts/_bulk
{"index":{}}
{"title":"Elasticsearch Basics","content":"Learn the basics of Elasticsearch and how to perform simple queries.","author":"John Doe","views":1500,"publish_date":"2023-01-15","tags":["search","database"]}
{"index":{}}
{"title":"Advanced Search Techniques","content":"Explore advanced search techniques in Elasticsearch including aggregations and filters.","author":"Jane Smith","views":3200,"publish_date":"2023-02-20","tags":["search","advanced"]}
{"index":{}}
{"title":"Data Analytics with ELK","content":"How to use the ELK stack for data analytics and visualization.","author":"John Doe","views":2800,"publish_date":"2023-03-10","tags":["analytics","elk"]}
{"index":{}}
{"title":"Elasticsearch Performance Tuning","content":"Tips and tricks for optimizing Elasticsearch performance in production environments.","author":"Mike Johnson","views":4200,"publish_date":"2023-04-05","tags":["performance","optimization"]}
{"index":{}}
{"title":"Kibana Dashboard Guide","content":"Creating effective dashboards in Kibana for monitoring and analysis.","author":"Jane Smith","views":1900,"publish_date":"2023-05-12","tags":["kibana","visualization"]}

2.排序功能

能排序的字段都具备正排索引,单 text 类型字段是不可以排序的。如果要使 text 字段支持排序、聚合,则需要开启 fielddata

sort 是和 query 平级的,并不会被 query 包含。

2.1 简单字段排序

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "views": {
        "order": "desc"
      }
    }
  ]
}

2.2 多字段排序

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "author": {
        "order": "asc"
      }
    },
    {
      "views": {
        "order": "desc"
      }
    }
  ]
}

2.3 日期排序

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "publish_date": {
        "order": "desc"
      }
    }
  ]
}

3.分页功能

Elasticsearch 支持对查询结果进行分页处理,允许用户逐步获取和浏览大量数据。

在编写查询语句时,可通过再请求体中添加 fromsize 字段实现分页。from 表示结果集的起始位置,而 size 表示每页返回的文档数量。

如果将 from 设置为 11 11 11,size 设置为 5 5 5,则返回的是第 10 10 10 ~ 14 14 14 条数据(默认从第 0 0 0 条开始)。

3.1 基础分页

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2,
  "sort": [
    {
      "publish_date": {
        "order": "desc"
      }
    }
  ]
}

3.2 深度分页(不推荐大数据量使用)

深度分页 指的是在 Elasticsearch 中查询结果集 非常靠后的页码(例如第 1000 1000 1000 页,每页 10 10 10 条数据,即 from=10000)。它通常表现为使用 from + size 参数组合来获取远端的分页数据。

❌ 不推荐的详细原因可参考我的另一篇博客:《【Elasticsearch】深度分页及其替代方案》。

当然,我们这里测试的数据没有那么多。

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "from": 3,
  "size": 2
}

3.3 使用 search_after 进行高效分页

首先获取第一页:

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "sort": [
    {
      "views": {
        "order": "desc"
      }
    },
    {
      "_id": {
        "order": "asc"
      }
    }
  ]
}

然后使用最后一个结果的排序值获取下一页:

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "search_after": [3200, "上一页最后一个文档的ID"],
  "sort": [
    {
      "views": {
        "order": "desc"
      }
    },
    {
      "_id": {
        "order": "asc"
      }
    }
  ]
}


4.综合示例:高亮+排序+分页

json 复制代码
GET /blog_posts/_search
{
  "query": {
    "multi_match": {
      "query": "search",
      "fields": ["title", "content"]
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {
        "fragment_size": 100,
        "number_of_fragments": 2
      }
    }
  },
  "sort": [
    {
      "views": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 3
}

5.实践建议

功能 实践建议
高亮 * 对于大文本字段,限制 fragment_sizenumber_of_fragments 以提高性能。 * 考虑使用 require_field_match: true 来只高亮查询中指定的字段。
排序 * 对于文本字段排序,使用 .keyword 子字段或设置 fielddata: true。 * 避免对未索引或分析的字段进行排序。 * 对于分页场景,使用包含唯一值的排序条件(如 _id)。
分页 * 避免深度分页(超过 1000 1000 1000 条记录)),使用 search_after 代替。 * 对于无限滚动等场景,优先考虑 search_after 而不是 from/size。 * 考虑使用滚动 API(Scroll API)对于大数据量导出场景。
相关推荐
leo_messi941 天前
2026版商城项目(一)
java·elasticsearch·k8s·springcloud
RoboWizard1 天前
本地AI主机批量部署 高效存储支撑全场景配置
大数据·人工智能
dingzd951 天前
产品同质化严重如何用材质升级做出溢价空间
大数据·人工智能·跨境电商·内容营销
@PHARAOH1 天前
WHAT - AI 时代下的候选人
大数据·前端·人工智能
Data-Miner1 天前
50页精品PPT | 数据安全运营体系建设方案
大数据·数据分析
weixin_436182421 天前
PLC 与 DCS 国产化报告获取:工控产业情报查找指南
大数据·人工智能·国产plc
金智维科技官方1 天前
制造业如何用Ki-AgentS智能体平台实现设备巡检自动化?
大数据·运维·人工智能
志栋智能1 天前
告别高昂投入:超自动化IT运维的轻量化实践
大数据·运维·网络·人工智能·自动化
汉克老师1 天前
GESP2026年3月认证C++五级( 第三部分编程题(2)找数)
c++·排序·双指针·二分算法·gesp5级·gesp五级
腾视科技TENSORTEC1 天前
腾视科技TS-SG-SM7系列AI算力模组:32TOPS算力引擎,开启边缘智能新纪元
大数据·人工智能·科技·ai·ai算力模组·ai模组·ainas