查詢航班信息時,同一用戶可以配置多個任務進行航班信息的查詢。配置的一個任務包括區域內航班查詢、根據航班號進行查詢,也可多個條件任意組合查詢航班信息。當對多個任務的數據進行合併進行翻頁,並且對於合併的數據需要進一步過濾查詢時,多個任務的查詢就必須合併到一起進行查詢。爲了解決這個問題,就需要使用嵌套的BOOL查詢。
elasticsearch的QUERY DSL的實現方式如下:
- 第一層bool中包含must數組,或條件和must條件添加到這個數組中,先在must中添加過濾條件如下(即多任務數據合併後再過濾的條件)。
// 多任務數據合併後再過濾的條件
{
"sort": [
{
"pubtime": "desc"
}
],
"_source": [
"callsign",
"type",
"mode_s",
"country",
"heading",
"op",
"sqk",
"manufacturer",
"random_code",
"is_am",
"source"
],
"query": {
"bool": {
"must": [
{},
{
"range": {
"pubtime": {
"gte": "2019-10-08 00:00:00",
"lte": "2019-10-08 15:40:46"
}
}
},
{
"term": {
"site": "radarbox24.com"
}
},
{
"query_string": {
"query": "+aircraft:*9V-SNB* +mode_s:*76CEEA* +type:*B77*"
}
}
]
}
},
"from": 0,
"size": 1
}
- 添加多個任務的過濾條件,用戶配置的多個任務在查詢時是或的關係。
// 多個任務的查詢方式:
// 最內層的BOOL表示一個任務下可以配置多個區域;
// 第二層BOOL表示在should裏面配置多個任務;
// 第三層BOOL表示一個任務。
{
"sort": [
{
"pubtime": "desc"
}
],
"_source": [
"callsign",
"type",
"mode_s",
"country",
"heading",
"op",
"sqk",
"manufacturer",
"random_code",
"is_am",
"source"
],
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 35.23916666666667,
"lat": -42.36388888888889
},
"top_left": {
"lon": -12.889166666666666,
"lat": -22.859166666666667
}
}
}
},
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 116.83948,
"lat": 39.782209
},
"top_left": {
"lon": 115.919615,
"lat": 40.333563
}
}
}
}
]
}
},
{
"query_string": {
"query": "+((mode_s:06A0F5 AND aircraft:A7-ALA) OR (mode_s:70A9FC AND aircraft:9N-AMU))"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 35.23916666666667,
"lat": -42.36388888888889
},
"top_left": {
"lon": -12.889166666666666,
"lat": -22.859166666666667
}
}
}
}
]
}
},
{
"query_string": {
"query": "+((mode_s:AC3DAB AND aircraft:N888XS) OR (mode_s:70A9FC AND aircraft:9N-AMU))"
}
}
]
}
}
]
}
},
{},
{},
{}
]
}
},
"from": 0,
"size": 1
}
- 用戶多任務合併查詢,並對合並的數據進行二次過濾,完整實現案例。
// 完整實現案例
{
"sort": [
{
"pubtime": "desc"
}
],
"_source": [
"callsign",
"type",
"mode_s",
"country",
"heading",
"op",
"sqk",
"manufacturer",
"random_code",
"is_am",
"source"
],
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 35.23916666666667,
"lat": -42.36388888888889
},
"top_left": {
"lon": -12.889166666666666,
"lat": -22.859166666666667
}
}
}
},
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 116.83948,
"lat": 39.782209
},
"top_left": {
"lon": 115.919615,
"lat": 40.333563
}
}
}
}
]
}
},
{
"query_string": {
"query": "+((mode_s:06A0F5 AND aircraft:A7-ALA) OR (mode_s:70A9FC AND aircraft:9N-AMU))"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_bounding_box": {
"type": "indexed",
"location_point": {
"bottom_right": {
"lon": 35.23916666666667,
"lat": -42.36388888888889
},
"top_left": {
"lon": -12.889166666666666,
"lat": -22.859166666666667
}
}
}
}
]
}
},
{
"query_string": {
"query": "+((mode_s:AC3DAB AND aircraft:N888XS) OR (mode_s:70A9FC AND aircraft:9N-AMU))"
}
}
]
}
}
]
}
},
{
"range": {
"pubtime": {
"gte": "2019-10-08 00:00:00",
"lte": "2019-10-08 15:40:46"
}
}
},
{
"term": {
"site": "chinaadarbox99.com"
}
},
{
"query_string": {
"query": "+aircraft:*9V-SNB* +mode_s:*76CEEA* +type:*B77*"
}
}
]
}
},
"from": 0,
"size": 1
}