网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
}
### 3.准备商品索引创建DSL语句!
将商品名称,标题以及详细描述添加到all字段,直接使用all字段搜索即可。
删除索引结构
DELETE /product
创建商品索引!
根据多列搜索性能较差,组成成一列搜索提高性能
PUT /product
{
“mappings”: {
“properties”: {
“productId”:{
“type”: “integer”
},
“productName”:{
“type”: “text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“categoryId”:{
“type”: “integer”
},
“productTitle”:{
“type”: “text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“productIntro”:{
“type”:“text”,
“analyzer”: “ik_smart”,
“copy_to”: “all”
},
“productPicture”:{
“type”: “keyword”,
“index”: false
},
“productPrice”:{
“type”: “double”,
“index”: true
},
“productSellingPrice”:{
“type”: “double”
},
“productNum”:{
“type”: “integer”
},
“productSales”:{
“type”: “integer”
},
“all”:{
“type”: “text”,
“analyzer”: “ik_max_word”
}
}
}
}
#查询索引
GET /product/_mapping
#全部查询
GET /product/_search
{
“query”: {
“match_all”: {
}
}
}
#关键字查询
GET /product/_search
{
“query”: {
“match”: {
“all”: “最好”
}
}
}
关键字 和 添加分页
GET /product/_search
{
“query”: {
“match”: {
“all”: “最好”
}
},
“from”: 0,
“size”: 1
}
添加数据
POST /product/_doc/1
{
“productId”:1,
“productName”:“雷碧”,
“productTitle”:“最好的碳酸饮料”,
“categoryId”:1,
“productIntro”:“硫酸+煤炭制品最好的产品!”,
“productPicture”:“http://www.baidu.com”,
“productPrice”:10.5,
“productSellingPrice”:6.0,
“productNum”:10,
“productSales”:5
}
删除数据
DELETE /product/_doc/1
以上添加到es中,使用kibana添加索引即可。
### 3.准备商品doc模型
添加商品Doc实体类,定义all字段,重新构造方法,传入product参数,注意Product类中加@JsonIgnoreProperties(ignoreUnknown = true)。
@Data
@NoArgsConstructor
public class ProductDoc extends Product {
/**
* 用于模糊查询字段,由商品名,标题和描述组成
*/
private String all;
public ProductDoc(Product product) {
super(product.getProductId(),product.getProductName(),
product.getCategoryId(),product.getProductTitle(),
product.getProductIntro(),product.getProductPicture(),
product.getProductPrice(),product.getProductSellingPrice(),
product.getProductNum(),product.getProductSales());
this.all = product.getProductName()+product.getProductTitle()+product.getProductIntro();
}
}
### 4.导入数据,添加配置类
mq序列化方式选择json,构建es客户端对象,并纳入容器管理
/**
-
projectName: b2c-cloud-store
-
description: 消息队列配置
*/
@Configuration
public class SearchConfiguration {/**
-
mq序列化方式,选择json!
-
@return
*/
@Bean
public MessageConverter messageConverter(){return new Jackson2JsonMessageConverter();
}
/**
-
es客户端添加到ioc容器
-
@return
*/
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client =
new RestHighLevelClient(
RestClient.builder(HttpHost.create(“http://自己的es服务地址:9200”)));return client;
}
}
-
### 5.修改启动类
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableFeignClients(clients = ProductClient.class)
public class SearchApplication {
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class,args);
}
}
### 6.程序启动监控
监控程序启动,初始化es数据。
初始化逻辑:在启动时就执行商品查询,然后判断是否存在es索引,用getindexrequest获取,对象传入索引名称。如果不存在索引就创建索引,然后删除全部数据,然后重新批量插入数据。
es入门常见的用法可以参照这篇博客:[http://t.csdnimg.cn/QsBAm]( )
@Component
@Slf4j
public class SpringBootListener implements ApplicationRunner {
/**
* 在此方法完成es数据的同步
* 1.判断es中的product索引是否存在
* 2.如果不存在,java代码创建一个
* 3.如果存在,删除原来的数据
* 4.查询商品全部数据
* 5.进行es库的更新工作(插入)
* @param args
* @throws Exception
*/
@Autowired
private RestHighLevelClient restHighLevelClient;
@Autowired
private ProductClient productClient;
private String indexStr="{\\n\" +\n" +
" \" \\\"mappings\\\": {\\n\" +\n" +
" \" \\\"properties\\\": {\\n\" +\n" +
" \" \\\"productId\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productName\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"text\\\",\\n\" +\n" +
" \" \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
" \" \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"categoryId\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productTitle\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"text\\\",\\n\" +\n" +
" \" \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
" \" \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productIntro\\\":{\\n\" +\n" +
" \" \\\"type\\\":\\\"text\\\",\\n\" +\n" +
" \" \\\"analyzer\\\": \\\"ik_smart\\\",\\n\" +\n" +
" \" \\\"copy_to\\\": \\\"all\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productPicture\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"keyword\\\",\\n\" +\n" +
" \" \\\"index\\\": false\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productPrice\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"double\\\",\\n\" +\n" +
" \" \\\"index\\\": true\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productSellingPrice\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"double\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productNum\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"productSales\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"integer\\\"\\n\" +\n" +
" \" },\\n\" +\n" +
" \" \\\"all\\\":{\\n\" +\n" +
" \" \\\"type\\\": \\\"text\\\",\\n\" +\n" +
" \" \\\"analyzer\\\": \\\"ik_max_word\\\"\\n\" +\n" +
" \" }\\n\" +\n" +
" \" }\\n\" +\n" +
" \" }\\n\" +\n" +
" \"}";
@Override
public void run(ApplicationArguments args) throws Exception {
GetIndexRequest getIndexRequest=new GetIndexRequest("product");
//判断是否存在
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
//存在查询删除
if(exists){
DeleteByQueryRequest deleteByQueryRequest=new DeleteByQueryRequest("product");
deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
restHighLevelClient.deleteByQuery(deleteByQueryRequest,RequestOptions.DEFAULT);
}else{
//不存在创建索引
CreateIndexRequest createIndexRequest=new CreateIndexRequest("product");
createIndexRequest.source(indexStr, XContentType.JSON);
restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
}
//查询全部数据
List<Product> productList=productClient.allList();
//遍历批量数据插入
BulkRequest bulkRequest=new BulkRequest();
ObjectMapper objectMapper=new ObjectMapper();
for (Product product : productList) {
ProductDoc productDoc=new ProductDoc(product);
//插入数据的作用
IndexRequest indexRequest=new IndexRequest("product").id(product.getProductId().toString());
//productDoc转成JSON放入
String json = objectMapper.writeValueAsString(productDoc);
indexRequest.source(json,XContentType.JSON);
bulkRequest.add(indexRequest);
}
restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
}
}
以上完成es的数据同步操作。
## 三、商品搜索服务
### 1.准备实体类
定义ProductParamSearch参数实体类,包含字段有search 查询关键字,以及分页字段:currentPage和pageSize。
### 2.定义接口
定义接口/search/product根据关键字和分页参数,进行es索引查询,并将结果封装到R中,返回商品服务即可。
@RestController
@RequestMapping(“search”)
public class SearchController {
@Autowired
private SearchService searchService;
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
d
private SearchService searchService;
[外链图片转存中…(img-RAnSibO4-1715807507810)]
[外链图片转存中…(img-MkkpgTcz-1715807507810)]
[外链图片转存中…(img-pY2COxw4-1715807507810)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新