#查询索引
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;
@PostMapping("product")
public R productList(@RequestBody ProductParamsSearch productParamsSearch) throws JsonProcessingException {
return searchService.search(productParamsSearch);
}
}
### 3.实现类
实现逻辑
首先判断关键字是否为空,为空就查询全部,调用searchRequest.source().query(QueryBuilders.matchAllQuery)进行查询
否则就根据关键字对all进行查询,之后设置分页参数,设置返回结果response,调用es客户端的search方法获取结果,然后解析结果,解析es查询结果汇总的hits数目用于统计关键字查询商品数量,对获取到的结果进行遍历,获取单独的JSON数据,把其加add到结果集合中。
@Autowired
private RestHighLevelClient client;
/**
-
商品搜索
-
@param productParamsSearch
-
@return
*/
@Override
public R search(ProductParamsSearch productParamsSearch) throws JsonProcessingException {SearchRequest searchRequest = new SearchRequest(“product”);
if (StringUtils.isEmpty(productParamsSearch.getSearch())){
//如果为null,查询全部
searchRequest.source().query(QueryBuilders.matchAllQuery());
}else{
//不为空 all字段进行搜索
searchRequest.source().query(QueryBuilders.matchQuery(“all”,productParamsSearch.getSearch()));
}//设置分页参数
searchRequest.source().from(productParamsSearch.getFrom());
searchRequest.source().size(productParamsSearch.getSize());SearchResponse response = null;
try {
response = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}//结果集解析
//获取集中的结果
SearchHits hits = response.getHits();
//获取符合的数量
long total = hits.getTotalHits().value;SearchHit[] items = hits.getHits();
List productList = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
tMapper = new ObjectMapper();
[外链图片转存中…(img-aERkxi5u-1714204230631)]
[外链图片转存中…(img-8g9nO8m4-1714204230632)]
[外链图片转存中…(img-ezphSV3l-1714204230632)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新