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);
    }
}
相关推荐
发仔1231 小时前
生产环境使用ELK查看和分析Java报错日志详解
elasticsearch·kibana·logstash
1.01^100013 小时前
[2-02-02].第03节:环境搭建 - Win10搭建ES集群环境
elasticsearch
张先shen15 小时前
Elasticsearch RESTful API入门:全文搜索实战(Java版)
java·大数据·elasticsearch·搜索引擎·全文检索·restful
Elastic 中国社区官方博客15 小时前
Elasticsearch 字符串包含子字符串:高级查询技巧
大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
张先shen16 小时前
Elasticsearch RESTful API入门:全文搜索实战
java·大数据·elasticsearch·搜索引擎·全文检索·restful
2201_7567767717 小时前
网络安全初级
大数据·elasticsearch·搜索引擎
张先shen18 小时前
Elasticsearch RESTful API入门:索引的增删改查完全指南
java·大数据·elasticsearch·搜索引擎·架构·全文检索·restful
重生之后端学习19 小时前
day08-Elasticsearch
后端·elasticsearch·spring cloud·中间件·全文检索·jenkins
Elasticsearch20 小时前
全领域的 AI 采用:主要用例和需避免的错误
elasticsearch
Elasticsearch20 小时前
使用 Maximum Marginal Relevance 实现搜索结果多样化
elasticsearch