博客
关于我
ElasticSearch - 基于 “黑马旅游” 案例,实现搜索框、分页、条件过滤、附近酒店、广告置顶功能
阅读量:800 次
发布时间:2023-01-24

本文共 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 的酒店赋予更高权重,提升搜索排名。

转载地址:http://weeyk.baihongyu.com/

你可能感兴趣的文章
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 时事和见解【2023】
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP、CV 很难入门?IBM 数据科学家带你梳理
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP入门(六)pyltp的介绍与使用
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>