// Search in index for a query private CursorPage<DProduct, String> searchInIndexWithQuery( com.google.appengine.api.search.Query query, Index index) { CursorPage<DProduct, String> cursorPage = null; try { // Query the index. Results<ScoredDocument> results = index.search(query); // Collect all the primary keys Collection<String> ids = new ArrayList<String>(); for (ScoredDocument document : results) { ids.add(document.getId()); } if (ids.size() != 0) { // We got results, get the places from datastore Iterator<DProduct> dProductIterator = queryByPrimaryKeys(null, ids).iterator(); Collection<DProduct> dProducts = new ArrayList<DProduct>(ids.size()); while (dProductIterator.hasNext()) { dProducts.add(dProductIterator.next()); } // Build a cursor page cursorPage = new CursorPage<DProduct, String>(); cursorPage.setCursorKey(results.getCursor().toWebSafeString()); cursorPage.setItems(dProducts); } } catch (SearchException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) { LOG.error("Search index failed"); // TODO: Error handling missing } throw new SearchException("Searching index failed"); } return cursorPage; }
@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; }
@Override public String from(ScoredDocument from) { return from.getId(); }