SpringBoot Data ElasticSearch 4.* 使用 GeoPoint 坐标位置获取范围内数据

一.前言

近期项目需要用到ElasticSearch根据坐标获取范围内的数据,以前有所接触的仅仅是老版本,去了解了一下新版本,发现很多方法已经处于弃用状态。而在网络上苦苦搜寻,却很少见到关于4.*版本关于GeoPoint新型写法的详细文章,在官方文档中,对于新人而言却又很难理解使用。本文将记录使用GeoPoint根据地理位置获取数据的案例。

注:作者目前是边学边用,有什么不对的地方请指出

二.使用案例

作者版本信息:Elasticsearch 7.17.3

  1. 依赖版本

参考官方文档:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface

依赖搜索:https://mvnrepository.com

根据自己的ES版本对照以下依赖版本

Spring Data Release Train

Spring Data Elasticsearch

Elasticsearch

Spring Framework

Spring Boot

2022.0 (Turing)

5.0.x

8.5.3

6.0.x

3.0.x

2021.2 (Raj)

4.4.x

7.17.3

5.3.x

2.7.x

2021.1 (Q)

4.3.x

7.15.2

5.3.x

2.6.x

2021.0 (Pascal)

4.2.x[1]

7.12.0

5.3.x

2.5.x

2020.0 (Ockham)[1]

4.1.x[1]

7.9.3

5.3.2

2.4.x

Neumann[1]

4.0.x[1]

7.6.2

5.2.12

2.3.x

Moore[1]

3.2.x[1]

6.8.12

5.2.12

2.2.x

Lovelace[1]

3.1.x[1]

6.2.2

5.1.19

2.1.x

Kay[1]

3.0.x[1]

5.5.0

5.0.13

2.0.x

Ingalls[1]

2.1.x[1]

2.4.0

4.3.25

1.5.x

  1. 依赖

父依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.9</version>
    <relativePath/>
</parent>

直接导入spring-boot-starter-data-elasticsearch依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

不需要指定版本,会根据你的SpringBoot版本自动指定spring-data-elasticsearch 版本

  1. YMAL配置类

相比较于之前的老版本,可以直接使用uris来指定地址

spring:
  elasticsearch:
    uris: http://localhost:9200
  1. 实体类Document

这里用到了lombok

GeoPoint须要用@GeoPointField注解标注

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "test",createIndex = true)
public class Test {
    @Id
    Long id;
    @Field(type = FieldType.Text)
    String name;
    //坐标对象
    @GeoPointField
    GeoPoint location;
}

  1. 访问层Repository


import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;

import java.util.List;

public interface TestRepository extends ElasticsearchRepository<Test,Long> {
    /**
     * 根据范围获取数据
     * @param point x y 对应经纬度
     * @param distance 范围
     * @return
     */
    List<Test> findByLocationNear(Point point, Distance distance);
}

此处的方法用法可以根据需求来

例如:findByLocationNearandAndName(Point point, Distance distance,String name);

返回值可以参考:

Return type

Description

void

Denotes no return value.

Primitives

Java primitives.

Wrapper types

Java wrapper types.

T

A unique entity. Expects the query method to return one result at most. If no result is found, null is returned. More than one result triggers an IncorrectResultSizeDataAccessException.

Iterator<T>

An Iterator.

Collection<T>

A Collection.

List<T>

A List.

...

更多参考官方文档

  1. 使用

@SpringBootApplication
@EnableElasticsearchRepositories
public class Run {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Run.class, args);
        TestRepository testRepository = run.getBean(TestRepository.class);
        Test test = testRepository.save(new Test(new Long(10001), "来打我啊", new GeoPoint().reset(50.2, 50.222)));
        System.out.println(test.toString());

        List<Test> list = testRepository.findByLocationNear(new Point(50.0,50.0), new Distance(100, Metrics.KILOMETERS));
        System.out.println("100KM内有:");
        list.forEach(ts -> {
            System.out.println(ts.toString());
        });
        List<Test> list2 = testRepository.findByLocationNear(new Point(50.0,50.0), new Distance(10, Metrics.KILOMETERS));
        System.out.println("10KM内有:");
        list2.forEach(ts -> {
            System.out.println(ts.toString());
        });
    }
}

new Distance(参1参2);

参1:距离

参2:单位

调用新增 结果:

调用查询 结果:

结束(有问题可以评论区或私信)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值