ElasticSearch - 基于 “黑马旅游” 案例,实现搜索框、分页、条件过滤、附近酒店、广告置顶功能
发布日期:2025-03-29 17:54:44 浏览次数:5 分类:精选文章

本文共 3913 字,大约阅读时间需要 13 分钟。

黑马旅游案例一揽Index文档

1. 黑马旅游案例概述

本文将介绍如何基于 Elasticsearch 实现酒店搜索功能,包括搜索框、分页、过滤、定位以及广告优先排名等多个模块的开发与实现。

2. 搜索框与分页

2.1 搜索框需求分析

搜索框是用户与服务进行交互的主要入口。用户输入关键词后,系统需要将其转化为有效的搜索请求参数。

2.1.1 参数分析

  • key:用户输入的搜索关键词
  • page:当前页码,支持分页功能
  • size:每页展示的酒店数量
  • sortBy:排序规则,默认按照查询关键词匹配度降序排序

2.1.2 请求格式

搜索请求的格式为标准的 Elasticsearch JSON 格式。用户输入关键词后,前端作为介质将请求转化为 JSON 格式后发送至后端。

2.1.3 后端处理

后端接收 JSON 参数后,调用 Elasticsearch 进行搜索,返回数据总数及酒店数据列表。需注意响应格式需提前与前端约定。

2.1.4 分页功能

分页功能的实现与搜索框无异。点击分页按钮后,会重复发送相同的搜索请求。分页参数需与前端同步更新。

2.2 定义实体类

  • RequestParams:用于接收前端请求参数
    @Datapublic class RequestParams {    private String key;    private Integer page;    private Integer size;    private String sortBy;}
  • PageResult:用于返回搜索结果
    @Datapublic class PageResult {    private Long total;    private List
    hotels; public PageResult(Long total, List
    hotels) { this.total = total; this.hotels = hotels; }}

2.3 Controller 定义

  • HotelController:负责接收搜索请求并调用服务接口
    @RestController@RequestMapping("/hotel")public class HotelController {    @Autowired    private IHotelService hotelService;    @RequestMapping("/list")    public PageResult search(@RequestBody RequestParams params) {        return hotelService.search(params);    }}

2.4 Elasticsearch 搜索实现

  • HotelService:提供 Elasticsearch 搜索功能
    public class HotelService extends ServiceImpl {    @Autowired    private RestHighLevelClient client;    @Autowired    private ObjectMapper objectMapper;    public PageResult search(RequestParams params) throws IOException {        try {            SearchRequest request = new SearchRequest("hotel");            String searchContent = params.getKey();            if(!StringUtils.hasLength(searchContent)) {                request.source().query(QueryBuilders.matchAllQuery());            } else {                request.source().query(QueryBuilders.matchQuery("all", searchContent));            }            Integer page = params.getPage();            Integer size = params.getSize();            if(page == null || size == null) {                throw new IOException("分页数据不能为空!");            }            request.source().from((page-1)*size).size(size);            SearchResponse response = client.search(request, RequestOptions.DEFAULT);            return handlerResponse(response);        } catch (IOException e) {            System.out.println("[HotelService] 搜索失败!");            e.printStackTrace();            return null;        }    }    private PageResult handlerResponse(SearchResponse response) throws JsonProcessingException {        SearchHits hits = response.getHits();        long total = hits.getTotalHits().value;        SearchHit[] hits1 = hits.getHits();        List
    hotelDocList = new ArrayList<>(); for(SearchHit searchHit : hits1) { String json = searchHit.getSourceAsString(); HotelDoc hotelDoc = objectMapper.readValue(json, HotelDoc.class); Object[] sortValues = searchHit.getSortValues(); if(sortValues != null && sortValues.length > 0) { hotelDoc.setDistance(sortValues[0]); } hotelDocList.add(hotelDoc); } return new PageResult(total, hotelDocList); }}

3. 过滤功能

3.1 需求分析

为提高搜索效率,支持用户根据品牌、城市、星级等条件进行过滤。

3.2 RequestParams 扩展

  • RequestParams 需扩展以包含过滤字段
    @Datapublic class RequestParams {    private String key;    private Integer page;    private Integer size;    private String sortBy;    private String brand;    private String starName;    private String city;    private Integer minPrice;    private Integer maxPrice;}

3.3 Elasticsearch 过滤查询

使用 BoolQuery,结合 term 查询和 range 查询实现过滤功能。具体逻辑可封装至方法中。

3.4响应处理

推荐使用 boolQuery 和 functionScoreQuery 来处理高级查询和排序。

4. 广告置顶

4.1 需求分析

用户点击搜索后,将指定酒店置于搜索结果顶部。需张广告标记字段并在 Elasticsearch 中进行函数评分。

4.2 HotelDoc 实体类扩展

  • 添加 isAD 字段标识广告数据
    @Datapublic class HotelDoc {    private Boolean isAD;    // 其他字段尽量简化}

4.3 Elasticsearch 调用

使用 functionScoreQuery 对匹配到 isAD: true 的酒店赋予更高权重,提升搜索排名。

上一篇:20241012更新_yum install 找不到合适的yum源_yum源不起作用_yum无法安装程序_Linux默认源替换---Linux工作笔记067
下一篇:ElasticSearch - 基于 JavaRestClient 查询文档(match、精确、复合查询,以及排序、分页、高亮)

发表评论

最新留言

不错!
[***.144.177.141]2025年04月27日 22时44分33秒