protected <T extends ESEntity> MultiGetRequestBuilder buildMultiGetRequest( Class<T> entityClass, long... osmIds) { ESEntityType type = ESEntityType.valueOf(entityClass); MultiGetRequestBuilder request = client.prepareMultiGet(); for (long osmId : osmIds) { request.add( new Item(indexName, type.getIndiceName(), String.valueOf(osmId)) .fields("centroid", "lengthKm", "areaKm2", "shape", "tags")); } return request; }
/** * Delete an OSM entity. * * <p><b>Warning:</b> please note that deleting {@link Relation} and {@link Bound} is not yet * supported. Trying to delete such {@link Entity} causes this method to throw an {@link * UnsupportedOperationException}. * * @param osmId the OSM id that identifies the Entity * @param entityClass the class (among {@link Node}, {@link Way}, {@link Relation} and {@link * Bound}) of the Entity * @return True if the Entity was deleted, false otherwise (i.e. the Entity was not found) * @throws IllegalArgumentException if the provided entityClass is null or invalid * @throws DaoException if something was wrong during the elasticsearch request */ public <T extends ESEntity> boolean delete(Class<T> entityClass, long osmId) { try { String indiceName = ESEntityType.valueOf(entityClass).getIndiceName(); return client .prepareDelete(indexName, indiceName, Long.toString(osmId)) .execute() .actionGet() .isFound(); } catch (Exception e) { String indiceName = ESEntityType.valueOf(entityClass).getIndiceName(); String message = String.format("Unable to delete entity %s in %s/%s", osmId, indexName, indiceName); throw new DaoException(message, e); } }
/** * Find all OSM entities. * * <p><b>Warning:</b> all objects are retrieved from elasticsearch and mounted in memory. In case * of large OSM data sets, ensure you have allocated enough heap. If you already know what ids to * retrieve, please consider the {@link #findAll(Class, long...)} method instead. * * <p><b>Warning:</b> please note that finding all {@link Relation} and {@link Bound} is not yet * supported. Trying to find such {@link Entity} causes this method to throw an {@link * UnsupportedOperationException}. * * @param entityClass the class (among {@link Node}, {@link Way}, {@link Relation} and {@link * Bound}) of the Entities * @param osmIds an array of OSM id that identifies the Entities. if null, all Entities will be * retrieved (be aware of your heap size) * @return The Entity objects as list if all ids was found * @throws IllegalArgumentException if the provided entityClass is null or invalid * @throws DaoException if something was wrong during the elasticsearch request */ public <T extends ESEntity> List<T> findAll(Class<T> entityClass, long... osmIds) { if (osmIds == null || osmIds.length == 0) return Collections.unmodifiableList(new ArrayList<T>(0)); try { MultiGetRequestBuilder request = buildMultiGetRequest(entityClass, osmIds); return executeMultiGetRequest(entityClass, request); } catch (Exception e) { if (e instanceof DaoException) throw (DaoException) e; String indiceName = ESEntityType.valueOf(entityClass).getIndiceName(); throw new DaoException("Unable to find all " + indiceName + " entities", e); } }