How to store lat-lon geolocation point in a document for GAE search?
Question: There where indications in the [GoogleIO talk on Search
API](http://youtu.be/7B7FyU9wW8Y?t=26m10s) that we can do searches based on
geolocation.

I can't find an [appropriate
field](https://developers.google.com/appengine/docs/python/search/fieldclasses)
to store location info.

How can I store geolocation info in the
[document](https://developers.google.com/appengine/docs/python/search/documentclass)
so I could issue queries based on distance from a particular GPS location?


Answer: On June 28, 2012, Google integrated the [GeoPoint
class](https://developers.google.com/appengine/docs/python/search/geopointclass)
into the Google App Engine Search API library with the specific intent of
making spatial points searchable.

GeoPoints are stored as GeoFields within the Search Document. Google provides
[this support
documentation](https://developers.google.com/appengine/docs/python/search/overview#Performing_Location-
Based_Searches) outlining the use of the GeoPoint with the Search API.

The following example declares a GeoPoint and assigns it to a GeoField in a
Search Document. These new classes provide a lot more functionality than what
is listed below, but this code is a starting point for a basic understanding
of how to use the new spatial search functionality..

**Constructing a document with an associated 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)
               ])
    

**Searching the GeoPoint document field (Slightly modified from the Search API
docs)**

    
    
    ## 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)
    

