业务:对省市的权限控制查询时,会有多个省市查询的条件限制。  
 如下:  
 需要查询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
		}
	}
}  
                
        
    
 
 |