建筑参考网站,如何做旅游休闲网站,济南集团网站建设费用,网站如何上传到主机目录
一、实现输入搜索功能
1、下载代码#xff0c;在idea上打开
2、新建RequestParams类#xff0c;用于接收解析请求
3、在启动类中加入客户端地址Bean#xff0c;以便实现服务
4、编写搜索方法
5、新建返回分页结果类
6、实现搜索方法
7、编写控制类#xff0c;…目录
一、实现输入搜索功能
1、下载代码在idea上打开
2、新建RequestParams类用于接收解析请求
3、在启动类中加入客户端地址Bean以便实现服务
4、编写搜索方法
5、新建返回分页结果类
6、实现搜索方法
7、编写控制类接收请求
8、运行代码测试功能
1检索功能
2分页功能
二、添加品牌、城市、星级、价格等过滤功能
1、修改RequestParams类
2、更改service类
3、启动并测试
三、实现排序功能
1、修改RequestParams类
2、在PageResult类中增加排序算法
3、修改HotelDoc类增加Distance属性
4、修改解析JSON的方法
四、实现广告置顶功能
1、修改HotelDoc类增加isAD属性
2、在service实现类中添加算分方法
3、运行测试 一、实现输入搜索功能
1、下载代码在idea上打开
2、新建RequestParams类用于接收解析请求 Data
public class RequestParams {private String key;private Integer page;private Integer size;private String sortBy;
}
3、在启动类中加入客户端地址Bean以便实现服务 Beanpublic RestHighLevelClient client(){return new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.248.152:9200)));}
4、编写搜索方法 PageResult search(RequestParams params);
5、新建返回分页结果类 Data
public class PageResult {private long total;private ListHotelDoc hotels;public PageResult() {}public PageResult(long total, ListHotelDoc hotels) {this.total total;this.hotels hotels;}
}
6、实现搜索方法 Service
public class HotelService extends ServiceImplHotelMapper, Hotel implements IHotelService {Autowiredprivate RestHighLevelClient client;Overridepublic PageResult search(RequestParams params) {
// 准备Request对象try {SearchRequest request new SearchRequest(hotel);String key params.getKey();
// 准备DSLif (keynull || .equals(key)){request.source().query(QueryBuilders.matchAllQuery());}else {request.source().query(QueryBuilders.matchQuery(name, key));}
// 分页Integer page params.getPage();Integer size params.getSize();request.source().from((page-1)*size).size(size);
// 发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);return extracted(response);} catch (IOException e) {throw new RuntimeException(e);}}private PageResult extracted(SearchResponse response) {
// 解析响应SearchHits searchHits response.getHits();
// 获取总条数long value searchHits.getTotalHits().value;System.out.println(共搜索到 value 条数据!);
// 获取文档数组SearchHit[] hits searchHits.getHits();ArrayListHotelDoc list new ArrayList();
// 遍历for (SearchHit hit : hits) {
// 获取文档sourceString json hit.getSourceAsString();
// 反序列化HotelDoc object JSON.parseObject(json, HotelDoc.class);list.add(object);
// 获取高亮结果MapString, HighlightField highlightFields hit.getHighlightFields();if (!CollectionUtils.isEmpty(highlightFields)){
// 根据字段名获取高亮结果HighlightField highlightField highlightFields.get(name);if (highlightFields ! null){
// 获取高亮值String name highlightField.getFragments()[0].string();
// 覆盖非高亮结果object.setName(name);}}
// System.out.println(object object);}return new PageResult(value,list);}
}
7、编写控制类接收请求 RestController
RequestMapping(/hotel)
public class HotelController {Autowiredprivate IHotelService service;PostMapping(/list)public PageResult search(RequestBody RequestParams params){return service.search(params);}
}
8、运行代码测试功能
1检索功能 2分页功能 二、添加品牌、城市、星级、价格等过滤功能
1、修改RequestParams类
Data
public class RequestParams {private String key;private Integer page;private Integer size;private String sortBy;private String city;private String brand;private String starName;private Integer maxPrice;private Integer minPrice;
}
2、更改service类 Overridepublic PageResult search(RequestParams params) {
// 准备Request对象try {SearchRequest request new SearchRequest(hotel);buildBasicQuery(params, request);
// 分页Integer page params.getPage();Integer size params.getSize();request.source().from((page-1)*size).size(size);
// 发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);return extracted(response);} catch (IOException e) {throw new RuntimeException(e);}}private void buildBasicQuery(RequestParams params, SearchRequest request) {String key params.getKey();BoolQueryBuilder boolQuery QueryBuilders.boolQuery();if (keynull || .equals(key)){boolQuery.must(QueryBuilders.matchAllQuery());}else {boolQuery.must(QueryBuilders.matchQuery(all, key));}
// 城市条件if (params.getCity() ! null !params.getCity().equals()){boolQuery.filter(QueryBuilders.termQuery(city, params.getCity()));}
// 品牌条件if (params.getBrand() ! null !params.getBrand().equals()){boolQuery.filter(QueryBuilders.termQuery(brand, params.getBrand()));}
// 星级条件if (params.getStarName() ! null !params.getStarName().equals()){boolQuery.filter(QueryBuilders.termQuery(starName, params.getStarName()));}
// 价格if (params.getMinPrice() ! null params.getMaxPrice() ! null){boolQuery.filter(QueryBuilders.rangeQuery(price).gte(params.getMinPrice()).lte(params.getMaxPrice()));}
// 准备DSLrequest.source().query(boolQuery);}
3、启动并测试 三、实现排序功能
1、修改RequestParams类
// 排序String location params.getLocation();if (location ! null !location.equals()){request.source().sort(SortBuilders.geoDistanceSort(location,new GeoPoint(location)).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));}
2、在PageResult类中增加排序算法 String location params.getLocation();if (location ! null !location.equals()){request.source().sort(SortBuilders.geoDistanceSort(location,new GeoPoint(location)).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));}
3、修改HotelDoc类增加Distance属性 private Object distance;
4、修改解析JSON的方法 // 获取排序值Object[] sortValues hit.getSortValues();if (sortValues.length 0){Object sortValue sortValues[0];object.setDistance(sortValue);}
四、实现广告置顶功能 1、修改HotelDoc类增加isAD属性 private Boolean isAD;
2、在service实现类中添加算分方法 // 算分控制FunctionScoreQueryBuilder functionScoreQuery QueryBuilders.functionScoreQuery(boolQuery,new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery(isAD,true),//过滤条件ScoreFunctionBuilders.weightFactorFunction(10) //分值*10)});
3、运行测试 文章转载自: http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn