elasticsearch11-实战搜索和分页


个人名片:

博主:酒徒ᝰ.
个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志:三人行,必有我师焉。

本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》,SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式

【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 点击观看

目录

四、黑马旅游案例

Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,其在DB-Engines"兵器"排行榜中长期位列第一。除了搜索领域外,Elasticsearch与Kibana、Logstash组成的ELK系统还可以应用到日志采集、分析、监控等领域。Elasticsearch具有以下优势:

  • 高可用性:能够以多种搜索方式执行及合并多种类型的搜索(结构化数据、非结构化数据、地理位置、指标)。
  • 高扩展性:能够水平扩展,每秒钟可处理海量事件,同时能够自动管理索引和查询在集群中的分布方式。
  • 访问速度快:能够快速查询大数据,访问速度极快。
  • 安全性好:支持安全性和可靠性。
    注意事项:
  1. 数据集分类:基本上,可以在Elasticsearch中索引(即存储)想要的任何数据,但实际上有两类:静态数据和时间序列数据。
  2. 数据集建模方式:根据存储的数据类型,应该以不同的方式为集群建模。对于静态数据,应该选择固定数量的索引和分片。对于时间序列数据,应该选择基于时间的滚动索引。
  3. 搜索评分:对于每个搜索查询,Elasticsearch都会计算相关性分数。该分数基于tf-idf算法,该算法代表词项频率-反向文档频率。

直接启动HotelDemoApplication,进入浏览器输入相应的ip地址和端口号(localhost:8089);

分析:

/list地址,

POST方式

请求参数:

key, page, size, sortBy

返回值:分页查询,需要返回分页结果PageResult,包含两个属性
total: 总条数
List <HotelDoc>: 当前页数据

建立PageResult和RequestParams

PageResult 代码示例:

java 复制代码
package cn.itcast.hotel.pojo;

import lombok.Data;

import java.util.List;

@Data
public class PageResult {
    private Long total;
    private List<HotelDoc> hotels;

    public PageResult() {
    }

    public PageResult(Long total, List<HotelDoc> hotels) {
        this.total = total;
        this.hotels = hotels;
    }
}

RequestParams 代码示例:

java 复制代码
package cn.itcast.hotel.pojo;

import lombok.Data;

@Data
public class RequestParams {
    private String key;
    private Integer page;
    private Integer size;
    private String sortBy;
}

1. 酒店搜索和分页

创建HotelController

代码示例:

java 复制代码
@RestController
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private IHotelService hotelService;

    @PostMapping("/list")
    public PageResult search1(@RequestBody RequestParams params) {
        return hotelService.search(params);
    }
}

在IHotelService中

java 复制代码
PageResult search(RequestParams params);

在IHotelService中

java 复制代码
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Bean
    public RestHighLevelClient client() {
        return new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.179.128:9200")
        ));
    }

    private PageResult handleResponse(SearchResponse response) {
        SearchHits hits = response.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("数据总条数:" + total + "条");
        SearchHit[] hitsHits = hits.getHits();
        List<HotelDoc> list = new ArrayList<>();
        for (SearchHit hit : hitsHits) {
            String json = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            list.add(hotelDoc);
        }
        return new PageResult(total, list);
    }
    @Override
    public PageResult search(RequestParams params) {
        try{
            SearchRequest request = new SearchRequest("hotel");

            buildBasicQuery(params, request);

            SearchResponse response = client().search(request, RequestOptions.DEFAULT);
            return handleResponse(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void buildBasicQuery(RequestParams params, SearchRequest request) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        String key = params.getKey();
        if (key == null || "".equals(key)) {
            boolQuery.must(QueryBuilders.matchAllQuery());
        }else {
            boolQuery.must(QueryBuilders.matchQuery("all", key));
        }
        request.source().query(boolQuery);
        
         int page = params.getPage(), size = params.getSize();
		 request.source().from((page-1)*page).size(size);
    }
}
相关推荐
北风toto4 小时前
Elasticsearch 8.2.3 快照迁移实战:从备份到恢复的完整指南
大数据·elasticsearch·jenkins
LaughingZhu4 小时前
Product Hunt 热榜 2026-11-01:Specsight、SKI、telemetry.dev
人工智能·深度学习·神经网络·搜索引擎·百度
Elasticsearch1 天前
你的日志早已写好告警:在 Elasticsearch 中生成 ES|QL 检测查询
elasticsearch
matrixcode11 天前
Orama:一个把全文搜索做成艺术品的开源搜索引擎
其他·搜索引擎·开源
垚垚学技术_聚焦云原生1 天前
Ansible Vault 安全实践:敏感密码、密钥加密存储方案
安全·elasticsearch·ansible
syntaxsage1 天前
searx:隐私优先的元搜索引擎,宣布停止维护
其他·搜索引擎
Flynn Liang2 天前
内存/Redis/PGVector/Milvus/ES 怎么选?Spring AI VectorStore 抽象与向量库选型对照表
redis·elasticsearch·milvus·rag·springai·pgvector·向量库
Elastic 中国社区官方博客2 天前
基于 Jina OCR v1 与 Elasticsearch 的 PDF 智能混合检索与 RAG 系统实战指南
大数据·数据库·elasticsearch·搜索引擎·ocr·jina
Eloudy3 天前
Qwen2.5-14B-Instruct-AWQ 模型文件结构分析
大数据·elasticsearch·搜索引擎
YIN_尹3 天前
【git原理与使用】git初识与安装
大数据·git·elasticsearch