Elasticsearch 核心技术(九):搜索结果处理(分页、排序、指定返回字段、去重、高亮显示)

news/2024/10/25 15:34:22/

在这里插入图片描述

❤️ 博客主页:水滴技术
🚀 支持水滴:点赞👍 + 收藏⭐ + 留言💬
🌸 订阅专栏:大数据核心技术从入门到精通

文章目录

  • 一、分页
    • 1.1 示例:查询第 1 页,每页大小为 5
    • 1.2 示例:查询第 2 页,每页大小为 5
    • 1.3 示例:查询第 3 页,每页大小为 5
  • 二、排序
    • 2.1 示例:按 id 正序排序
    • 2.2 示例:按 id 倒序排序
  • 三、指定返回字段
    • 3.1 示例:只返回 id 和 name 字段
  • 四、去重
    • 4.1 示例:根据 store_id 字段去重
  • 五、高亮显示
    • 5.1 示例:将 name 字段做高亮显示
  • 系列文章
  • 热门专栏


大家好,我是水滴~~

本篇主要讲述 Elasticsearch 关于搜索结果的处理,主要内容有:分页查询、结果排序、指定返回字段、去重、高亮显示等。

一、分页

Elasticsearch 对所有查询都进行了分页,默认返回前 10 条数据。如果想要查询更多的数据,就需要使用分页参数了。Elasticsearch 提供了两个参数 fromsize 来控制分页结果。

  • from 表示从第几条数据开始查询,默认值为 0

  • size 表示返回数据量大小,默认值为 10

注:分页也有一定的限制,不能查询 from + size 超过 10000 的数据。

1.1 示例:查询第 1 页,每页大小为 5

GET /mt_product/_search
{"query": {"match_all": {}},"from": 0,"size": 5
}

查询结果:

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : 1,"name" : "招牌海苔单人餐","tags" : ["寿司"],"price" : 9.9,"sales" : 1000,"score" : 4.7,"store_id" : 1,"store_name" : "M多寿司","create_time" : "2023-01-18 08:01:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"id" : 2,"name" : "1-2人招牌双拼套餐","tags" : ["寿司"],"price" : 18.9,"sales" : 1200,"score" : 4.8,"store_id" : 1,"store_name" : "M多寿司","create_time" : "2023-01-18 08:02:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"id" : 3,"name" : "三文鱼寿司","tags" : ["寿司,鱼肉"],"price" : 16.9,"sales" : 820,"score" : 4.9,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:03:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "4","_score" : 1.0,"_source" : {"id" : 4,"name" : "极上全品寿司套餐","tags" : ["寿司"],"price" : 25,"sales" : 1500,"score" : 4.6,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:04:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "5","_score" : 1.0,"_source" : {"id" : 5,"name" : "劲脆鸡腿汉堡","tags" : ["汉堡,鸡肉"],"price" : 21.5,"sales" : 200,"score" : 4.5,"store_id" : 3,"store_name" : "肯德基","create_time" : "2023-01-18 08:05:00"}}]}
}

1.2 示例:查询第 2 页,每页大小为 5

