// Build location index
 private com.google.appengine.api.search.Index getLocationIndex() {
   IndexSpec indexSpec =
       IndexSpec.newBuilder()
           .setName(LOCATION_INDEX)
           .setConsistency(Consistency.PER_DOCUMENT)
           .build();
   return SearchServiceFactory.getSearchService().getIndex(indexSpec);
 }
  private void procurar(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {
      String search = req.getParameter("q");
      String lat = req.getParameter("lat");
      String lng = req.getParameter("lng");
      String distancia = req.getParameter("distancia");
      IndexSpec indexSpec = IndexSpec.newBuilder().setName("produtos").build();
      Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
      String query_string = "";
      if (!"".equals(search)) query_string = search + " AND ";
      query_string += "distance(posicao, geopoint(" + lat + ", " + lng + ")) < " + distancia;
      Results<ScoredDocument> results = index.search(query_string);

      boolean first = true;
      resp.getWriter().write("[");
      for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
        ScoredDocument doc = (ScoredDocument) iterator.next();
        if (!first) resp.getWriter().write(", ");
        first = false;
        boolean first_field = true;
        resp.getWriter().write("{");
        for (Iterator fields = doc.getFields().iterator(); fields.hasNext(); ) {
          if (!first_field) resp.getWriter().write(", ");
          first_field = false;
          Field field = (Field) fields.next();
          if ("posicao".equals(field.getName())) {
            resp.getWriter().write("\"latitude\": ");
            resp.getWriter().write("\"" + field.getGeoPoint().getLatitude() + "\", ");
            resp.getWriter().write("\"longitude\": ");
            resp.getWriter().write("\"" + field.getGeoPoint().getLongitude() + "\"");
          } else {
            resp.getWriter().write("\"" + field.getName() + "\": ");
            resp.getWriter().write("\"" + field.getText() + "\"");
          }
        }
        resp.getWriter().write("}");
      }
      resp.getWriter().write("]");
    } catch (SearchException e) {
      // handle exception...
    }
  }
 /**
  * Shortcut notation to easily get an index from the search service.
  *
  * <pre><code>
  *     def index = search.index("books")
  * </code></pre>
  *
  * @param search the search service
  * @param indexName the name of the index
  * @return an index
  */
 public static Index index(SearchService search, String indexName) {
   return search.getIndex(IndexSpec.newBuilder().setName(indexName).build());
 }
 /** Extension point allowing the Index implementation to be modified */
 protected Index getIndex() {
   SearchService searchService = SearchServiceFactory.getSearchService();
   return searchService.getIndex(IndexSpec.newBuilder().setName(indexName));
 }