【ElasticSearch】(三)浅析Query DSL 映射sql语法

2024-08-26 20:58

本文主要是介绍【ElasticSearch】(三)浅析Query DSL 映射sql语法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

     ElasticSearch官方给出的DSL解释如下:

elasticsearch provides a full Query DSL based on JSON to define queries. In general, there are basic queries such as term or prefix. There are also compound queries like the bool query. Queries can also have filters associated with them such as the filtered or constant_score queries, with specific filter queries.Think of the Query DSL as an AST of queries. Certain queries can contain other queries (like the bool query), others can contain filters (like the constant_score), and some can contain both a query and a filter (like the filtered). Each of those can contain any query of the list of queries or any filter from the list of filters, resulting in the ability to build quite complex (and interesting) queries.Both queries and filters can be used in different APIs. For example, within a search query, or as a facet filter. This section explains the components (queries and filters) that can form the AST one can use.Filters are very handy since they perform an order of magnitude better than plain queries since no scoring is performed and they are automatically cached.

       reference address: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl.html

 

       点击上述超链,官方从filter和queris两个角度展开对elastic search dsl的介绍,经历了项目实战的洗礼之后,我从使用者的角度来谈谈elastic serach的dsl。

 

一、分类        

     如上所述,对查询从6个角度做的分类,使用过程中最为常见的是“全文查询”和“词项查询”两个部分,当然我的前提是把mysql表结构映射到了es,后续会把这个映射的思想分享出来。

 

二、查询详情

//todo sorting……

 

三、dsl(组合查询)与 sql 映射

      常见sql查询语句如下:

SELECT product
FROM   products
WHERE  (price = 20 OR productID = "XHDK-A-1293-#fJ3")AND  (price != 30)

     这里需要通过“bool”表达式来做组合,bool过滤器的基本语法如下:

{"bool" : {"must" :     [],"should" :   [],"must_not" : [],}
}

      最简单的映射关系如下:

must : 所有的语句都 必须(must) 匹配,与 AND 等价。
must_not : 所有的语句都 不能(must not) 匹配,与 NOT 等价。
should : 至少有一个语句要匹配,与 OR 等价。

      当我们需要多个过滤器时,只须将它们置入 bool 过滤器的不同部分即可。

      下边展示上述sql语句对应的es dsl写法:

GET /my_store/products/_search
{"query" : {"filtered" : { "filter" : {"bool" : {"should" : [{ "term" : {"price" : 20}}, { "term" : {"productID" : "XHDK-A-1293-#fJ3"}} ],"must_not" : {"term" : {"price" : 30} }}}}}
}

     针对上述查询需注意:

     1.在bool表达式外层需要一个 filtered 查询将所有的东西包起来。

     2.在 should 语句块里面的两个 term 过滤器与 bool 过滤器是父子关系,两个 term 条件需要匹配其一。

     3.如果一个产品的价格是 30 ,那么它会自动被排除,因为它处于 must_not 语句里面。

 

     另外针对查询条件嵌套较深的情况,比如:

SELECT document
FROM   products
WHERE  productID      = "KDKE-B-9947-#kL5"OR (     productID = "JODL-X-1937-#pV7"AND price     = 30 )

       其中“or”的右侧,则是又深入一层后的筛选,这时,只需在已有的bool表达式内再做一层嵌套。即尽管 bool 是一个复合的过滤器,可以接受多个子过滤器,需要注意的是 bool 过滤器本身仍然还只是一个过滤器。 这意味着我们可以将一个 bool 过滤器置于其他 bool 过滤器内部,这为我们提供了对任意复杂布尔逻辑进行处理的能力。代码如下:

GET /my_store/products/_search
{"query" : {"filtered" : {"filter" : {"bool" : {"should" : [{ "term" : {"productID" : "KDKE-B-9947-#kL5"}}, { "bool" : { "must" : [{ "term" : {"productID" : "JODL-X-1937-#pV7"}}, { "term" : {"price" : 30}} ]}}]}}}}
}

      

四、工作遇到有特色的sql转dsl(ibatis查询为例,均为片段)

0. “is null”写法,比如 la.department_id is null

{"bool": {"must_not": [{"exists": {"field": "t_%$^%$^_apply|department_id"}}]}
}

       其实,最初的 is null 写法是这样:

{"missing": {"field": "t_loan_apply|department_id"}
}

       那时,项目里用的es版本还是2.X,之后升级到了6.3后开始不识别missing了,查了下,竟然被废弃了。

 

1.“!=”写法

"bool": {"must_not": [{"term": {"t_loan_third_party_zh|zh_final_audit_status": 1}}]
}

 

2.“or”写法

{{#deptScope}}
{"bool": {"should": [{"missing": {"field": "t_loan_apply|department_id"}},{"terms": {"t_loan_apply|department_id":[ {{{deptScope}}} ]}}]}
},
{{/deptScope}}

 

3.“in”写法

{{#cityScope}}{"terms":{"t_loan_apply|city_id":[ {{{cityScope}}} ]}},
{{/cityScope}}

 

4.时间区间写法

{{#applyTimeStart}}{"range": {"t_loan_apply|apply_time": {"gte": "{{applyTimeStart}}"}}},
{{/applyTimeStart}}
{{#applyTimeEnd}}{"range": {"t_loan_apply|apply_time": {"lte": "{{applyTimeEnd}}"}}},
{{/applyTimeEnd}}

 

5.“like”写法

{{#customerName}}{"match_phrase": {"t_loan_apply|custom_name": "{{customerName}}"}},
{{/customerName}}

 

6.嵌套查询

<isNotEmpty prepend="AND" property="zhApplyStatus">ltpz.zh_apply_status=#zhApplyStatus#<isEqual compareValue="2" property="zhApplyStatus" >and (ltpz.zh_first_&&&*&_status!=1 OR ltpz.zh_first_&&&*&_status  is null)</isEqual><isEqual compareValue="4" property="zhApplyStatus" >and (ltpz.zh_final_&&&*&_status!=1 OR ltpz.zh_final_&&&*&_status  is null)</isEqual>
</isNotEmpty>

       转化后如下:

{{#zhApplyStatus}}{"term": {"t_loan_#¥%%¥¥%_zh|zh_first_&&&*&_status": "{{zhApplyStatus}}"}},<%#bool zhApplyStatus == 2 %>{"bool": {"should": [{"bool": {"must_not": [{"term": {"t_loan_#¥%%¥¥%_zh|zh_first_&&&*&_status": 1}}]}},{"bool": {"must_not": [{"exists": {"field": "t_loan_#¥%%¥¥%_zh|zh_first_&&&*&_status"}}]}}]}},<%/bool%><%#bool zhApplyStatus == 4 %>{"bool": {"should": [{"bool": {"must_not": [{"term": {"t_loan_#¥%%¥¥%_zh|zh_final_&&&*&_status": 1}}]}},{"bool": {"must_not": [{"exists": {"field": "t_loan_#¥%%¥¥%_zh|zh_final_&&&*&_status"}}]}}]}},<%/bool%>
{{/zhApplyStatus}}

 

    that's all. modify 18.12.01.

这篇关于【ElasticSearch】(三)浅析Query DSL 映射sql语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1109662

相关文章

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

Mac电脑如何通过 IntelliJ IDEA 远程连接 MySQL

《Mac电脑如何通过IntelliJIDEA远程连接MySQL》本文详解Mac通过IntelliJIDEA远程连接MySQL的步骤,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友跟... 目录MAC电脑通过 IntelliJ IDEA 远程连接 mysql 的详细教程一、前缀条件确认二、打开 ID