❤️ 博客主页:水滴技术
🚀 支持水滴:点赞👍 + 收藏⭐ + 留言💬
🌸 订阅专栏:大数据核心技术从入门到精通
文章目录
- 一、分页
- 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 提供了两个参数 from
和 size
来控制分页结果。
-
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 教程:从入门到精通》
👍 《大数据核心技术从入门到精通》