6

There where indications in the GoogleIO talk on Search API that we can do searches based on geolocation.

I can't find an appropriate field to store location info.

How can I store geolocation info in the document so I could issue queries based on distance from a particular GPS location?

Janusz Skonieczny
  • 17,642
  • 11
  • 55
  • 63
  • I don't believe there is a good answer to this. At the moment, the Search API is in an Experimental state. I've search through the documentation and the sample code(http://goo.gl/Yb7n1). There isn't much information that is available that either addresses this functionality, or allows you to construct a good mechanism for solving this problem. I recommend checking out the Search API's issue tracker page (http://goo.gl/LczvP). Someone has requested this functionality and it is currently ranked #3 (http://goo.gl/SIDqA) in the list of issues. – RLH May 16 '12 at 13:35
  • I think so myself, but I wasn't sure. The Google IO talk is somewhat misleading there is [mention of GeoPoint](http://youtu.be/7B7FyU9wW8Y?t=12m30s) but there is no such field. I did not know it was not released yet or API implementation have changed. – Janusz Skonieczny May 16 '12 at 14:03

1 Answers1

7

On June 28, 2012, Google integrated the GeoPoint class 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 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)
RLH
  • 15,230
  • 22
  • 98
  • 182