Elasticsearch组合条件的或查询( must 和 should 组合)

业务:对省市的权限控制查询时,会有多个省市查询的条件限制。

如下:

需要查询2021年8月25日之前位于海南省、陕西省、广东省深圳市和广州市的用户数。

分析:

条件限制如下:

must:2021-08-25【时间】
should:海南省  ||  陕西省  ||  广东省(深圳市、广州市)【省市】

 ① 一开始是想到用 一个should 包裹 多个must 来实现,如下:

缺点:此时会发现 时间条件 重复了多次,在代码中也有所体现。

{
	"size": 0,
	"query": {
		"bool": {
			"should": [
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "海南省"
									}
								}
							},
							{
								"range": {
									"event.createDate": {
										"from": null,
										"to": "2021-08-25",
										"include_lower": true,
										"include_upper": true
									}
								}
							}
						]
					}
				},
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "陕西省"
									}
								}
							},
							{
								"range": {
									"event.createDate": {
										"from": null,
										"to": "2021-08-25",
										"include_lower": true,
										"include_upper": true
									}
								}
							}
						]
					}
				},
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "广东省"
									}
								}
							},
							{
								"terms": {
									"header.city": [
										"深圳市",
										"广州市"
									]
								}
							},
							{
								"range": {
									"event.createDate": {
										"from": null,
										"to": "2021-08-25",
										"include_lower": true,
										"include_upper": true
									}
								}
							}
						]
					}
				}
			]
		}
	}
}

 ② 将 时间条件 单独拎出来有两种实现方式:

   (1)在 should 的外层多包裹一个 must,把时间条件放在 must 里(和 should 同级条件)。

缺点:多包裹一层,使得与改动前的复杂性相差不大。

{
	"size": 0,
	"query": {
		"bool": {
			"must": [
				{
					"range": {
						"event.createDate": {
							"from": null,
							"to": "2021-08-25",
							"include_lower": true,
							"include_upper": true
						}
					}
				},
				{
					"bool": {
						"should": [
							{
								"bool": {
									"must": [
										{
											"term": {
												"header.region": {
													"value": "海南省"
												}
											}
										}
									]
								}
							},
							{
								"bool": {
									"must": [
										{
											"term": {
												"header.region": {
													"value": "陕西省"
												}
											}
										}
									]
								}
							},
							{
								"bool": {
									"must": [
										{
											"term": {
												"header.region": {
													"value": "广东省"
												}
											}
										},
										{
											"terms": {
												"header.city": [
													"深圳市",
													"广州市"
												]
											}
										}
									]
								}
							}
						]
					}
				}
			]
		}
	}
}

  (2)把时间条件直接和 should 同级条件。【推荐】

注意:

must 和 should 并列条件组合时,参数 minimum_should_match 默认为0 (即 should 的条件可以都不满足)。这也是 must 和 should 组合查询时,should 失效的原因。

should下有多个条件时,必须设置参数 minimum_should_match 为1 才能实现 或 操作。

{
	"size": 0,
	"query": {
		"bool": {
			"must": [
				{
					"range": {
						"event.createDate": {
							"from": null,
							"to": "2021-08-25",
							"include_lower": true,
							"include_upper": true
						}
					}
				}
			],
			"should": [
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "海南省"
									}
								}
							}
						]
					}
				},
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "陕西省"
									}
								}
							}
						]
					}
				},
				{
					"bool": {
						"must": [
							{
								"term": {
									"header.region": {
										"value": "广东省"
									}
								}
							},
							{
								"terms": {
									"header.city": [
										"深圳市",
										"广州市"
									]
								}
							}
						]
					}
				}
			],
			"minimum_should_match": 1
		}
	}
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值