接口信息
请求/返回参数
请求参数 SearchQueryObject
SearchQueryObject@Getter @Setter public class SearchQueryObject extends QueryObject { private Integer type = -1; }
|
返回结果 SearchResult
SearchResult@Getter @Setter public class SearchResult { private List<DestinationDTO> dests = new ArrayList<>(); private List<StrategyDTO> strategies = new ArrayList<>(); private List<TravelDTO> travels = new ArrayList<>(); private List<UserInfoDTO> users = new ArrayList<>(); private Long total = 0L; }
|
Controller
SearchController@RestController @RequestMapping("/q") public class SearchController { private final ArticleFeignService articleFeignService; private final UserInfoFeignService userInfoFeignService; private final ElasticsearchService elasticsearchService;
public SearchController(ArticleFeignService articleFeignService, UserInfoFeignService userInfoFeignService, ElasticsearchService elasticsearchService) { this.articleFeignService = articleFeignService; this.userInfoFeignService = userInfoFeignService; this.elasticsearchService = elasticsearchService; }
@GetMapping public R<?> search(SearchQueryObject qo) { qo.setKeyword(URLDecoder.decode(qo.getKeyword(), StandardCharsets.UTF_8)); switch (qo.getType()) { case 0: return this.searchForDest(qo); case 1: return this.searchForStrategy(qo); case 2: return this.searchForTravel(qo); case 3: return this.searchForUserInfo(qo); default: return this.searchForAll(qo); } } private R<?> searchForUserInfo(SearchQueryObject qo) { return R.ok(); }
private R<?> searchForAll(SearchQueryObject qo) { return R.ok(); }
private R<?> searchForTravel(SearchQueryObject qo) { return R.ok(); }
private R<?> searchForDest(SearchQueryObject qo) { return R.ok(); } }
|
搜索目的地
目的地的查询是精确查询,直接从数据库中查询
远程查询目的地
找到 trip-article-server 模块,在 DestinationController 添加查询方法。
DestinationController@GetMapping("/getByName") public R<Destination> getDestByName(@RequestParam String name) { return R.ok(destinationService.getOne(Wrappers.<Destination>lambdaQuery().eq(Destination::getName, name))); }
|
ArticleFeignService@GetMapping("/destinations/getByName") public R<DestinationDTO> getDestByName(@RequestParam String name);
|
远程查询攻略
找到 trip-article-server 模块,在 StrategyController 添加查询方法。
StrategyController@GetMapping("/findByDestName") public R<List<Strategy>> findStrategyByDestName(@RequestParam String destName) { return R.ok(strategyService.list(Wrappers.<Strategy>lambdaQuery() .eq(Strategy::getDestName, destName) .eq(Strategy::getState, Strategy.STATE_PUBLISH))); }
|
ArticleFeignService@GetMapping("/strategies/findByDestName") public R<List<StrategyDTO>> findStrategyByDestName(@RequestParam String destName);
|
远程查询游记
找到 trip-article-server 模块,在 TravelController 添加查询方法。
TravelController@GetMapping("/findByDestName") public R<List<Travel>> findTravelByDestName(@RequestParam String destName) { return R.ok(travelService.list(Wrappers.<Travel>lambdaQuery() .eq(Travel::getDestName, destName) .eq(Travel::getState, Travel.STATE_RELEASE))); }
|
ArticleFeignService@GetMapping("/travels/findByDestName") public R<List<TravelDTO>> findTravelByDestName(@RequestParam String destName);
|
远程查询用户
找到 trip-article-server 模块,在 UserInfoController 添加查询方法。
UserInfoController@GetMapping("/findByDestName") public R<List<UserInfo>> findUserByDestName(@RequestParam String destName) { return R.ok(userInfoService.list(Wrappers.<UserInfo>lambdaQuery() .eq(UserInfo::getCity, destName) .eq(UserInfo::getState, UserInfo.STATE_NORMAL))); }
|
UserInfoFeignService@GetMapping("/users/findByDestName") public R<List<UserInfoDTO>> findUserByDestName(@RequestParam String destName);
|
完成根据目的地搜索
SearchControllerprivate R<?> searchForDest(SearchQueryObject qo) { SearchResult result = new SearchResult(); R<DestinationDTO> destResult = articleFeignService.getDestByName(qo.getKeyword()); DestinationDTO dest = destResult.checkAndGet(); if (dest != null) { result.setTotal(1L); R<List<StrategyDTO>> strategyResult = articleFeignService.findStrategyByDestName(qo.getKeyword()); List<StrategyDTO> strategyDTOS = strategyResult.checkAndGet(); result.setStrategies(strategyDTOS); result.setTotal(result.getTotal() + strategyDTOS.size());
R<List<TravelDTO>> travelResult = articleFeignService.findTravelByDestName(qo.getKeyword()); List<TravelDTO> travelDTOS = travelResult.checkAndGet(); result.setTravels(travelDTOS); result.setTotal(result.getTotal() + travelDTOS.size());
R<List<UserInfoDTO>> userResult = userInfoFeignService.findUserByDestName(qo.getKeyword()); List<UserInfoDTO> userInfoDTOS = userResult.checkAndGet(); result.setUsers(userInfoDTOS); result.setTotal(result.getTotal() + userInfoDTOS.size()); } JSONObject jsonObject = new JSONObject(); jsonObject.put("qo", qo); jsonObject.put("result", result); jsonObject.put("dest", dest); return R.ok(jsonObject); }
|
搜索攻略
攻略搜索结果有高亮显示,是从ES中查询的,搜索字段为 title, subtitle, summary
远程查询攻略
找到 trip-article-server 模块,在 StrategyController 添加查询方法。
StrategyController@GetMapping("/getById") public Strategy getById(Long id) { return strategyService.getOne(Wrappers.<Strategy>lambdaQuery().eq(Strategy::getId, id)); }
|
ArticleFeignService@GetMapping("/strategies/getById") public StrategyDTO getStrategyById(@RequestParam String id);
|
完成攻略搜索
ES查询时,需要远程调用根据ID查询攻略,填充游记的其他信息
SearchControllerprivate R<?> searchForStrategy(SearchQueryObject qo) { Page<StrategyDTO> page = elasticsearchService.searchWithHighlight(StrategyEs.class, StrategyDTO.class, qo, (clazz, id) -> articleFeignService.getStrategyById(id), "title", "subtitle", "summary"); JSONObject jsonObject = new JSONObject(); jsonObject.put("qo", qo); jsonObject.put("page", page); return R.ok(jsonObject); }
|
查询游记
远程查询游记
找到 trip-article-server 模块,在 TravelController 添加查询方法。
TravelController@GetMapping("/getById") public R<Travel> getById(Long id) { return R.ok(travelService.getOne(Wrappers.<Travel>lambdaQuery().eq(Travel::getId, id))); }
|
ArticleFeignService@GetMapping("/travels/getById") R<TravelDTO> getTravelById(@RequestParam String id);
|
完成攻略搜索
ES查询时,需要远程调用根据ID查询游记,填充游记的其他信息
SearchControllerprivate R<?> searchForTravel(SearchQueryObject qo) { Page<TravelDTO> page = elasticsearchService.searchWithHighlight(TravelEs.class, TravelDTO.class, qo, (clazz, id) -> articleFeignService.getTravelById(id).checkAndGet(), "title", "summary"); JSONObject jsonObject = new JSONObject(); jsonObject.put("qo", qo); jsonObject.put("page", page); return R.ok(jsonObject); }
|
查询用户
远程查询游记
找到 trip-article-server 模块,在 TravelController 添加查询方法。
TravelController@GetMapping("/getById") public R<UserInfoDTO> getById(Long id) { return R.ok(userInfoService.getDtoById(id)); }
|
ArticleFeignService@GetMapping("/users/getById") R<UserInfoDTO> getById(@RequestParam String id);
|
完成搜索用户
SearchControllerprivate R<?> searchForUserInfo(SearchQueryObject qo) { Page<UserInfoDTO> page = elasticsearchService.searchWithHighlight(UserInfoEs.class, UserInfoDTO.class, qo, (clazz, id) -> userInfoFeignService.getById(id).checkAndGet(), "city", "info"); JSONObject jsonObject = new JSONObject(); jsonObject.put("qo", qo); jsonObject.put("page", page); return R.ok(jsonObject); }
|
查询全部
完成查询全部
SearchControllerprivate R<?> searchForAll(SearchQueryObject qo) { SearchResult result = new SearchResult(); Page<UserInfoDTO> userPage = elasticsearchService.searchWithHighlight(UserInfoEs.class, UserInfoDTO.class, qo, (clazz, id) -> userInfoFeignService.getById(id).checkAndGet(), "city", "info"); result.setUsers(userPage.getContent()); result.setTotal(userPage.getTotalElements());
Page<TravelDTO> travelPage = elasticsearchService.searchWithHighlight(TravelEs.class, TravelDTO.class, qo, (clazz, id) -> articleFeignService.getTravelById(id).checkAndGet(), "title", "summary"); result.setTravels(travelPage.getContent()); result.setTotal(result.getTotal() + travelPage.getTotalElements());
Page<StrategyDTO> strategyPage = elasticsearchService.searchWithHighlight(StrategyEs.class, StrategyDTO.class, qo, (clazz, id) -> articleFeignService.getStrategyById(id), "title", "subtitle", "summary"); result.setStrategies(strategyPage.getContent()); result.setTotal(result.getTotal() + strategyPage.getTotalElements());
Page<DestinationDTO> destPage = elasticsearchService.searchWithHighlight(DestinationEs.class, DestinationDTO.class, qo, (clazz, id) -> articleFeignService.getDestById(id), "name", "info"); result.setDests(destPage.getContent()); result.setTotal(result.getTotal() + destPage.getTotalElements());
JSONObject jsonObject = new JSONObject(); jsonObject.put("result", result); jsonObject.put("qo", qo); return R.ok(jsonObject); }
|