/** * 转换JSONObject * * @param list * @return */ private JSONObject parseJson(LocationIndex index) { JSONObject location = new JSONObject(); location.put("id", index.getId()); location.put("x", index.getX()); location.put("y", index.getY()); location.put("description", StringUtils.join(index.getDescription(), " ")); return location; }
/** * 构建结果,去掉重复结果,从前向后优先级降低 * * @param exactResult * @param leadResult * @param fuzzyResult * @return */ private JSONObject buildResult( List<LocationIndex> exactList, List<LocationIndex> leadList, List<LocationIndex> fuzzyList) { JSONArray exactResult = parseJson(exactList); JSONArray leadResult = new JSONArray(); JSONArray fuzzyResult = new JSONArray(); // 去掉重复结果 int length = exactList.size(); Set<String> ids = new HashSet<String>(length); for (int i = 0; i < length; i++) { ids.add(exactList.get(i).getId()); } length = leadList.size(); Set<String> idsPlus = new HashSet<String>(); for (int i = 0; i < length; i++) { LocationIndex index = leadList.get(i); if (!ids.contains(index.getId())) { leadResult.add(parseJson(index)); idsPlus.add(index.getId()); } } ids.addAll(idsPlus); length = fuzzyList.size(); for (int i = 0; i < length; i++) { LocationIndex index = fuzzyList.get(i); if (!ids.contains(index.getId())) { fuzzyResult.add(parseJson(index)); } } idsPlus = null; ids = null; exactList = null; leadList = null; fuzzyList = null; JSONObject result = new JSONObject(); result.put("exactResult", exactResult); result.put("leadResult", leadResult); result.put("fuzzyResult", fuzzyResult); return result; }
/** * edit a location * * @param request * @return */ @RequestMapping(value = "location/edit", method = RequestMethod.POST) public ResponseEntity<JSONObject> editLocation(HttpServletRequest request) { JSONObject result = new JSONObject(); String valid = checkParams(request); if (valid != null) { result.put("error", valid); return new ResponseEntity<JSONObject>(result, HttpStatus.OK); } LocationIndex index = getLocationIndex(request); index.setId(getString(request, "id")); try { solrService.update(index); result.put("success", true); } catch (SolrServerException | IOException e) { logger.error("Add location fail", e); result.put("error", e.getLocalizedMessage()); } return new ResponseEntity<JSONObject>(result, HttpStatus.OK); }
/** * 从request取LocationIndex,不包含id * * @param request * @return */ private LocationIndex getLocationIndex(HttpServletRequest request) { String description = getString(request, "description"); if (StringUtils.isBlank(description)) { return null; } String[] descArr = description.split(" "); Collection<String> descriptions = new ArrayList<String>(); for (String desc : descArr) { if (StringUtils.isNotBlank(desc)) { descriptions.add(desc); } } LocationIndex index = new LocationIndex(); index.setType(LocationIndex.TYPE_LEAFLET); index.setX(getFloat(request, "x")); index.setY(getFloat(request, "y")); index.setDescription(descriptions); return index; }