use of org.springframework.data.elasticsearch.core.geo.GeoPoint in project spring-data-elasticsearch by spring-projects.
the class GeoRepositoryIntegrationTests method shouldSaveAndLoadGeoPoints.
@Test
public void shouldSaveAndLoadGeoPoints() {
// given
Point point = new Point(15, 25);
GeoEntity entity = new GeoEntity();
entity.setPointA(point);
entity.setPointB(new GeoPoint(point.getX(), point.getY()));
entity.setPointC(toGeoString(point));
entity.setPointD(toGeoArray(point));
// when
GeoEntity saved = repository.save(entity);
Optional<GeoEntity> result = repository.findById(entity.getId());
// then
assertThat(result).isPresent();
result.ifPresent(geoEntity -> {
assertThat(saved.getPointA().getX()).isEqualTo(geoEntity.getPointA().getX());
assertThat(saved.getPointA().getY()).isEqualTo(geoEntity.getPointA().getY());
assertThat(saved.getPointB().getLat()).isEqualTo(geoEntity.getPointB().getLat());
assertThat(saved.getPointB().getLon()).isEqualTo(geoEntity.getPointB().getLon());
assertThat(saved.getPointC()).isEqualTo(geoEntity.getPointC());
assertThat(saved.getPointD()).isEqualTo(geoEntity.getPointD());
});
}
use of org.springframework.data.elasticsearch.core.geo.GeoPoint in project spring-data-elasticsearch by spring-projects.
the class CriteriaFilterProcessor method withinQuery.
private QueryBuilder withinQuery(String fieldName, Object[] valArray) {
GeoDistanceQueryBuilder filter = QueryBuilders.geoDistanceQuery(fieldName);
Assert.noNullElements(valArray, "Geo distance filter takes 2 not null elements array as parameter.");
Assert.isTrue(valArray.length == 2, "Geo distance filter takes a 2-elements array as parameter.");
Assert.isTrue(valArray[0] instanceof GeoPoint || valArray[0] instanceof String || valArray[0] instanceof Point, "First element of a geo distance filter must be a GeoPoint, a Point or a text");
Assert.isTrue(valArray[1] instanceof String || valArray[1] instanceof Distance, "Second element of a geo distance filter must be a text or a Distance");
StringBuilder dist = new StringBuilder();
if (valArray[1] instanceof Distance) {
extractDistanceString((Distance) valArray[1], dist);
} else {
dist.append((String) valArray[1]);
}
if (valArray[0] instanceof GeoPoint) {
GeoPoint loc = (GeoPoint) valArray[0];
filter.point(loc.getLat(), loc.getLon()).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
} else if (valArray[0] instanceof Point) {
GeoPoint loc = GeoPoint.fromPoint((Point) valArray[0]);
filter.point(loc.getLat(), loc.getLon()).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
} else {
String loc = (String) valArray[0];
if (loc.contains(",")) {
String[] c = loc.split(",");
filter.point(Double.parseDouble(c[0]), Double.parseDouble(c[1])).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
} else {
filter.geohash(loc).distance(dist.toString()).geoDistance(GeoDistance.PLANE);
}
}
return filter;
}
use of org.springframework.data.elasticsearch.core.geo.GeoPoint in project spring-data-elasticsearch by spring-projects.
the class MappingElasticsearchConverterUnitTests method shouldWriteEntityWithListOfGeoPoints.
// DATAES-857
@Test
void shouldWriteEntityWithListOfGeoPoints() throws JSONException {
GeoPointListEntity entity = new GeoPointListEntity();
entity.setId("42");
List<GeoPoint> locations = Arrays.asList(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
entity.setLocations(locations);
String expected = //
"{\n" + //
" \"id\": \"42\",\n" + //
" \"locations\": [\n" + //
" {\n" + //
" \"lat\": 12.34,\n" + //
" \"lon\": 23.45\n" + //
" },\n" + //
" {\n" + //
" \"lat\": 34.56,\n" + //
" \"lon\": 45.67\n" + //
" }\n" + //
" ]\n" + //
"}";
Document document = Document.create();
mappingElasticsearchConverter.write(entity, document);
String json = document.toJson();
assertEquals(expected, json, false);
}
use of org.springframework.data.elasticsearch.core.geo.GeoPoint in project spring-data-elasticsearch by spring-projects.
the class MappingElasticsearchConverterUnitTests method shouldReadEntityWithListOfGeoPoints.
// DATAES-857
@Test
void shouldReadEntityWithListOfGeoPoints() {
String json = //
"{\n" + //
" \"id\": \"42\",\n" + //
" \"locations\": [\n" + //
" {\n" + //
" \"lat\": 12.34,\n" + //
" \"lon\": 23.45\n" + //
" },\n" + //
" {\n" + //
" \"lat\": 34.56,\n" + //
" \"lon\": 45.67\n" + //
" }\n" + //
" ]\n" + //
"}";
Document document = Document.parse(json);
GeoPointListEntity entity = mappingElasticsearchConverter.read(GeoPointListEntity.class, document);
assertThat(entity.id).isEqualTo("42");
assertThat(entity.locations).containsExactly(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
}
use of org.springframework.data.elasticsearch.core.geo.GeoPoint in project spring-native by spring-projects-experimental.
the class CLR method run.
@Override
public void run(String... args) throws Exception {
{
System.out.println("refresh index");
repository.deleteAll();
operations.indexOps(Conference.class).delete();
operations.indexOps(Conference.class).create();
}
{
System.out.println("\n--- REPOSITORY ---");
// Save data sample
repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London").keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build());
repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London").keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build());
repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin").keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999)).build());
repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build());
repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build());
System.out.println("repository.count(): " + repository.count());
}
{
System.out.println("\n--- CUSTOM REPOSITORY ---");
SearchPage<Conference> searchPage = repository.findBySomeCustomImplementation("eXchange", PageRequest.of(0, 10));
System.out.println("custom implementation finder.size(): " + searchPage.getSearchHits().getTotalHits());
}
String expectedDate = "2014-10-29";
String expectedWord = "java";
CriteriaQuery query = new CriteriaQuery(new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
{
System.out.println("\n--- TEMPLATE FIND ---");
SearchHits<Conference> result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
System.out.println("result.size(): " + result.getSearchHits().size());
}
{
System.out.println("\n--- REPOSITORY FINDER ---");
List<Conference> result = repository.findByKeywordsContaining("spring");
System.out.println("result.size(): " + result.size());
}
{
System.out.println("\n--- REACTIVE TEMPLATE ---");
System.out.println("reactiveTemplate.count(): " + reactiveOps.count(Query.findAll(), Conference.class, IndexCoordinates.of("conference-index")).block());
}
{
System.out.println("\n--- REACTIVE REPOSITORY ---");
System.out.println("reactiveRepository.count(): " + reactiveRepository.count().block());
}
{
// does currently not work in SD ES - wrong reactor-netty dependency
// System.out.println("\n--- REACTIVE REPOSITORY FINDER ---");
// System.out.println("result.size(): " + reactiveRepository.findByKeywordsContaining("spring").collectList().block().size());
}
System.out.println("DONE");
}
Aggregations