栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

spring elastic使用地图查询功能,简单上手

spring elastic使用地图查询功能,简单上手

添加依赖
		
			org.springframework.boot
			spring-boot-starter-data-elasticsearch
			2.3.4.RELEASE
		
添加es配置文件
@Configuration
public class EsConfiguration extends AbstractElasticsearchConfiguration {
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .withBasicAuth("elastic","fantong")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }

}

添加保存实体
@document(indexName = "es_store")
@Data
public class EsStoreEntity {
    
    @GeoPointField
    @Field(value = "location")
    private GeoPoint location;
    @Id
    private Long id;
    @ApiModelProperty(value = "店铺名字")
    @Field(value = "store_name")
    private String storeName;
}
添加jpa实现类

@query是json数据,可用map转换后自己模拟

@Repository
public interface EsStoreRepository extends ElasticsearchRepository {
    @Query("{"bool":{"filter":{"geo_distance":{"distance":"?0","location":{"lon":"?1","lat":"?2"}}}}}")
    ListmapperSelectByLocation(String distance,double lon,double lat);
}
保存模拟数据
   @Test
    public void saveTest(){
        GeoPoint location=new GeoPoint(40.12,-71.34);
        EsStoreEntity esStoreEntity=new EsStoreEntity();
        esStoreEntity.setId(1L);
        esStoreEntity.setLocation(location);
        esStoreEntity.setStoreName("小商店");
        esStoreRepository.save(esStoreEntity);
    }
查询测试数据

测试完成,查看到坐标被查询出来了,多试几个坐标

    @Test
    public void getTest(){
        Listlist= esStoreRepository.mapperSelectByLocation("200km",-70d,40d);
    }
kibana查询视图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号