コード例 #1
0
ファイル: SearchExtensions.java プロジェクト: anthonyu/gaelyk
  /**
   * 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;
    }
  }
コード例 #2
0
  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...
    }
  }
コード例 #3
0
 @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;
 }