袁庭新ES系列12节 | Elasticsearch高级查询操作

前言

上篇文章讲了关于Elasticsearch的基本查询操作。接下来袁老师为大家带来Elasticsearch高级查询部分相关的内容。Elasticsearch是基于JSON提供完整的查询DSL(Domain Specific Language:领域特定语言)来定义查询。因此,我们有必要在专题模块来详细探讨Elasticsearch高级查询部分内容。

我们先来做个热身,了解下这一小节学习的目标,我将带领大家从以下五个模块来学习Elasticsearch的高级查询相关技术。

  • 结果过滤查询
  • 条件过滤查询
  • 结果排序
  • 分页查询
  • 高亮显示

一. 结果过滤查询

默认情况下,Elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加_source属性来进行过滤。

1.直接指定字段

演示示例:

GET /yx/_search
{"_source": ["title", "price"],"query": {"term": {"price": 2699}}
}

语法说明:在查询结构中,通过_source属性来指定查询结果集中需要保留哪些字段信息。

响应结果:

{"took": 90,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1,"_source": {"price": 2699,"title": "小米手机"}}]}
}

运行上述代码响应结果见下:

2.指定includes和excludes

我们也可以通过:

属性

描述

includes

来指定想要显示的字段

excludes

来指定不想要显示的字段

注意:二者都是可选的。

演示示例:

GET /yx/_search
{"_source": {"includes": ["title", "images"]},"query": {"term": {"price": 2699}}
}

响应结果:

{"took": 148,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1,"_source": {"images": "http://image.yx.com/12479122.jpg","title": "小米手机"}}]}
}

运行上述代码响应结果见下:

下面的示例与上面的结果将是一样的:

GET /yx/_search
{"_source": {"excludes": ["price"]},"query": {"term": {"price": 2699}}
}

响应结果:

{"took": 6,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1,"_source": {"images": "http://image.yx.com/12479122.jpg","title": "小米手机"}}]}
}

运行上述代码响应结果见下:

二. filter过滤

Elasticsearch使用的查询语言(DSL)拥有一套查询组件,这些组件可以以无限组合的方式进行搭配。这套组件可以在以下两种情况下使用:过滤情况(filtering context)和查询情况(query context)。

如何选择查询与过滤?通常的规则是,使用查询(query)语句来进行全文搜索或者其它任何需要影响相关性得分的搜索。 除此以外的情况都使用过滤(filters)。

1.条件查询中进行过滤

所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式:

GET /yx/_search
{"query": {"bool": {"must": {"match": {"title": "小米手机"}},"filter": {"range": {"price": {"gt": 2000.00, "lt": 3800.00}}}}}
}

响应结果:

{"took": 71,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1.1143606,"hits": [{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1.1143606,"_source": {"title": "小米手机","images": "http://image.yx.com/12479122.jpg","price": 2699}},{"_index": "yx","_type": "goods","_id": "1","_score": 0.2876821,"_source": {"title": "大米手机","images": "http://image.yx.com/12479122.jpg","price": 2899}}]}
}

运行上述代码响应结果见下:

2.无查询条件直接过滤

如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score取代只有filter语句的bool查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。

GET /yx/_search
{"query": {"constant_score": {"filter": {"range": {"price": {"gt": 2000.00, "lt": 3800.00}}}}}
}

响应结果:

{"took": 16,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1,"_source": {"title": "小米手机","images": "http://image.yx.com/12479122.jpg","price": 2699}},{"_index": "yx","_type": "goods","_id": "1","_score": 1,"_source": {"title": "大米手机","images": "http://image.yx.com/12479122.jpg","price": 2899}}]}
}

运行上述代码响应结果见下:

三. 结果排序

1.单字段排序

sort可以让我们按照不同的字段进行排序,并且通过order属性指定排序的方式。

属性

描述

asc

升序排序

desc

降序排序

演示案例:

GET /yx/_search
{"query": {"match": {"title": "小米手机"}},"sort": [{"price": {"order": "desc"}}]
}

响应结果:

