黑马旅游案例(包括搜索,分页,广告置顶)中使用 elasticsearch 7.17.9 Java API

引言

学习黑马 SpringCloud 的 es 部分时发现老师用的是es的高级客户端来操作es的,而高级客户端已经显示弃用,上网搜索发现关于新的 Java client API 只有基础的索引、文档操作,没有关于这种稍复杂案例的操作,于是自己琢磨整理了一份笔记,也为其他学习最新的 es 的小伙伴 提供一个思路吧。

项目结构

                 image-20230325164334304

添加项目依赖

        <!--es7.17.9-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.17.9</version>
        </dependency>
        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>7.17.9</version>
            <exclusions>
                <exclusion>
                    <groupId>jakarta.json</groupId>
                    <artifactId>jakarta.json-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.14.2</version>
        </dependency>

        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.1.1</version>
            <scope>compile</scope>
        </dependency>

EsClientConfig

package cn.itcast.hotel.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsClientConfig {
    @Bean
    public ElasticsearchClient createClient(){
        // 使用 RestClientBuilder 对象创建 Elasticsearch 的 REST 客户端
        RestClient esClient = RestClient.builder(
                new HttpHost("your es server ip", 9200, "http")
        ).build();

        // 创建 ElasticsearchTransport 对象,该对象用于传输数据。这里使用 JacksonJsonpMapper 对象,将 JSON 数据映射为 Java 对象。
        RestClientTransport transport = new RestClientTransport(
                esClient, new JacksonJsonpMapper()
        );

        // 创建 ElasticsearchClient 对象,该对象用于访问 Elasticsearch 的 API
        return new ElasticsearchClient(transport);
    }
}

HotelService

package cn.itcast.hotel.service.impl;

import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.RequestParams;
import cn.itcast.hotel.service.IHotelService;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.*;
import co.elastic.clients.elasticsearch._types.query_dsl.*;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.json.JsonData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 根据参数查询酒店信息
 * @param params 查询参数
 * @return 分页结果
 */
