国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot Data ElasticSearch 4.* 使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot Data ElasticSearch 4.* 使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一.前言

近期項(xiàng)目需要用到ElasticSearch根據(jù)坐標(biāo)獲取范圍內(nèi)的數(shù)據(jù),以前有所接觸的僅僅是老版本,去了解了一下新版本,發(fā)現(xiàn)很多方法已經(jīng)處于棄用狀態(tài)。而在網(wǎng)絡(luò)上苦苦搜尋,卻很少見到關(guān)于4.*版本關(guān)于GeoPoint新型寫法的詳細(xì)文章,在官方文檔中,對于新人而言卻又很難理解使用。本文將記錄使用GeoPoint根據(jù)地理位置獲取數(shù)據(jù)的案例。

注:作者目前是邊學(xué)邊用,有什么不對的地方請指出

二.使用案例

作者版本信息:Elasticsearch 7.17.3

  1. 依賴版本

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

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

根據(jù)自己的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>

直接導(dǎo)入spring-boot-starter-data-elasticsearch依賴

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

不需要指定版本,會(huì)根據(jù)你的SpringBoot版本自動(dòng)指定spring-data-elasticsearch 版本

SpringBoot Data ElasticSearch 4.*  使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)
  1. YMAL配置類

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

spring:
  elasticsearch:
    uris: http://localhost:9200
  1. 實(shí)體類Document

這里用到了lombok

GeoPoint須要用@GeoPointField注解標(biāo)注

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "test",createIndex = true)
public class Test {
    @Id
    Long id;
    @Field(type = FieldType.Text)
    String name;
? ? //坐標(biāo)對象
    @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> {
? ? /**
     * 根據(jù)范圍獲取數(shù)據(jù)
     * @param point x y 對應(yīng)經(jīng)緯度
     * @param distance 范圍
     * @return
     */
    List<Test> findByLocationNear(Point point, Distance distance);
}

此處的方法用法可以根據(jù)需求來

例如: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.

...

更多參考官方文檔

SpringBoot Data ElasticSearch 4.*  使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)
  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內(nèi)有:");
        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內(nèi)有:");
        list2.forEach(ts -> {
            System.out.println(ts.toString());
        });
? ? }
}

new Distance(參1,參2);

參1:距離

參2:單位

調(diào)用新增 結(jié)果:

SpringBoot Data ElasticSearch 4.*  使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)

調(diào)用查詢 結(jié)果:

SpringBoot Data ElasticSearch 4.*  使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)

結(jié)束(有問題可以評論區(qū)或私信)文章來源地址http://www.zghlxwxcb.cn/news/detail-406014.html

到了這里,關(guān)于SpringBoot Data ElasticSearch 4.* 使用 GeoPoint 坐標(biāo)位置獲取范圍內(nèi)數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包