当前位置: 首页 > news >正文

【Java 实战】通过ElasticSearch实现全局搜索功能

前言

在电商项目中,我们经常会使用到全局搜索来查询自己想要购买的商品,而商品的数量非常多,而且分类繁杂。

面对这样复杂的搜索业务和数据量,使用传统数据库搜索就显得力不从心,一般我们都会使用全文检索技术,比如Solr,Elasticsearch


一、环境搭建

Windows环境:
参考前文:Elasticsearch 安装及启动【Windows】、RabbitMQ安装和使用
Linux环境:
参考前文:Elasticsearch 安装及启动【Linux】、Linux安装RabbitMq

这里为了方便演示,我们统一在windows环境下安装

二、使用步骤

1.引入依赖

<!-- 引入ES依赖 -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.12.0</version>
</dependency>

2.环境配置

修改配置文件application.yml如下:

创建ElasticSearchConfig配置类

package com.local.springboot.springbootcommon.config.es;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
    private String host;
    private int port;

    @Bean
    public RestHighLevelClient getClient() {
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(
                        host, port, "http"
                )
        ));
    }
}

3.商品同步至es

全局搜索是在Elasticsearch中搜索商品信息,所以我们需要将商品信息同步至Elasticsearch中,在商品修改、新增、删除时通过RabbitMQ异步处理Elasticsearch中的商品信息

创建商品索引

这里直接在测试类中添加goods索引,mapping创建前端商品列表需要展示的字段

@Test
void createIndex() throws IOException {
    // 创建一个索引请求
    CreateIndexRequest indexRequest = new CreateIndexRequest("goods");
    // 创建一个Settings
    Settings.Builder settings = Settings.builder();
    settings.put("number_of_shards", "3");  // 设置三个分片
    settings.put("number_of_replicas", "1");  // 设置一个备份
    // 把settings设置给request对象
    indexRequest.settings(settings);
    // 创建一个mappings
    XContentBuilder mappings = JsonXContent.contentBuilder();
    mappings
            .startObject()
                .startObject("properties")
                    .startObject("skuId")
                        .field("type", "text")
                    .endObject()
                    .startObject("skuName")
                        .field("type", "text")
                        .field("analyzer", "ik_max_word")
                    .endObject()
                    .startObject("productName")
                        .field("type", "text")
                        .field("analyzer", "ik_max_word")
                    .endObject()
                    .startObject("num")
                        .field("type", "integer")
                    .endObject()
                    .startObject("sellPrice")
                        .field("type", "double")
                    .endObject()
                    .startObject("coverUrl")
                        .field("type", "keyword")
                    .endObject()
                    .startObject("creatTime")
                        .field("type", "date")
                        .field("format", "yyyy-MM-dd")
                    .endObject()
                .endObject()
            .endObject();
    // 把mappings设置给index
    indexRequest.mapping(mappings);

    // 客户端开始发送请求
    CreateIndexResponse response = client.indices().create(indexRequest, RequestOptions.DEFAULT);
    System.out.println(response.toString());
}

执行完进行查询,索引创建成功

es添加商品

将商品信息添加到Elasticsearch中,方法IndexRequest.source()

ElasticSearchServiceImpl.java

@Override
public void addGoods(ItemEntity itemEntity) throws IOException {
    // 获取操作索引的对象
    IndexRequest request = new IndexRequest(ElasticConstant.GOODS)
            .id(itemEntity.getSkuId())
            .source(JSON.toJSONString(itemEntity), XContentType.JSON);
    client.index(request, RequestOptions.DEFAULT);
}

商品同步

商品做修改操作时,将商品信息同步至Elasticsearch中

商品添加处理

ItemInfoServiceImpl.java

@Override
public ApiResponse saveItem(ItemEntity itemEntity) {
    if (itemEntity != null) {
        String id = itemEntity.getSkuId();
        if (StringUtils.isNotBlank(id)) {
            ItemEntity entity = getById(id);
            if (entity != null) {
                BeanUtils.copyProperties(itemEntity, entity);
                updateById(entity);
            }
        } else {
            EntityUtil.initEntity(itemEntity);
            itemEntity.setSkuId(IdWorker.get32UUID());
            save(itemEntity);
        }
    }
    // 同步商品信息
    rabbitTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_GOODS_EXCHANGE, RabbitMQConstant.ROUTING_KEY_GOODS_EVENT, itemEntity);
    return ApiResponse.ok();
}

RabbitMQ 处理

参考前文:SpringBoot —— 整合RabbitMQ常见问题及解决方案
RabbitMQ配置

监听队列

测试

添加商品接口

package com.local.springboot.springbootapi.api.item;

import com.local.springboot.springbootcommon.reponse.ApiResponse;
import com.local.springboot.springbootdao.entity.ItemEntity;
import com.local.springboot.springbootservice.service.item.ItemInfoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("api/item/")
@RestController
public class ItemApiController {

    @Resource
    private ItemInfoService itemInfoService;

    @RequestMapping(value = "/addItem", produces = "application/json;charset=UTF-8")
    public ApiResponse addItem(@RequestBody ItemEntity itemEntity) {
        return itemInfoService.saveItem(itemEntity);
    }
}

调用接口,然后查询es数据,同步成功


ps:如果Elasticsearch是中途搭建的,可以写个脚本方法查询所有商品添加到Elasticsearch中

商品查询

ElasticSearchServiceImpl.java

