Example #1
0
  /**
   * Get a document field raw value or list of raw values.
   *
   * <pre><code>
   *      assert document.title = "Big bad wolf"
   *      assert document.keyword[0] == "wolf"
   *      assert document.keyword[1] == "red hook"
   * </code></pre>
   *
   * @param document the document
   * @param fieldName the field name
   * @return a raw value or a list of raw values if the field is multivalued
   */
  public static Object get(Document document, String fieldName) {
    @SuppressWarnings("unchecked")
    List<Field> fields = (List<Field>) DefaultGroovyMethods.collect(document.getFields(fieldName));

    switch (fields.size()) {
      case 0:
        if (document instanceof ScoredDocument) {
          List<Object> exps = new ArrayList<Object>();
          for (Field f : ((ScoredDocument) document).getExpressions()) {
            if (f.getName().equals(fieldName)) {
              exps.add(getFieldRawValue(f));
            }
          }

          if (exps.size() == 0) {
            return null;
          }
          if (exps.size() == 1) {
            return exps.get(0);
          }
          return exps;
        }
        return null;
      case 1:
        return getFieldRawValue(fields.get(0));
      default:
        List<Object> exps = new ArrayList<Object>();
        for (Field f : fields) {
          exps.add(getFieldRawValue(f));
        }
        return exps;
    }
  }
Example #2
0
 @Override
 public Document toDocument() {
   Document document =
       Document.newBuilder()
           .setId(id)
           .addField(Field.newBuilder().setName(ComentarioFields.id.name()).setText(id))
           .addField(
               Field.newBuilder().setName(ComentarioFields.conteudo.name()).setText(conteudo))
           .addField(Field.newBuilder().setName(ComentarioFields.usuario.name()).setText(usuario))
           .build();
   return document;
 }
 // Index the geo location
 private void indexProduct(DProduct dProduct) {
   if (null != dProduct.getLocation()) {
     GeoPoint geoPoint =
         new GeoPoint(dProduct.getLocation().getLatitude(), dProduct.getLocation().getLongitude());
     int averageRating =
         (null != dProduct.getRatingAverage()) ? dProduct.getRatingAverage().getRating() : -1;
     Document.Builder locationBuilder =
         Document.newBuilder()
             .setId(dProduct.getProductId())
             .addField(Field.newBuilder().setName("location").setGeoPoint(geoPoint))
             .addField(Field.newBuilder().setName("averageRating").setNumber(averageRating))
             .addField(Field.newBuilder().setName("likeCount").setNumber(dProduct.getLikeCount()))
             .addField(Field.newBuilder().setName("thumbsUp").setNumber(dProduct.getThumbsUp()));
     getLocationIndex().add(locationBuilder.build());
   }
 }
 <F> Field buildField(SearchMetadata<T, K> metadata, String field, Object value) {
   com.google.appengine.api.search.Field.Builder fieldBuilder =
       Field.newBuilder().setName(metadata.getEncodedFieldName(field));
   IndexType indexType = metadata.getIndexType(field);
   FieldMediator<F> fieldMediator = fieldMediators.get(indexType);
   F normalised = fieldMediator.normalise(transformerManager, value);
   fieldMediator.setValue(fieldBuilder, normalised);
   return fieldBuilder.build();
 }
Example #5
0
 private static Object getFieldRawValue(Field field) {
   switch (field.getType()) {
     case ATOM:
       return field.getAtom();
     case DATE:
       return field.getDate();
     case HTML:
       return field.getHTML();
     case NUMBER:
       return field.getNumber();
     case TEXT:
       return field.getText();
     case GEO_POINT:
       return field.getGeoPoint();
   }
   return null;
 }
 @Override
 public Map<String, Object> from(ScoredDocument from) {
   Map<String, Object> results = new HashMap<String, Object>();
   results.put("___id___", from.getId());
   for (Field field : from.getFields()) {
     FieldType fieldType = field.getType();
     Object value = null;
     if (FieldType.TEXT.equals(fieldType)) {
       value = field.getText();
     } else if (FieldType.NUMBER.equals(fieldType)) {
       value = field.getNumber();
     } else if (FieldType.DATE.equals(fieldType)) {
       value = field.getDate();
     } else if (FieldType.ATOM.equals(fieldType)) {
       value = field.getAtom();
     } else if (FieldType.HTML.equals(fieldType)) {
       value = field.getHTML();
     } else if (FieldType.GEO_POINT.equals(fieldType)) {
       value = field.getGeoPoint();
     }
     results.put(field.getName(), value);
   }
   return results;
 }
  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...
    }
  }