{"took": 31,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 5,"max_score": null,"hits": [{"_index": "yx","_type": "goods","_id": "4","_score": null,"_source": {"title": "Apple手机","images": "http://image.yx.com/12479122.jpg","price": 6899},"sort": [6899]},{"_index": "yx","_type": "goods","_id": "2","_score": null,"_source": {"title": "IPhone手机","images": "http://image.yx.com/12479122.jpg","price": 6299,"stock": 200,"saleable": true,"subTitle": "IPhone 15 Pro"},"sort": [6299]},{"_index": "yx","_type": "goods","_id": "5","_score": null,"_source": {"title": "小米电视4A","images": "http://images.com","price": 3999},"sort": [3999]},{"_index": "yx","_type": "goods","_id": "1","_score": null,"_source": {"title": "大米手机","images": "http://image.yx.com/12479122.jpg","price": 2899},"sort": [2899]},{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": null,"_source": {"title": "小米手机","images": "http://image.yx.com/12479122.jpg","price": 2699},"sort": [2699]}]}
}

2.多字段排序

假定我们想要结合使用price和_score(得分)进行查询,并且匹配的结果首先按照价格排序,然后再按照相关性得分降序排序:

GET /yx/_search
{"query": {"bool": {"must": { "match": { "title": "小米手机" }},"filter": {"range": {"price": {"gt": 2000,"lt": 3000}}}}},"sort": [{ "price": { "order": "desc" }},{ "_score": { "order": "desc" }}]
}

响应结果:

{"took": 10,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": null,"hits": [{"_index": "yx","_type": "goods","_id": "1","_score": 0.2876821,"_source": {"title": "大米手机","images": "http://image.yx.com/12479122.jpg","price": 2899},"sort": [2899,0.2876821]},{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 1.1143606,"_source": {"title": "小米手机","images": "http://image.yx.com/12479122.jpg","price": 2699},"sort": [2699,1.1143606]}]}
}

四. 分页查询

Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回。那么,如果要实现分页查询该怎么办呢?

Elasticsearch的分页与MySQL数据库非常相似,都是指定两个值:

属性

描述

from

目标数据的偏移值(开始位置),默认from为0

size

每页大小

演示案例:

GET /yx/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}}],"from": 1,"size": 3
}

响应结果:

{"took": 17,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 5,"max_score": null,"hits": [{"_index": "yx","_type": "goods","_id": "2","_score": null,"_source": {"title": "IPhone手机","images": "http://image.yx.com/12479122.jpg","price": 6299,"stock": 200,"saleable": true,"subTitle": "IPhone 15 Pro"},"sort": [6299]},{"_index": "yx","_type": "goods","_id": "3","_score": null,"_source": {"title": "小米电视4A","images": "http://image.yx.com/12479122.jpg","price": 3899},"sort": [3899]}]}
}

五. 高亮显示

1.高亮显示原理

高亮显示的原理介绍见下:

  • 服务端搜索数据,得到搜索结果。
  • 把搜索结果中,搜索关键字都加上约定好的标签。
  • 前端页面提前写好标签的CSS样式,即可高亮显示。

Elasticsearch中实现高亮的语法比较简单,高亮显示语法格式见下:

GET /索引库名/_search
{"query": {"match": {"字段": "字段值"}},"highlight": {"pre_tags": "前置标签","post_tags": "后置标签","fields": {"高亮字段名": {}}}
}

在使用match查询的同时,加上一个highlight属性。highlight属性提供以下属性:

属性

描述

pre_tags

前置标签

post_tags

后置标签

fields

需要高亮的字段(例如这里声明title字段需要高亮)

2.高亮显示案例

演示案例:

GET /yx/_search
{"query": {"match": {"title": "手机"}},"highlight": {"pre_tags": "<span>","post_tags": "</span>","fields": {"title": {}}}
}

响应结果:

{"took": 19,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 4,"max_score": 0.2876821,"hits": [{"_index": "yx","_type": "goods","_id": "1","_score": 0.2876821,"_source": {"title": "大米手机","images": "http://image.yx.com/12479122.jpg","price": 2899},"highlight": {"title": ["大米<span>手机</span>"]}},{"_index": "yx","_type": "goods","_id": "lNC7KYUB35ub5htYEZMU","_score": 0.13353139,"_source": {"title": "小米手机","images": "http://image.yx.com/12479122.jpg","price": 2699},"highlight": {"title": ["小米<span>手机</span>"]}},{"_index": "yx","_type": "goods","_id": "2","_score": 0.13353139,"_source": {"title": "IPhone手机","images": "http://image.yx.com/12479122.jpg","price": 6299,"stock": 200,"saleable": true,"subTitle": "IPhone 15 Pro"},"highlight": {"title": ["IPhone<span>手机</span>"]}},{"_index": "yx","_type": "goods","_id": "4","_score": 0.13353139,"_source": {"title": "Apple手机","images": "http://image.yx.com/12479122.jpg","price": 6899},"highlight": {"title": ["Apple<span>手机</span>"]}}]}
}

运行上述代码响应结果见下:

六. 结语

关于Elasticsearch高级查询篇相关的内容我们就给大家介绍完了,来复习回顾下这一章节的主要内容。本文从结果过滤查询、结果排序、分页查询、检索查询、关键字查询、高亮显示、过滤查询等几个方面通过实例讲解了Elasticsearch的高级查询。如果还没有掌握的小伙伴,一定要通过文章中大量的案例来进行实操演练从而巩固这一部分知识。下一小节我们将为大家带来Elasticsearch中聚合操作相关的内容。

今天的内容就分享到这里吧。关注「袁庭新」,干货天天都不断!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/495508.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vscode不能远程连接ubuntu18.04.6