@Override
public ApiResponse selectItems(String keyword, Integer sort) {
    log.info("es查询商品信息参数:{},{}", keyword, sort);
    List<ItemEntity> entities = new ArrayList<>();
    SearchRequest request = new SearchRequest(ElasticConstant.GOODS);
    // 设置查询条件 productName、skuName 匹配keyword
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    // 设置排序
    if (sort ==1) {
        searchSourceBuilder.sort("sellPrice", SortOrder.ASC);
    }

    // keyword为空,查询全部
    if (StringUtils.isBlank(keyword)) {
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    } else {
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword, "skuName", "productName"));
        // 设置高亮属性
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("skuName", 30);
        highlightBuilder.preTags("<font color = 'red'>");
        highlightBuilder.postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);
    }
    request.source(searchSourceBuilder);
    try {
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : response.getHits().getHits()) {
            ItemEntity item = new ItemEntity();
            Map<String, Object> sourceMap = hit.getSourceAsMap();
            BeanUtils.populate(item, sourceMap);
            // 获取高亮字段的信息, 但是有些数据是没有高亮信息的
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField skuNameHigh = highlightFields.get("skuName");
            if (skuNameHigh != null) {
                item.setSkuName(skuNameHigh.getFragments()[0].toString());
            }
            entities.add(item);
        }
    } catch (Exception e) {
        log.info("es查询商品信息异常:{}", e.getMessage());
        return ApiResponse.error("es查询商品信息异常:" + e.getMessage());
    }
    return ApiResponse.ok(entities);
}

ElasticSearch为搜索结果提供高亮、排序、分页等功能

比如搜索手机,查询匹配字段有手机两个字,查询结果手机就会出行高亮效果;排序我们可以通过传入字段的值进行相应匹配排序;分页这里就不说了,后续会单独写一篇文章总结ElasticSearch的分页查询

查询测试

package com.local.springboot.springbootapi.api.search;

import com.local.springboot.springbootcommon.reponse.ApiResponse;
import com.local.springboot.springbootservice.service.search.ElasticSearchService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("api/elastic/")
@RestController
public class ElasticSearchController {

    @Resource
    private ElasticSearchService elasticSearchService;

    @RequestMapping(value = "/selectItems", produces = "application/json;charset=UTF-8")
    public ApiResponse selectItems(String keyword, Integer sort) {
        return elasticSearchService.selectItems(keyword, sort);
    }
}

接口测试

查询成功

商品删除

单个商品删除

@Test
void delete() throws IOException {
    DeleteByQueryRequest request = new DeleteByQueryRequest("goods");
    request.setQuery(QueryBuilders.multiMatchQuery("d15e00ad1be60272d81ec79dfc01d4f1","skuId"));
    BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
    // 返回结果 true
    System.out.println(response);
}

清除es中的所有商品数据

@Test
void clear() throws IOException {
    DeleteByQueryRequest request = new DeleteByQueryRequest("goods");
    request.setQuery(QueryBuilders.matchAllQuery());
    BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
    // 返回结果 true
    System.out.println(response);
}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了通过ElasticSearch在java中实现入门级全局搜索功能,后续会深入ElasticSearch搜索的其他功能

创作不易,关注💖、点赞👍、收藏🎉就是对作者最大的鼓励👏,欢迎在下方评论留言🧐

相关文章:

  • webgis —— 为瓦片构建缓存
  • 最惨面试季:“这么简单的9道题,我刷掉了90%的测试员。”
  • c++11 function模板:模板特化与可变参数函数模板
  • CSDN竞赛14期题解
  • Qt创建线程的几种方式_创建一个新线程的方法
  • Android自定义ViewGroup布局进阶,完整的九宫格实现
  • 完全开源的代码生成器之code-generator
  • C++:实现量化将期限结构与一组债券相匹配 使用四种不同的拟合方法测试实例
  • 毫米波雷达的那些知识点——增益、阈值、功耗、窗口、RF 发射功率调整、VCO、LNA
  • 1568_AURIX_TC275_电源管理_唤醒配置与状态
  • MySQL表的增删查改(上)
  • 世界杯---人生就是一届又一届世界杯
  • LeetCode 1832. 判断句子是否为全字母句
  • 多数据源解决分布式事务
  • 跳槽一次能涨多少?今天见识到跳槽天花板。
  • 07.Android之多媒体问题
  • 2018一半小结一波
  • 30天自制操作系统-2
  • CentOS 7 防火墙操作
  • input实现文字超出省略号功能
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Wamp集成环境 添加PHP的新版本
  • 从零开始学习部署
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 前端代码风格自动化系列(二)之Commitlint
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 数据仓库的几种建模方法
  • 消息队列系列二(IOT中消息队列的应用)
  • 译米田引理
  • 应用生命周期终极 DevOps 工具包
  • 阿里云重庆大学大数据训练营落地分享
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #DBA杂记1
  • #stm32整理(一)flash读写
  • #微信小程序:微信小程序常见的配置传旨
  • (0)Nginx 功能特性
  • (04)odoo视图操作
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (二)springcloud实战之config配置中心
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)ssm高校实验室 毕业设计 800008
  • (十六)一篇文章学会Java的常用API
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (一)SpringBoot3---尚硅谷总结
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)平衡树
  • (转)树状数组
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .gitignore文件设置了忽略但不生效
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
  • .NET Framework 服务实现监控可观测性最佳实践