ES-QUERY-DSL的使用-用户配置多任务开启合并查询和二次过滤航班信息

查询航班信息时,同一用户可以配置多个任务进行航班信息的查询。配置的一个任务包括区域内航班查询、根据航班号进行查询,也可多个条件任意组合查询航班信息。当对多个任务的数据进行合并进行翻页,并且对于合并的数据需要进一步过滤查询时,多个任务的查询就必须合并到一起进行查询。为了解决这个问题,就需要使用嵌套的BOOL查询。

elasticsearch的QUERY DSL的实现方式如下:

  1. 第一层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
}
  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
}
  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
}
发布了173 篇原创文章 · 获赞 113 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/superman_xxx/article/details/102572194