GET /mt_product/_search
{"query": {"match_all": {}},"from": 5,"size": 5
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "6","_score" : 1.0,"_source" : {"id" : 6,"name" : "香辣鸡腿汉堡","tags" : ["汉堡,鸡肉"],"price" : 21.5,"sales" : 98,"score" : 4.4,"store_id" : 3,"store_name" : "肯德基","create_time" : "2023-01-18 08:06:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "7","_score" : 1.0,"_source" : {"id" : 7,"name" : "20块香辣鸡翅","tags" : ["鸡肉"],"price" : 99,"sales" : 5,"score" : 4.8,"store_id" : 3,"store_name" : "肯德基","create_time" : "2023-01-18 08:07:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "8","_score" : 1.0,"_source" : {"id" : 8,"name" : "3层芝士年堡套餐","tags" : ["汉堡"],"price" : 29,"sales" : 4000,"score" : 4.9,"store_id" : 4,"store_name" : "汉堡王","create_time" : "2023-01-18 08:08:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "9","_score" : 1.0,"_source" : {"id" : 9,"name" : "霸道小酥鸡+薯霸王","tags" : ["汉堡,鸡肉"],"price" : 19,"sales" : 300,"score" : 4.2,"store_id" : 4,"store_name" : "汉堡王","create_time" : "2023-01-18 08:09:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "10","_score" : 1.0,"_source" : {"id" : 10,"name" : "双层原味板烧鸡腿麦满分四件套","tags" : ["汉堡,鸡肉"],"price" : 29,"sales" : 3000,"score" : 4.8,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:10:00"}}]}
}

1.3 示例:查询第 3 页,每页大小为 5

GET /mt_product/_search
{"query": {"match_all": {}},"from": 10,"size": 5
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "11","_score" : 1.0,"_source" : {"id" : 11,"name" : "火腿扒麦满分组合","tags" : ["汉堡"],"price" : 8,"sales" : 100000,"score" : 4.9,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:11:00"}},{"_index" : "mt_product","_type" : "_doc","_id" : "12","_score" : 1.0,"_source" : {"id" : 12,"name" : "原味板烧鸡腿麦满组件","tags" : ["汉堡,鸡肉"],"price" : 9.9,"sales" : 140000,"score" : 4.9,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:12:00"}}]}
}

二、排序

Elasticsearch 支持按指定的字段进行排序,使用 sort 参数来设置排序,排序字段也可以是多个。排序选项中正序使用 asc,倒序使用 desc

2.1 示例:按 id 正序排序

GET /mt_product/_search
{"query": {"match_all": {}},"size": 3,"sort": [{"id": {"order": "asc"}}]
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "1","_score" : null,"_source" : {"id" : 1,"name" : "招牌海苔单人餐","tags" : ["寿司"],"price" : 9.9,"sales" : 1000,"score" : 4.7,"store_id" : 1,"store_name" : "M多寿司","create_time" : "2023-01-18 08:01:00"},"sort" : [1]},{"_index" : "mt_product","_type" : "_doc","_id" : "2","_score" : null,"_source" : {"id" : 2,"name" : "1-2人招牌双拼套餐","tags" : ["寿司"],"price" : 18.9,"sales" : 1200,"score" : 4.8,"store_id" : 1,"store_name" : "M多寿司","create_time" : "2023-01-18 08:02:00"},"sort" : [2]},{"_index" : "mt_product","_type" : "_doc","_id" : "3","_score" : null,"_source" : {"id" : 3,"name" : "三文鱼寿司","tags" : ["寿司,鱼肉"],"price" : 16.9,"sales" : 820,"score" : 4.9,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:03:00"},"sort" : [3]}]}
}

2.2 示例:按 id 倒序排序

GET /mt_product/_search
{"query": {"match_all": {}},"size": 3,"sort": [{"id": {"order": "desc"}}]
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "12","_score" : null,"_source" : {"id" : 12,"name" : "原味板烧鸡腿麦满组件","tags" : ["汉堡,鸡肉"],"price" : 9.9,"sales" : 140000,"score" : 4.9,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:12:00"},"sort" : [12]},{"_index" : "mt_product","_type" : "_doc","_id" : "11","_score" : null,"_source" : {"id" : 11,"name" : "火腿扒麦满分组合","tags" : ["汉堡"],"price" : 8,"sales" : 100000,"score" : 4.9,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:11:00"},"sort" : [11]},{"_index" : "mt_product","_type" : "_doc","_id" : "10","_score" : null,"_source" : {"id" : 10,"name" : "双层原味板烧鸡腿麦满分四件套","tags" : ["汉堡,鸡肉"],"price" : 29,"sales" : 3000,"score" : 4.8,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:10:00"},"sort" : [10]}]}
}

三、指定返回字段

在实际的查询中,我们通过是有选择地返回我们想要的字段,这样会节省带宽和内存的使用。Elasticsearch 通过两个参数来指定返回字段:

  • fields:用来指定返回的字段

  • _source:将 _source 设为 false,表示不会返回所有完整文档数据(文档数据存在 _source 中)。

3.1 示例:只返回 id 和 name 字段

GET /mt_product/_search
{"query": {"match_all": {}},"size": 3,"fields": ["id","name"],"_source": false
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "1","_score" : 1.0,"fields" : {"name" : ["招牌海苔单人餐"],"id" : [1]}},{"_index" : "mt_product","_type" : "_doc","_id" : "2","_score" : 1.0,"fields" : {"name" : ["1-2人招牌双拼套餐"],"id" : [2]}},{"_index" : "mt_product","_type" : "_doc","_id" : "3","_score" : 1.0,"fields" : {"name" : ["三文鱼寿司"],"id" : [3]}}]}
}

