多元索引支持三种地理位置查询方式,分别是GeoBoundingBoxQuery、GeoDistanceQuery和GeoPolygonQuery。

GeoBoundingBoxQuery

使用GeoBoundingBoxQuery进行地理边界框查询。根据一个矩形范围的地理位置边界条件查询表中的数据,当一个地理位置点落在给出的矩形范围内时,满足查询条件。

/// <summary>
/// geo_type_col是GeoPoint类型,查询表中geo_type_col这一列的值在左上角为"10,0", 右下角为"0,10"的矩形范围内的数据
/// </summary>
/// <param name="client"></param>
public static void GeoBoundingBoxQuery(OTSClient client)
{
    SearchQuery searchQuery = new SearchQuery();
    GeoBoundingBoxQuery geoBoundingBoxQuery = new GeoBoundingBoxQuery(); // 设置查询类型为GeoBoundingBoxQuery
    geoBoundingBoxQuery.FieldName = Geo_type_col; // 设置比较哪个字段的值
    geoBoundingBoxQuery.TopLeft = "10,0"; // 设置矩形左上角
    geoBoundingBoxQuery.BottomRight = "0,10"; // 设置矩形右下角
    searchQuery.Query = geoBoundingBoxQuery;

    SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

    var columnsToGet = new ColumnsToGet();
    columnsToGet.Columns = new List<string> { Geo_type_col };  //设置返回Col_GeoPoint这一列
    searchRequest.ColumnsToGet = columnsToGet;

    SearchResponse response = client.Search(searchRequest);
    Console.WriteLine(response.TotalCount);
}

GeoDistanceQuery

使用GeoDistanceQuery进行地理距离查询。根据一个中心点和距离条件查询表中的数据,当一个地理位置点到指定的中心点的距离不超过指定的值时,满足查询条件。

/// <summary> 
///  查询表中geo_type_col这一列的值距离中心点不超过一定距离的数据。
/// </summary>
/// <param name="client"></param>
public static void GeoDistanceQuery(OTSClient client)
{
    SearchQuery searchQuery = new SearchQuery();
    GeoDistanceQuery geoDistanceQuery = new GeoDistanceQuery();  // 设置查询类型为GeoDistanceQuery
    geoDistanceQuery.FieldName = Geo_type_col;
    geoDistanceQuery.CenterPoint = "10,11"; // 设置中心点
    geoDistanceQuery.DistanceInMeter = 10000; // 设置到中心点的距离条件,不超过50米
    searchQuery.Query = geoDistanceQuery;

    SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

    ColumnsToGet columnsToGet = new ColumnsToGet();
    columnsToGet.Columns = new List<string>() { Geo_type_col };  //设置返回Col_GeoPoint这一列
    searchRequest.ColumnsToGet = columnsToGet;

    SearchResponse response = client.Search(searchRequest);
    Console.WriteLine(response.TotalCount);
}

GeoPolygonQuery

使用GeoPolygonQuery进行地理多边形查询。根据一个多边形范围条件查询表中的数据,当一个地理位置点落在指定的多边形内时,满足查询条件。

/// <summary>
/// 查询表中geo_type_col这一列的值在一个给定多边形范围内的数据。
/// </summary>
/// <param name="client"></param>
public static void GeoPolygonQuery(OTSClient client)
{
    SearchQuery searchQuery = new SearchQuery();
    GeoPolygonQuery geoPolygonQuery = new GeoPolygonQuery();  // 设置查询类型为GeoPolygonQuery
    geoPolygonQuery.FieldName = Geo_type_col;
    geoPolygonQuery.Points = new List<string>() { "0,0", "10,0", "10,10" }; // 设置多边形的顶点
    searchQuery.Query = geoPolygonQuery;

    SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);

    ColumnsToGet columnsToGet = new ColumnsToGet();
    columnsToGet.Columns = new List<string>() { Geo_type_col };  //设置返回Col_GeoPoint这一列
    searchRequest.ColumnsToGet = columnsToGet;

    SearchResponse response = client.Search(searchRequest);
    Console.WriteLine(response.TotalCount);
}