Search in sources :

Example 6 with GeoPoint

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());
    });
}
Also used : GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) Point(org.springframework.data.geo.Point) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 7 with GeoPoint

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;
}
Also used : GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) GeoDistanceQueryBuilder(org.elasticsearch.index.query.GeoDistanceQueryBuilder) GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) Point(org.springframework.data.geo.Point) GeoDistance(org.elasticsearch.common.geo.GeoDistance) Distance(org.springframework.data.geo.Distance)

Example 8 with GeoPoint

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);
}
Also used : GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) GeoJsonLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonLineString) GeoJsonMultiLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 9 with GeoPoint

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));
}
Also used : GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) GeoJsonLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonLineString) GeoJsonMultiLineString(org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 10 with GeoPoint

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");
}
Also used : SearchPage(org.springframework.data.elasticsearch.core.SearchPage) GeoPoint(org.springframework.data.elasticsearch.core.geo.GeoPoint) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) List(java.util.List) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) SearchHits(org.springframework.data.elasticsearch.core.SearchHits)

Aggregations

GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)18 Test (org.junit.jupiter.api.Test)15 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)12 Point (org.springframework.data.geo.Point)8 Distance (org.springframework.data.geo.Distance)5 Document (org.springframework.data.elasticsearch.core.document.Document)2 GeoBox (org.springframework.data.elasticsearch.core.geo.GeoBox)2 GeoJsonLineString (org.springframework.data.elasticsearch.core.geo.GeoJsonLineString)2 GeoJsonMultiLineString (org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString)2 Box (org.springframework.data.geo.Box)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GeoDistance (org.elasticsearch.common.geo.GeoDistance)1 GeoDistanceQueryBuilder (org.elasticsearch.index.query.GeoDistanceQueryBuilder)1 Sort (org.springframework.data.domain.Sort)1 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)1 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)1 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)1 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)1 GeoDistanceOrder (org.springframework.data.elasticsearch.core.query.GeoDistanceOrder)1