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

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

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查询视图

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存