四、去重

当我们的索引中存在重复数据时,可以将其去重。Elasticsearch 通过 collapse 参数来实现去重,去重时需要指定字段。

4.1 示例:根据 store_id 字段去重

GET /mt_product/_search
{"query": {"match_all": {}},"collapse": {"field": "store_id"}
}

响应结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 12,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : 1,"name" : "招牌海苔单人餐","tags" : ["寿司"],"price" : 9.9,"sales" : 1000,"score" : 4.7,"store_id" : 1,"store_name" : "M多寿司","create_time" : "2023-01-18 08:01:00"},"fields" : {"store_id" : ["1"]}},{"_index" : "mt_product","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"id" : 3,"name" : "三文鱼寿司","tags" : ["寿司,鱼肉"],"price" : 16.9,"sales" : 820,"score" : 4.9,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:03:00"},"fields" : {"store_id" : ["2"]}},{"_index" : "mt_product","_type" : "_doc","_id" : "5","_score" : 1.0,"_source" : {"id" : 5,"name" : "劲脆鸡腿汉堡","tags" : ["汉堡,鸡肉"],"price" : 21.5,"sales" : 200,"score" : 4.5,"store_id" : 3,"store_name" : "肯德基","create_time" : "2023-01-18 08:05:00"},"fields" : {"store_id" : ["3"]}},{"_index" : "mt_product","_type" : "_doc","_id" : "8","_score" : 1.0,"_source" : {"id" : 8,"name" : "3层芝士年堡套餐","tags" : ["汉堡"],"price" : 29,"sales" : 4000,"score" : 4.9,"store_id" : 4,"store_name" : "汉堡王","create_time" : "2023-01-18 08:08:00"},"fields" : {"store_id" : ["4"]}},{"_index" : "mt_product","_type" : "_doc","_id" : "10","_score" : 1.0,"_source" : {"id" : 10,"name" : "双层原味板烧鸡腿麦满分四件套","tags" : ["汉堡,鸡肉"],"price" : 29,"sales" : 3000,"score" : 4.8,"store_id" : 5,"store_name" : "麦当劳","create_time" : "2023-01-18 08:10:00"},"fields" : {"store_id" : ["5"]}}]}
}

五、高亮显示

Elasticsearch 可以将搜索关键字做高亮显示,可以指定高亮显示的字段,会在响应结果中对关键字加 <em> 标签,便于前端特殊处理。

5.1 示例:将 name 字段做高亮显示

GET /mt_product/_search
{"query": {"multi_match": {"query": "寿司","fields": ["name", "tag"]}},"highlight": {"fields": {"name": {}}}
}

