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; }
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... } }
@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; }