目录 问题解决Portable Mode 安装vscode 补充说明学习资料 问题 vscode远程ssh连接ubuntu18.04.6时&#xff0c;出现如下提示框&#xff0c;单击Learn More后&#xff0c;定位到问题。Can I run VS Code Server on older Linux distributions? 原始是&#xff1a;需要glibc …

Vue项目构建优化

本文作者为 360 奇舞团前端开发工程师 宁航 在开发大型前端项目时&#xff0c;往往是一个需求对应一个分支&#xff0c;当完成需求后&#xff0c;就需要将代码打包、部署。代码通常需要部署到多个环境中&#xff0c;这些环境包括&#xff1a;日常环境、测试环境、回归环境和生产…

【操作技巧】如何给Jetson Orin Nano的ubuntu port换镜像源

学习《OpenCV应用开发&#xff1a;入门、进阶与工程化实践》一书 做真正的OpenCV开发者&#xff0c;从入门到入职&#xff0c;一步到位&#xff01; 换镜像源 因为工作需要&#xff0c;我想把之前Jetson Orin Nano上OpenCV4.5.4 升级到OpenCV4.8。先到这里下载脚本 wget htt…

搜维尔科技:OptiTrack 提供了性能最佳的动作捕捉平台

OptiTrack 动画 我们的 Prime 系列相机和 Motive 软件相结合&#xff0c;产生了世界上最大的捕获量、最精确的 3D 数据和有史以来最高的相机数量。OptiTrack 提供了性能最佳的动作捕捉平台&#xff0c;具有易于使用的制作工作流程以及运行世界上最大舞台所需的深度。 无与伦比…

【mysql 数据库事务】开启事务操作数据库,写入失败后,不回滚,会有问题么? 这里隐藏着大坑,复试,面试时可以镇住面试老师!!!!

建表字段: CREATE TABLE user (id INT(11) NOT NULL AUTO_INCREMENT,nickname VARCHAR(32) NOT NULL COLLATE utf8mb4_general_ci,email VARCHAR(32) NOT NULL COLLATE utf8mb4_general_ci,status SMALLINT(6) UNSIGNED NULL DEFAULT NULL,password VARCHAR(256) NULL DEFAULT…

P2141 [NOIP2014 普及组] 珠心算测验

AC代码&#xff1a; #include<iostream> #include<map>using namespace std;const int N 110; int a[N];int main() {int n;cin >> n;for(int i0;i<n;i) cin >> a[i];map<int,bool> mp;for(int i0;i<n;i){for(int ji1;j<n;j){mp[a[i…

Django配置静态文件

Django配置静态文件 目录 Django配置静态文件静态文件配置调用方法 一般我们将html文件都放在默认templates目录下 静态文件放在static目录下 static目录大致分为 js文件夹css文件夹img文件夹plugins文件夹 在浏览器输入url能够看到对应的静态资源&#xff0c;如果看不到说明…

在linux上不依赖于Nignx等服务器部署ASP.NET Core 7.0 WebAPI

笔者近期需要部署一款基于B/S架构的后端程序在linux的Debian发行版上&#xff0c;本文章以本次部署遇到的问题为线索&#xff0c;总结如何在Debian上部署ASP.NET Core7.0WebAPI应用程序。 在linux上不依赖于Nignx等服务器部署ASP.NET Core 7.0 WebAPI 1.先决条件2.应用发布3.部…

MetaGPT 1 安装与配置踩坑实录

安装 与 配置直接参考这里就行&#xff1a;Hugging Muti Agent&#xff08;二月学习&#xff09; - 飞书云文档 (feishu.cn) 这里按照教程安装的是metagpt 0.6.6 &#xff0c;经过跟0.7.0对比&#xff0c;个人认为0.7对其他llm接入可能更好&#xff0c;文档也更清晰。 0.6.6的…

【Python笔记-设计模式】中介者模式

一、说明 中介者模式是一种行为设计模式&#xff0c;减少对象之间混乱无序的依赖关系。该模式会限制对象之间的直接交互&#xff0c;迫使它们通过一个中介者对象进行合作。 (一) 解决问题 降低系统中对象之间的直接通信&#xff0c;将复杂的交互转化为通过中介者进行的间接交…

热闹元宵进行中,如何利用VR全景展示民宿品牌形象?

错峰出游闹元宵&#xff0c;元宵节恰逢周末&#xff0c;而且还是春节假期返工之后的首个休息日&#xff0c;不少人都想通过短途度假来缓解“节后综合征”。两位数的特价机票、打折的各种酒店让你实现“旅行自由”&#xff0c;那么如何知道特价酒店服务好不好呢&#xff1f;先别…

VR转接器:破解虚拟与现实边界的革命性设备

VR转接器&#xff0c;这一革命性的设备&#xff0c;为虚拟现实体验带来了前所未有的自由度。它巧妙地连接了虚拟与现实&#xff0c;使得用户在享受VR眼镜带来的奇幻世界的同时&#xff0c;也能自由地在现实世界中活动。这一设计的诞生&#xff0c;不仅解决了VR眼镜续航的瓶颈问…