ElasticSearch DSL小记之Nested Query + Nested Aggregation + Reverse Nested

ElasticSearch Version:6.2.2

Nested Aggregation
A special single bucket aggregation that enables aggregating nested documents.
一种特殊的单桶聚合,可聚合嵌套文档。

Reverse nested Aggregation
A special single bucket aggregation that enables aggregating on parent docs from nested documents. Effectively this aggregation can break out of the nested block structure and link to other nested structures or the root document, which allows nesting other aggregations that aren’t part of the nested object in a nested aggregation.

The reverse_nested aggregation must be defined inside a nested
aggregation.
一种特殊的单桶聚合,支持从嵌套文档中聚合父文档。实际上,这种聚合可以脱离嵌套块结构,并链接到其他嵌套结构或根文档,这允许嵌套不属于嵌套聚合中嵌套对象的其他聚合。reverse_nested聚合必须在嵌套聚合中定义。

DSL:


{
	"size": 0,
	"query": {
		"bool": {
			"filter": [{
				"term": {
					"params1": {
						"value": 0,
						"boost": 1.0
					}
				}
			}, {
				"nested": {
					"query": {
						"bool": {
							"filter": [{
								"bool": {
									"should": [{
										"bool": {
											"filter": [{
												"term": {
													"nestedObject.params2": {
														"value": 0,
														"boost": 1.0
													}
												}
											}],
											"adjust_pure_negative": true,
											"boost": 1.0
										}
									}, {
										"bool": {
											"should": [{
												"bool": {
													"filter": [{
														"term": {
															"nestedObject.params2": {
																"value": "1",
																"boost": 1.0
															}
														}
													}, {
														"range": {
															"nestedObject.params2Start": {
																"from": null,
																"to": "07:02:26",
																"include_lower": true,
																"include_upper": true,
																"boost": 1.0
															}
														}
													}],
													"adjust_pure_negative": true,
													"boost": 1.0
												}
											}, {
												"bool": {
													"filter": [{
														"term": {
															"nestedObject.params2": {
																"value": 1,
																"boost": 1.0
															}
														}
													}, {
														"range": {
															"nestedObject.params2End": {
																"from": "07:02:26",
																"to": null,
																"include_lower": true,
																"include_upper": true,
																"boost": 1.0
															}
														}
													}],
													"adjust_pure_negative": true,
													"boost": 1.0
												}
											}, {
												"bool": {
													"filter": [{
														"term": {
															"nestedObject.params2": {
																"value": 1,
																"boost": 1.0
															}
														}
													}, {
														"range": {
															"nestedObject.params2Start": {
																"from": null,
																"to": "07:02:26",
																"include_lower": true,
																"include_upper": true,
																"boost": 1.0
															}
														}
													}, {
														"range": {
															"nestedObject.params2End": {
																"from": "07:02:26",
																"to": null,
																"include_lower": true,
																"include_upper": true,
																"boost": 1.0
															}
														}
													}],
													"adjust_pure_negative": true,
													"boost": 1.0
												}
											}],
											"adjust_pure_negative": true,
											"boost": 1.0
										}
									}],
									"adjust_pure_negative": true,
									"boost": 1.0
								}
							}, {
								"bool": {
									"should": [{
										"bool": {
											"filter": [{
												"range": {
													"nestedObject.params3": {
														"from": "2020-05-07 07:02:26",
														"to": null,
														"include_lower": true,
														"include_upper": true,
														"boost": 1.0
													}
												}
											}, {
												"range": {
													"nestedObject.params4": {
														"from": null,
														"to": "2020-05-07 07:02:26",
														"include_lower": true,
														"include_upper": true,
														"boost": 1.0
													}
												}
											}, {
												"term": {
													"nestedObject.params5": {
														"value": 4,
														"boost": 1.0
													}
												}
											}],
											"adjust_pure_negative": true,
											"boost": 1.0
										}
									}, {
										"bool": {
											"should": [{
												"term": {
													"nestedObject.params5": {
														"value": 1,
														"boost": 1.0
													}
												}
											}, {
												"term": {
													"nestedObject.params5": {
														"value": 2,
														"boost": 1.0
													}
												}
											}],
											"adjust_pure_negative": true,
											"boost": 1.0
										}
									}],
									"adjust_pure_negative": true,
									"boost": 1.0
								}
							}],
							"should": [{
								"term": {
									"nestedObject.params6": {
										"value": 0,
										"boost": 1.0
									}
								}
							}, {
								"term": {
									"nestedObject.params6": {
										"value": 1,
										"boost": 1.0
									}
								}
							}, {
								"term": {
									"nestedObject.params6": {
										"value": 4,
										"boost": 1.0
									}
								}
							}],
							"adjust_pure_negative": true,
							"boost": 1.0
						}
					},
					"path": "nestedObject",
					"ignore_unmapped": false,
					"score_mode": "none",
					"boost": 1.0
				}
			}, {
				"term": {
					"params7": {
						"value": "CN"
					}
				}
			}],
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"_source": true,
	"aggregations": {
		"params7": {
			"terms": {
				"field": "params7",
				"missing": "",
				"size": 10000,
				"min_doc_count": 1,
				"shard_min_doc_count": 0,
				"show_term_doc_count_error": false,
				"order": [{
					"_count": "desc"
				}, {
					"_key": "asc"
				}],
				"collect_mode": "breadth_first"
			},
			"aggregations": {
				"params8": {
					"nested": {
						"path": "nestedObject"
					},
					"aggregations": {
						"packageType": {
							"terms": {
								"field": "nestedObject.params8"
							},
							"aggregations": {
								"params5": {
									"filter": {
										"term": {
											"nestedObject.params5": {
												"value": 1,
												"boost": 1.0
											}
										}
									},
									"aggregations": {
										"params5sum": {
											"sum": {
												"field": "nestedObject.params5"
											}
										}
									}

								},
								"params9": {
									"reverse_nested": {},
									"aggregations": {
										"params9-1": {
											"filter": {
												"term": {
													"params1": {
														"value": 0,
														"boost": 1.0
													}
												}
											}
										}
									}

								},
								"params2": {
									"filter": {
										"nested": {
											"query": {
												"term": {
													"nestedObject.params2": {
														"value": 1,
														"boost": 1.0
													}
												}
											},
											"path": "nestedObject",
											"ignore_unmapped": false,
											"score_mode": "none",
											"boost": 1.0
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值