在电商项目中,我们经常会使用到全局搜索来查询自己想要购买的商品,而商品的数量非常多,而且分类繁杂。
面对这样复杂的搜索业务和数据量,使用传统数据库搜索就显得力不从心,一般我们都会使用全文检索技术,比如Solr,Elasticsearch
。
Windows环境:
参考前文:Elasticsearch 安装及启动【Windows】、RabbitMQ安装和使用
Linux环境:
参考前文:Elasticsearch 安装及启动【Linux】、Linux安装RabbitMq
这里为了方便演示,我们统一在windows环境下安装
org.elasticsearch.client elasticsearch-rest-high-level-client 7.12.0
修改配置文件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;@Beanpublic RestHighLevelClient getClient() {return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));}
}
全局搜索是在Elasticsearch中搜索商品信息,所以我们需要将商品信息同步至Elasticsearch中,在商品修改、新增、删除时通过RabbitMQ异步处理Elasticsearch中的商品信息
这里直接在测试类中添加goods
索引,mapping创建前端商品列表需要展示的字段
@Test
void createIndex() throws IOException {// 创建一个索引请求CreateIndexRequest indexRequest = new CreateIndexRequest("goods");// 创建一个SettingsSettings.Builder settings = Settings.builder();settings.put("number_of_shards", "3"); // 设置三个分片settings.put("number_of_replicas", "1"); // 设置一个备份// 把settings设置给request对象indexRequest.settings(settings);// 创建一个mappingsXContentBuilder 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设置给indexindexRequest.mapping(mappings);// 客户端开始发送请求CreateIndexResponse response = client.indices().create(indexRequest, RequestOptions.DEFAULT);System.out.println(response.toString());
}
执行完进行查询,索引创建成功
将商品信息添加到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();
}
参考前文: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 {@Resourceprivate 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 entities = new ArrayList<>();SearchRequest request = new SearchRequest(ElasticConstant.GOODS);// 设置查询条件 productName、skuName 匹配keywordSearchSourceBuilder 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("");highlightBuilder.postTags("");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 sourceMap = hit.getSourceAsMap();BeanUtils.populate(item, sourceMap);// 获取高亮字段的信息, 但是有些数据是没有高亮信息的Map 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 {@Resourceprivate 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);// 返回结果 trueSystem.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);// 返回结果 trueSystem.out.println(response);
}
以上就是今天要讲的内容,本文仅仅简单介绍了通过ElasticSearch在java中实现入门级全局搜索功能,后续会深入ElasticSearch搜索的其他功能
创作不易,关注💖、点赞👍、收藏🎉就是对作者最大的鼓励👏,欢迎在下方评论留言🧐