查询结果:

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.7955686,"hits" : [{"_index" : "mt_product","_type" : "_doc","_id" : "3","_score" : 1.7955686,"_source" : {"id" : 3,"name" : "三文鱼寿司","tags" : ["寿司,鱼肉"],"price" : 16.9,"sales" : 820,"score" : 4.9,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:03:00"},"highlight" : {"name" : ["三文鱼<em>寿司</em>"]}},{"_index" : "mt_product","_type" : "_doc","_id" : "4","_score" : 1.6760855,"_source" : {"id" : 4,"name" : "极上全品寿司套餐","tags" : ["寿司"],"price" : 25,"sales" : 1500,"score" : 4.6,"store_id" : 2,"store_name" : "爱食寿司","create_time" : "2023-01-18 08:04:00"},"highlight" : {"name" : ["极上全品<em>寿司</em>套餐"]}}]}
}

系列文章

🔥 Elasticsearch 核心技术(一):Elasticsearch 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(二):elasticsearch-head 插件安装和使用
🔥 Elasticsearch 核心技术(三):Kibana 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(四):索引管理、映射管理、文档管理(REST API)
🔥 Elasticsearch 核心技术(五):常用数据类型详解
🔥 Elasticsearch 核心技术(六):内置的 8 种分词器详解 + 代码示例
🔥 Elasticsearch 核心技术(七):IK 中文分词器的安装、使用、自定义字典
🔥 Elasticsearch 核心技术(八):常用 DSL 查询(全文搜索、精确匹配、布尔查询)

热门专栏

👍 《Python入门核心技术》
👍 《IDEA 教程:从入门到精通》
👍 《Java 教程:从入门到精通》
👍 《MySQL 教程:从入门到精通》
👍 《大数据核心技术从入门到精通》

在这里插入图片描述


http://www.ppmy.cn/news/38482.html

相关文章

Java Script基础与提升——流程控制分支

Java Script系列文章目录 【Java Script基础与提升】——数据类型模块_张小鱼༒的博客-CSDN博客 【Java Script基础与提升】——运算符_张小鱼༒的博客-CSDN博客 文章目录 目录 Java Script系列文章目录 前言 一、Java Script流程控制概述 二、三种流程控制 1.顺序结构 2.分支…

近期java学习

注释 在程序上方添加说明信息 1.单行注释// 2.多行注释/*内容*/ 3.文档注释/*内容*/关键字 关键字特点: 关键字的字母全部小写 常用的代码编辑器,针对关键字有特殊的颜色标记 class 用于(创建/定义)一个类,后面跟随类名 字面量 数据在程序中的书写格式 字符串 public c…

刷题_25:星际密码 and 数根

一.星际密码 题目链接&#xff1a; 星际密码 题目描述&#xff1a; 星际战争开展了100年之后&#xff0c;NowCoder终于破译了外星人的密码&#xff01;他们的密码是一串整数&#xff0c;通过一张表里的信息映射成最终4位密码。表的规则是&#xff1a;n对应的值是矩阵X的n次方的…

Linux命令之ln -s命令(软链接)

ln命令 Linux ln&#xff08;英文全拼&#xff1a;link files&#xff09;命令是一个非常重要命令&#xff0c;它的功能是为某一个文件在另外一个位置建立一个同步的链接。 当我们需要在不同的目录&#xff0c;用到相同的文件时&#xff0c;我们不需要在每一个需要的目录下都放…

JavaWeb学习笔记-02-TomcatServletThymeleaf

1.Tomcat下载及使用 1.1 下载 官方网站&#xff1a; https://tomcat.apache.org/ 1.2 使用 下载后解压 bin 可执行文件目录conf 配置文件目录lib 存放库目录logs 日志文件目录websapp 项目部署的目录work 工作目录temp 临时目录 由于Tomcat也是用java和C来写的&#x…

PySpark RDD的缓存和Checkpoint

文章目录1. 前言2. RDD的缓存3. RDD的CheckPoint1. 前言 RDD之间进行相互迭代计算&#xff08;Transformation的转换&#xff09;&#xff0c;当执行开启后&#xff0c;新RDD的生成&#xff0c;代表老RDD的消息&#xff0c;RDD的数据只在处理的过程中存在&#xff0c;一旦处理…

SQL VQ14 统计每天刷题数超过5的user_id以及刷题数

SQL VQ14 统计每天刷题数超过5的user_id以及刷题数数据需求解决题目 : https://www.nowcoder.com/practice/852a14b888c448f1a26f0e2e20f668a7?tpId341 数据 drop table if exists questions_pass_record_detail;CREATE TABLE questions_pass_record_detail ( user_id int N…

Edge集锦没有同步按钮 - 待解决

问题 日期&#xff1a;2023年4月6日 2023年2月份左右&#xff0c;更新Edge后&#xff0c;集锦的同步选项消失了&#xff0c;右键标签页也没有添加到集锦的选项&#xff0c;如下图。 详细说明此问题&#xff0c;Edge一旦登录个人账号&#xff0c;集锦同步选项就会消失&#…