Jan*_*zny 6 google-app-engine gae-search
在GoogleIO谈论Search API时,我们可以根据地理位置进行搜索.
我找不到合适的字段来存储位置信息.
如何在文档中存储地理位置信息,以便根据距离特定GPS位置的距离发出查询?
2012年6月28日,Google将GeoPoint类集成到Google App Engine搜索API库中,其具体目的是使空间点可搜索.
GeoPoints在搜索文档中存储为GeoField.Google提供此支持文档,概述了GeoPoint与Search API的使用.
以下示例声明了GeoPoint并将其分配给搜索文档中的GeoField.这些新类提供了比下面列出的功能更多的功能,但是这段代码是基本理解如何使用新的空间搜索功能的起点.
使用关联的GeoPoint构建文档
## IMPORTS ##
from google.appengine.api import search
def CreateDocument(content, lat, long):
geopoint = search.GeoPoint(lat, long)
return search.Document(
fields=[
search.HtmlField(name='content', value=content),
search.DateField(name='date', value=datetime.now().date())
search.GeoField(name='location', value=geopoint)
])
Run Code Online (Sandbox Code Playgroud)
搜索GeoPoint文档字段(从Search API文档略微修改)
## IMPORTS ##
from google.appengine.api import search
ndx = search.Index(DOCUMENT_INDEX)
loc = (-33.857, 151.215)
query = "distance(location, geopoint(-33.857, 151.215)) < 4500"
loc_expr = "distance(location, geopoint(-33.857, 151.215))"
sortexpr = search.SortExpression(
expression=loc_expr,
direction=search.SortExpression.ASCENDING, default_value=4501)
search_query = search.Query(
query_string=query,
options=search.QueryOptions(
sort_options=search.SortOptions(expressions=[sortexpr])))
results = index.search(search_query)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2096 次 |
| 最近记录: |