匹配与边界框相交的geo_point和geo_shape值。
示例
假设以下文档被索引:
PUT /my_locations
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}
PUT /my_locations/_doc/1
{
  "pin": {
    "location": {
      "lat": 40.12,
      "lon": -71.34
    }
  }
}
PUT /my_geoshapes
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_shape"
          }
        }
      }
    }
  }
}
PUT /my_geoshapes/_doc/1
{
  "pin": {
    "location": {
      "type" : "polygon",
      "coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
    }
  }
}
使用geo_bounding_box筛选器匹配与边界框相交的geo_point值。要定义方框,请提供两个对角的地理点值。
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.01,
              "lon": -71.12
            }
          }
        }
      }
    }
  }
}
使用相同的过滤器匹配与边界框相交的geo_shape值:
GET my_geoshapes/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.01,
              "lon": -71.12
            }
          }
        }
      }
    }
  }
}
要匹配geo_point和geo_shape值,搜索这两个索引:
GET my_locations,my_geoshapes/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.01,
              "lon": -71.12
            }
          }
        }
      }
    }
  }
}
查询选项
| 选项 | 说明 | 
| _name | 可选名称字段来标识过滤器 | 
| validation_method | 设置为IGNORE_MALFORMED以接受无效纬度或经度的地理点,设置为COERCE也试图推断正确的纬度或经度。(默认是STRICT)。 | 
支持的格式
就像geo_point类型可以接受geo点的不同表示方式一样,过滤器也可以接受它:
lat lon作为属性
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.01,
              "lon": -71.12
            }
          }
        }
      }
    }
  }
}
lat lon作为数组
格式为[lon, lat],注意,这里的lon/lat的顺序是为了符合GeoJSON。
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": [ -74.1, 40.73 ],
            "bottom_right": [ -71.12, 40.01 ]
          }
        }
      }
    }
  }
}
lat lon作为字符串
格式为lat,lon。
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": "40.73, -74.1",
            "bottom_right": "40.01, -71.12"
          }
        }
      }
    }
  }
}
wkt
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
          }
        }
      }
    }
  }
}
geohash
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": "dr5r9ydj2y73",
            "bottom_right": "drj7teegpus6"
          }
        }
      }
    }
  }
}
当geohash用于指定边界框的边界时,geohash被视为矩形。边界框的定义方式是,它的左上角对应于top_left参数中指定的geohash的左上角,它的右下角定义为bottom_right参数中指定的geohash的右下角。
为了指定与geohash的整个区域匹配的边界框,可以在top_left和bottom_right参数中指定geohash:
GET my_locations/_search
{
  "query": {
    "geo_bounding_box": {
      "pin.location": {
        "top_left": "dr",
        "bottom_right": "dr"
      }
    }
  }
}
在本例中,geohash dr将生成边界框查询,其左上角为45.0,-78.75,右下角为39.375,-67.5。
顶点
边界框的顶点可以通过top_left和bottom_right或top_right和bottom_left参数设置。更多的名字,支持topLeft, bottomRight, topRight和bottomLeft。可以使用简单的名称top、left、bottom和right分别设置值,而不是成对设置值。
GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top": 40.73,
            "left": -74.1,
            "bottom": 40.01,
            "right": -71.12
          }
        }
      }
    }
  }
}
多位置文档
过滤器可以处理每个文档的多个位置/点。一旦一个位置/点匹配过滤器,该文档将被包含在过滤器中
忽略未映射的
当ignore_unmapped选项设置为true时,将忽略未映射字段,并且不会匹配此查询的任何文档。这在查询可能具有不同映射的多个索引时很有用。当设置为false(默认值)时,如果该字段没有映射,查询将抛出异常。
精度说明
地理点的精度有限,在索引时间内总是四舍五入。在查询期间,边界框的上边界向下舍入,而下边界向上舍入。因此,由于舍入误差,沿下界(边界框的底部和左侧边缘)的点可能不会进入边界框。与此同时,查询可能会选择上界(顶部和右侧边缘)旁边的点,即使它们位于边缘之外。四舍五入误差应在纬度上小于4.20 -8度,在经度上小于8.39 -8度,即使在赤道上也小于1cm误差。
地理形状也有有限的精度,由于四舍五入。沿着边界框底部和左边边缘的Geoshape边缘可能不匹配一个geo_bounding_box查询。Geoshape边缘略偏在盒子顶部和右边边缘可能仍然匹配查询。
 
                   
                   
                   
                   
                             这篇博客介绍了如何在Elasticsearch中使用`geo_bounding_box`筛选器来匹配`geo_point`和`geo_shape`类型的地理位置数据。通过示例展示了如何定义边界框,以及支持的不同格式,如经纬度、数组、字符串、WKT和geohash。同时,还提到了查询选项、精度问题以及如何处理多位置文档和未映射字段的情况。
这篇博客介绍了如何在Elasticsearch中使用`geo_bounding_box`筛选器来匹配`geo_point`和`geo_shape`类型的地理位置数据。通过示例展示了如何定义边界框,以及支持的不同格式,如经纬度、数组、字符串、WKT和geohash。同时,还提到了查询选项、精度问题以及如何处理多位置文档和未映射字段的情况。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   3425
					3425
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            