@Service
@Slf4j
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Resource
    private ElasticsearchClient client;

    @Override
    public PageResult search(RequestParams params) {
        String key = params.getKey(); // 搜索关键字
        int page = params.getPage(); // 当前页码
        int size = params.getSize(); // 每页记录数
        SearchResponse<HotelDoc> search;
        try {
            search = getHotelDocSearchResponse(params, key, page, size); // 调用方法获取搜索结果
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 解析响应结果中的文档列表和总数
        ArrayList<HotelDoc> hotelDocs = new ArrayList<>();
        long total;
        // 遍历搜索结果并把酒店信息添加到列表中
        search.hits().hits().forEach(h-> {
            // 获取每个文档的排序字段,设置为距离字段(如果有)
            List<FieldValue> sort = h.sort();
            if (!sort.isEmpty()){
                FieldValue fieldValue = sort.get(0);
                if(h.source() != null) h.source().setDistance(fieldValue.doubleValue());
            }

            hotelDocs.add(h.source());
        });
        if(search.hits().total()!=null){
            total = search.hits().total().value();
        }else total = 0;
        // 返回分页结果
        return new PageResult(total,hotelDocs);
    }

    /**
     * 根据参数构造搜索请求并返回搜索结果
     * @param params 查询参数
     * @param key 搜索关键字
     * @param page 当前页码
     * @param size 每页记录数
     * @return 搜索结果
     * @throws IOException
     */
    private SearchResponse<HotelDoc> getHotelDocSearchResponse(RequestParams params, String key, int page, int size) throws IOException {
        SearchResponse<HotelDoc> search;
        SearchRequest.Builder builder = new SearchRequest.Builder(); // 创建搜索请求构建器
        BoolQuery.Builder bool = QueryBuilders.bool(); // 创建布尔查询构建器
        // 如果搜索关键字为空,则匹配所有记录,否则匹配包含关键字的记录
        if(key == null || "".equals(key)) {
            bool.must(must -> must.matchAll(m -> m));
        }else{
            bool.must(must -> must.match(m -> m.field("all").query(params.getKey())));
        }
        // 如果请求参数中指定了城市,则添加一个 term 过滤条件
        if (params.getCity()!=null&&!params.getCity().equals("")) {
            bool.filter(f -> f.term(t -> t.field("city").value(params.getCity())));
        }
        // 如果请求参数中指定了品牌,则添加一个 term 过滤条件
        if (params.getBrand()!=null&&!Objects.equals(params.getBrand(), "")) {
            bool.filter(f -> f.term(t -> t.field("brand").value(params.getBrand())));
        }
        // 如果请求参数中指定了星级,则添加一个 term 过滤条件
        if (params.getStarName()!=null&&!params.getStarName().equals("")) {
            bool.filter(f -> f.term(t -> t.field("starName").value(params.getStarName())));
        }
        // 如果请求参数中指定了价格范围,则添加一个 range 过滤条件
        if (params.getMinPrice()!=null&& params.getMaxPrice()!=null){
            bool.filter(f -> f.range(t -> t.field("price").gte(JsonData.of(params.getMinPrice())).lte(JsonData.of(params.getMaxPrice()))));
        }
        // 创建一个 function score 查询构造器,用于添加权重和评分模式
        FunctionScoreQuery.Builder function = new FunctionScoreQuery.Builder();
        // 添加查询条件,使用一个 BoolQuery 构建器构建的查询
        function.query(bool.build()._toQuery())
                // 添加函数,如果符合 "isAD" 字段等于 "true" 的条件,权重为 10.0
                .functions(functions->functions.filter(fi -> fi.term(t -> t.field("isAD").value("true"))).weight(10.0))
                // 设置函数计算得分时的模式为 "Sum"(即求和)
                .scoreMode(FunctionScoreMode.Sum);
        // 将 FunctionScoreQuery 添加到查询构建器中
        builder.query(function.build()._toQuery());
        // 分页查询,设置查询结果的起始位置和查询的数量
        builder.from((page -1)* size).size(size);
        // 获取请求中的位置参数
        String location = params.getLocation();
        // 如果位置参数不为空
        if(location!=null) {
            // 将位置参数按逗号分隔为经度和纬度数组
            String[] locationArr = location.split(",");
            // 创建一个基于距离排序的构建器,以 "location" 字段作为排序依据
            GeoDistanceSort geoDistanceSort = new GeoDistanceSort.Builder().field("location")
                    // 设置排序基准点的经纬度坐标
                    .location(l->l.latlon(la->la.lat(Double.parseDouble(locationArr[0])).lon(Double.parseDouble(locationArr[1]))))
                    // 设置排序方式为升序
                    .order(SortOrder.Asc)
                    // 设置排序的距离单位为公里
                    .unit(DistanceUnit.Kilometers).build();
            // 将距离排序添加到查询构建器中
            builder.sort(s -> s.geoDistance(geoDistanceSort));
        }
        // 构建搜索请求,指定搜索的索引为 "hotel"
        SearchRequest request = builder.index("hotel").build();
        // 打印请求日志
        log.info("request:{}",request);
        // 执行搜索请求,并将结果保存到 search 变量中
        search = client.search(request, HotelDoc.class);
        // 返回搜索结果
        return search;
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Elasticsearch 提供了官方的 Java 客户端,可以通过它与 Elasticsearch 服务器进行交互。下面是一个简单的示例,展示如何使用 Elasticsearch Java 客户端: 1. 首先,确保你已经在项目添加了 Elasticsearch Java 客户端的依赖。你可以在 Maven 或 Gradle 添加以下依赖项: Maven: ```xml <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.15.0</version> </dependency> ``` Gradle: ```groovy implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.15.0' ``` 2. 在代码创建 Elasticsearch 客户端实例: ```java import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"))); ``` 请确保将 "localhost" 和 9200 替换为你的 Elasticsearch 服务器的主机和端口。 3. 使用客户端执行操作,例如执行搜索操作: ```java import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.QueryBuilders; SearchRequest searchRequest = new SearchRequest("your_index_name"); searchRequest.source().query(QueryBuilders.matchQuery("your_field_name", "your_search_keyword")); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 处理搜索响应 ``` 请将 "your_index_name" 替换为你的索引名称,将 "your_field_name" 替换为你要搜索的字段名称,将 "your_search_keyword" 替换为你要搜索的关键字。 这只是一个简单的示例,Elasticsearch Java 客户端提供了丰富的 API,可用于执行各种操作,如索引文档、更新文档、删除文档等。你可以参考 Elasticsearch Java 客户端的官方文档来了解更多信息:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IsALian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值