匹配接口新增自然村匹配

This commit is contained in:
shenjian@fangjia.com
2023-02-17 13:13:07 +08:00
parent 8a652a38b7
commit b592fa8527
@@ -24,6 +24,7 @@ import com.fangjia.open.model.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term;
@@ -85,7 +86,6 @@ import com.fangjia.service.mongo.fangjia.ServerAreaService;
import com.fangjia.service.open.WaitCrawlKeywordsService;
import com.fangjia.service.sql.FjtEvaluationService;
import com.fangjia.service.web.AddressService;
import org.sothis.web.mvc.annotation.Param;
@Authority(role = Role.USER)
public class AddressController {
@@ -116,6 +116,8 @@ public class AddressController {
private static final String GRADE_CONSUME = "消费评分";
public final static OpenConfig CONFIG = OpenConfig.getConfig();
private static final String ES_USER = "developer";
private static final String ES_PASSWORD = "5e8edd851d2fdfbd7415232c67367cc3";
/**
* @deprecated see parse2Action
@@ -522,7 +524,53 @@ public class AddressController {
MatchDistrictResult matchResult = addressMatchProxyService.checkMatchDistrictCorrect(param);
jsonResult.put("matchResult", matchResult);
} else if (jsonResult != null) {
jsonResult.put("matchResult", new MatchDistrictResult());
// 判断是否为自然村
boolean matchedVillage = false;
if (jsonResult.get("searchAddress") != null) {
JSONObject searchAddress = (JSONObject) jsonResult.get("searchAddress");
String city = searchAddress.getString("city");
String region = searchAddress.getString("district");
String name = searchAddress.getString("keywords");
String township = searchAddress.getString("township");
if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(region) && StringUtils.isNotBlank(name)) {
List<JSONObject> villages = findVillage(city, region, name);
if (CollectionUtils.isNotEmpty(villages)) {
if (villages.size() == 1 || StringUtils.isBlank(township)) {
JSONObject village = villages.get(0).getJSONObject("_source");
jsonResult.put("id", villages.get(0).getString("_id"));
jsonResult.put("province", village.getString("province"));
jsonResult.put("city", village.getString("city"));
jsonResult.put("district", village.getString("region"));
jsonResult.put("township", village.getString("township"));
jsonResult.put("name", village.getString("name"));
jsonResult.put("lng", village.getJSONObject("location").getDouble("lon"));
jsonResult.put("lat", village.getJSONObject("location").getDouble("lat"));
jsonResult.put("propertyType", "自然村");
matchedVillage = true;
} else {
for (JSONObject source : villages) {
JSONObject village = source.getJSONObject("_source");
if (village.getString("township").contains(township) || township.contains(village.getString("township"))) {
jsonResult.put("id", source.getString("_id"));
jsonResult.put("province", village.getString("province"));
jsonResult.put("city", village.getString("city"));
jsonResult.put("district", village.getString("region"));
jsonResult.put("township", village.getString("township"));
jsonResult.put("name", village.getString("name"));
jsonResult.put("lng", village.getJSONObject("location").getDouble("lon"));
jsonResult.put("lat", village.getJSONObject("location").getDouble("lat"));
jsonResult.put("propertyType", "自然村");
matchedVillage = true;
break;
}
}
}
}
}
}
if (matchedVillage == false) {
jsonResult.put("matchResult", new MatchDistrictResult());
}
}
resultModel.setResult(json.get("result"));
mav.setModel(resultModel);
@@ -918,6 +966,60 @@ public class AddressController {
return ret;
}
private JSONObject addTerm(String name, String value){
JSONObject term = new JSONObject();
JSONObject match = new JSONObject();
match.put(name, value);
term.put("term", match);
return term;
}
/***
* 获取自然村
* @param city
* @param region
* @param name
* @return
*/
private List<JSONObject> findVillage(String city, String region, String name) {
JSONObject reqBody = new JSONObject();
reqBody.put("from", 0);
reqBody.put("size", 5);
JSONObject query = new JSONObject();
JSONObject bool = new JSONObject();
JSONArray must = new JSONArray();
must.add(addTerm("city", city));
must.add(addTerm("region", region));
must.add(addTerm("name", name));
bool.put("must", must);
query.put("bool", bool);
reqBody.put("query", query);
Base64 token = new Base64();
String authenticationEncoding = token.encodeAsString((""+ES_USER+":"+ES_PASSWORD+"").getBytes());
HttpRequest request = new HttpRequest();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Basic "+authenticationEncoding+"");
try {
String response = request.postWithBody("http://192.168.0.45:9200/village/_search", reqBody, headers, "UTF-8");
JSONObject json = JSONObject.fromObject(response);
if(!json.containsKey("hits") || !json.getJSONObject("hits").containsKey("hits")){
return Collections.emptyList();
}
JSONArray hits = json.getJSONObject("hits").getJSONArray("hits");
List<JSONObject> results = new ArrayList<JSONObject>();
for(int i=0 ;i < hits.size(); i++){
JSONObject jsonObject = hits.getJSONObject(i);
results.add(jsonObject);
}
return results;
} catch (HttpRequestException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
/**
* 加载测试数据
*