小区周边配套(facilities)切换高德POI数据源并优化性能
- searchNearbyPoi改查ES高德POI(didata_map_poi_2024),按secondary_type中类过滤 - POI类型映射兼容高德分类:small_type精确匹配+secondary_type中类兜底 - 新增排除集合AROUND_EXCLUDE_TYPES:过滤ATM/培训机构/诊所/泛中餐厅/快餐厅等噪音类型 - 高德GCJ-02坐标转百度BD-09输出,经纬度保留6位小数 - 性能优化:批量12小区ES查询并行化、ES结果加缓存(MIDDLE)、ES客户端连接池单例复用、geo_distance只放filter - BaiduPlacePoi实现Serializable支持缓存序列化 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+146
-6
@@ -1,12 +1,17 @@
|
||||
package com.fangjia.service.api;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.fangjia.core.ProvinceData;
|
||||
import com.fangjia.core.RegionData;
|
||||
import com.fangjia.core.util.*;
|
||||
import com.fangjia.dal.QueryParam;
|
||||
import com.fangjia.dal.mongo.dao.BaiDuSearchCacheDao;
|
||||
@@ -19,6 +24,7 @@ import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONException;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
@@ -91,14 +97,148 @@ public class BaiduApiServiceImpl extends AbstractHttpApiService implements Baidu
|
||||
@Override
|
||||
public List<BaiduPlacePoi> searchNearbyLocalDatabase(District district,
|
||||
double radius) {
|
||||
if(district.getCity() == null ||district.getLat2() == null || district.getLng2() == null){
|
||||
return null;
|
||||
}
|
||||
double lat = district.getLat2()/10000000000d;
|
||||
double lng = district.getLng2()/10000000000d;
|
||||
return searchNearbyLocalDatabase(district.getCity(), lat,lng,radius);
|
||||
if(district.getCity() == null || district.getLat() == null || district.getLng() == null){
|
||||
return null;
|
||||
}
|
||||
return searchNearbyPoi(district.getCity(), district.getLat()/10000000000d, district.getLng()/10000000000d, radius);
|
||||
}
|
||||
|
||||
/** ES查询客户端单例,复用连接池 */
|
||||
private static volatile HttpRequest esHttpRequest;
|
||||
|
||||
private static HttpRequest getEsHttpRequest() {
|
||||
if (esHttpRequest == null) {
|
||||
synchronized (BaiduApiServiceImpl.class) {
|
||||
if (esHttpRequest == null) {
|
||||
esHttpRequest = new HttpRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
return esHttpRequest;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<BaiduPlacePoi> searchNearbyPoi(final String city ,final double lat,final double lng, final double radius) {
|
||||
|
||||
String cacheKey = city + "_" + lat + "_" + lng + "_" + radius;
|
||||
Object cached = CacheUtils.getValue(this.getClass(), cacheKey);
|
||||
if (cached != null) {
|
||||
return (List<BaiduPlacePoi>) cached;
|
||||
}
|
||||
|
||||
Map<String,String> header = new HashMap<String, String>();
|
||||
try {
|
||||
String auth = "Basic " + Base64.encodeBase64String("elastic:elasticsearch".getBytes("UTF-8"));
|
||||
header.put("Authorization", auth);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String seawaterUrl = "http://192.168.0.89:9200/didata_map_poi_2024/_search";
|
||||
JSONObject root = new JSONObject();
|
||||
// _source 过滤
|
||||
JSONArray includes = new JSONArray();
|
||||
includes.add("small_type");
|
||||
includes.add("secondary_type");
|
||||
includes.add("address");
|
||||
includes.add("data_id");
|
||||
includes.add("name");
|
||||
includes.add("location");
|
||||
JSONObject sourceFilter = new JSONObject();
|
||||
sourceFilter.put("includes", includes);
|
||||
root.put("_source", sourceFilter);
|
||||
// bool 查询
|
||||
JSONObject bool = new JSONObject();
|
||||
JSONArray mustArray = new JSONArray();
|
||||
// term status
|
||||
JSONObject termStatus = new JSONObject();
|
||||
termStatus.put("status", "0");
|
||||
JSONObject termWrapper = new JSONObject();
|
||||
termWrapper.put("term", termStatus);
|
||||
mustArray.add(termWrapper);
|
||||
// city status
|
||||
JSONObject termCity = new JSONObject();
|
||||
termCity.put("city", city + "市");
|
||||
JSONObject cityTermWrapper = new JSONObject();
|
||||
cityTermWrapper.put("term", termCity);
|
||||
mustArray.add(cityTermWrapper);
|
||||
// 按高德POI中类(secondary_type)过滤,覆盖菜系等细分小类
|
||||
JSONObject termType = new JSONObject();
|
||||
termType.put("secondary_type", Arrays.asList(
|
||||
"学校", "综合医院", "专科医院",// 学校/医疗
|
||||
"银行",// 银行(仅网点,ATM/银行相关不展示)
|
||||
"超级市场", "商场", "便民商店/便利店", "综合市场",// 超市/商场
|
||||
"美容美发店",// 美容美发
|
||||
"运动场馆",// 健身/运动
|
||||
"公园广场",// 公园
|
||||
"机场相关", "火车站", "长途汽车站", "港口码头",// 交通
|
||||
"外国餐厅", "咖啡厅",// 餐饮(仅品类辨识度高的中类,泛中餐厅/快餐厅等小类噪音多由小类精确映射过滤)
|
||||
"宾馆酒店"));// 酒店(仅宾馆酒店中类)
|
||||
JSONObject typeTermWrapper = new JSONObject();
|
||||
typeTermWrapper.put("terms", termType);
|
||||
mustArray.add(typeTermWrapper);
|
||||
bool.put("must", mustArray);
|
||||
// geo_distance 只放在filter(不评分,走filter cache,勿重复放must)
|
||||
JSONObject point = new JSONObject();
|
||||
point.put("lat", lat);
|
||||
point.put("lon", lng);
|
||||
JSONObject geoDistance = new JSONObject();
|
||||
geoDistance.put("distance", radius + "m");
|
||||
geoDistance.put("location", point); // 直接放入对象
|
||||
JSONObject geoWrapper = new JSONObject();
|
||||
geoWrapper.put("geo_distance", geoDistance);
|
||||
JSONArray filterArray = new JSONArray();
|
||||
filterArray.add(geoWrapper);
|
||||
bool.put("filter", filterArray);
|
||||
JSONObject query = new JSONObject();
|
||||
query.put("bool", bool);
|
||||
root.put("query", query);
|
||||
root.put("size", "300");
|
||||
// ---------- 按距离排序 ----------
|
||||
JSONArray sortArray = new JSONArray();
|
||||
JSONObject geoDistanceSort = new JSONObject();
|
||||
// 指定坐标点(对象格式)
|
||||
geoDistanceSort.put("location", point);
|
||||
// 排序参数(与 location 平级)
|
||||
geoDistanceSort.put("order", "asc");
|
||||
geoDistanceSort.put("unit", "m"); // 单位:米
|
||||
JSONObject sortWrapper = new JSONObject();
|
||||
sortWrapper.put("_geo_distance", geoDistanceSort);
|
||||
sortArray.add(sortWrapper);
|
||||
root.put("sort", sortArray);
|
||||
try {
|
||||
String res = getEsHttpRequest().postWithBody(seawaterUrl, root, header, "UTF-8");
|
||||
JSONObject json = JSONObject.fromObject(res);
|
||||
ArrayList<BaiduPlacePoi> lists = new ArrayList<BaiduPlacePoi>();
|
||||
if(json.has("hits") && json.getJSONObject("hits").has("hits")){
|
||||
JSONArray hits = json.getJSONObject("hits").getJSONArray("hits");
|
||||
for(int i = 0; i < hits.size(); i++){
|
||||
JSONObject hit = hits.getJSONObject(i);
|
||||
JSONObject source = hit.getJSONObject("_source");
|
||||
JSONObject location = source.getJSONObject("location");
|
||||
BaiduPlacePoi poi = new BaiduPlacePoi();
|
||||
poi.setId(source.getString("data_id"));
|
||||
poi.setName(source.getString("name"));
|
||||
poi.setAddress(source.optString("address"));
|
||||
poi.setKeyword(source.optString("small_type"));
|
||||
// 中类存入streetId,供上层做类型兜底匹配
|
||||
poi.setStreetId(source.optString("secondary_type"));
|
||||
poi.setCity(city);
|
||||
// 高德坐标(GCJ-02)转百度坐标(BD-09),经纬度保留6位小数
|
||||
Map<String, Double> bdLocation = GeoUtils.gcj02ToBd09(location.getDouble("lat"), location.getDouble("lon"));
|
||||
poi.setLat(DataUtil.roundDouble(bdLocation.get(GeoUtils.LAT), 6));
|
||||
poi.setLng(DataUtil.roundDouble(bdLocation.get(GeoUtils.LNG), 6));
|
||||
poi.setStatus(0);
|
||||
lists.add(poi);
|
||||
}
|
||||
}
|
||||
CacheUtils.putValueCompress(CachePolicyEnum.MIDDLE, this.getClass(), cacheKey, lists);
|
||||
return lists;
|
||||
} catch (HttpRequestException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Map<String, JSONObject> searchNearBy(List<String> keywords, double lat, double lng, double radius, String ak){
|
||||
List<QueryParam> paramList = new ArrayList<QueryParam>();
|
||||
paramList.add(new QueryParam("lat",lat));
|
||||
|
||||
Reference in New Issue
Block a user