本文主要是介绍【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语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!