添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
添加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<EsStoreEntity, Long> {
@Query("{\"bool\":{\"filter\":{\"geo_distance\":{\"distance\":\"?0\",\"location\":{\"lon\":\"?1\",\"lat\":\"?2\"}}}}}")
List<EsStoreEntity>mapperSelectByLocation(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(){
List<EsStoreEntity>list= esStoreRepository.mapperSelectByLocation("200km",-70d,40d);
}
kibana查询视图
|