地图定位功能 参考代码

分词查询 elasticSearch 分词搜索引擎


启动类


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;

import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) //排除mongo的自动配置

public class ESApplication {

public static void main(String[] args) {

SpringApplication.run(ESApplication.class, args);

}

}




============================================================



定位功能 API



import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.elasticsearch.common.geo.GeoPoint;

import org.springframework.data.annotation.Id;

import org.springframework.data.elasticsearch.annotations.Document;

import org.springframework.data.elasticsearch.annotations.Field;

import org.springframework.data.elasticsearch.annotations.FieldType;

import org.springframework.data.elasticsearch.annotations.GeoPointField;

@Data

@NoArgsConstructor

@AllArgsConstructor

@Document(indexName = "tanhua", type = "user_location", shards = 6, replicas = 2)

public class UserLocation {

@Id

private Long userId; //用户id

@GeoPointField

private GeoPoint location; //lon:经度 lat:纬度

@Field(type = FieldType.Keyword)

private String address; //位置描述

@Field(type = FieldType.Long)

private Long created; //创建时间

@Field(type = FieldType.Long)

private Long updated; //更新时间

@Field(type = FieldType.Long)

private Long lastUpdated; //上次更新时间

}



==========================================================


定位功能 APP VO



import cn.hutool.core.bean.BeanUtil;

import com.tanhua.dubbo.server.pojo.UserLocation;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import java.util.ArrayList;

import java.util.List;

@Data

@NoArgsConstructor

@AllArgsConstructor

public class UserLocationVo implements java.io.Serializable {

private static final long serialVersionUID = 4133419501260037769L;

private Long userId; //用户id

private Double longitude; //经度

private Double latitude; //维度

private String address; //位置描述

private Long created; //创建时间

private Long updated; //更新时间

private Long lastUpdated; //上次更新时间

public static final UserLocationVo format(UserLocation userLocation) {

UserLocationVo userLocationVo = BeanUtil.toBean(userLocation, UserLocationVo.class);

userLocationVo.setLongitude(userLocation.getLocation().getLon());

userLocationVo.setLatitude(userLocation.getLocation().getLat());

return userLocationVo;

}

public static final List formatToList(List userLocations) {

List list = new ArrayList<>();

for (UserLocation userLocation : userLocations) {

list.add(format(userLocation));

}

return list;

}

}




============================================================



定位接口 API


public interface UserLocationApi {

/**

* 更新用户地理位置

*

* @param userId 用户id

* @param longitude 经度

* @param latitude 纬度

* @param address 地址名称

* @return

*/

Boolean updateUserLocation(Long userId, Double longitude, Double latitude, String address);

}




===============================================================



定位接口 API IMPL



import cn.hutool.core.util.ObjectUtil;

import com.alibaba.dubbo.config.annotation.Service;

import com.tanhua.dubbo.server.api.UserLocationApi;

import com.tanhua.dubbo.server.pojo.UserLocation;

import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.action.update.UpdateRequest;

import org.elasticsearch.common.geo.GeoPoint;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import org.springframework.data.elasticsearch.core.query.*;

import javax.annotation.PostConstruct;

import java.util.HashMap;

import java.util.Map;

@Service(version = "1.0.0")

@Slf4j

public class UserLocationApiImpl implements UserLocationApi {

@Autowired

private ElasticsearchTemplate elasticsearchTemplate;

/**

* 初始化索引库

*

*/

@PostConstruct

public void initIndex(){

//判断索引库是否存在,如果不存在,需要创建

if(!this.elasticsearchTemplate.indexExists("tanhua")){

this.elasticsearchTemplate.createIndex(UserLocation.class);

}

//判断表是否存在,如果不存在,需要创建

if(!this.elasticsearchTemplate.typeExists("tanhua", "user_location")){

this.elasticsearchTemplate.putMapping(UserLocation.class);

}

}

@Override

public Boolean updateUserLocation(Long userId, Double longitude, Double latitude, String address) {

//查询个人的地理位置数据,如果不存在,需要新增,如果是存在数据,更新数据

try {

GetQuery getQuery = new GetQuery();

getQuery.setId(String.valueOf(userId));

UserLocation userLocation = this.elasticsearchTemplate.queryForObject(getQuery, UserLocation.class);

if(ObjectUtil.isEmpty(userLocation)){

//新增数据

userLocation = new UserLocation();

userLocation.setUserId(userId);

userLocation.setAddress(address);

userLocation.setCreated(System.currentTimeMillis());

userLocation.setUpdated(userLocation.getCreated());

userLocation.setLastUpdated(userLocation.getCreated());

userLocation.setLocation(new GeoPoint(latitude, longitude));

IndexQuery indexQuery = new IndexQueryBuilder().withObject(userLocation).build();

//保存数据到ES中

this.elasticsearchTemplate.index(indexQuery);

}else {

//更新数据

//更新的字段

Map map = new HashMap<>();

map.put("location", new GeoPoint(latitude, longitude));

map.put("updated", System.currentTimeMillis());

map.put("lastUpdated", userLocation.getUpdated());

map.put("address", address);

UpdateRequest updateRequest = new UpdateRequest();

updateRequest.doc(map);

UpdateQuery updateQuery = new UpdateQueryBuilder()

.withId(String.valueOf(userId))

.withClass(UserLocation.class)

.withUpdateRequest(updateRequest).build();

//更新数据

this.elasticsearchTemplate.update(updateQuery);

}

return true;

} catch (Exception e) {

log.error("更新地理位置失败~ userId = " + userId + ", longitude = " + longitude + ", latitude = " + latitude + ", address = " + address, e);

}

return false;

}

}

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章