ElasticSearch【五】浅析Query DSL & 映射sql语法

41 篇文章 0 订阅
3 篇文章 0 订阅

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查询为例,均为片段)

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

    {
    “bool”: {
    “must_not”: [
    {
    “exists”: {
    “field”: “t_%KaTeX parse error: Expected group after '^' at position 1: ^̲%^_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.

作者:暂7师师长常乃超
来源:CSDN
原文:https://blog.csdn.net/zzh920625/article/details/82876276
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值