实现商品的搜索功能
2019-03-29 本文已影响270人
岳峙
新建ly-search
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>leyou</artifactId>
<groupId>com.leyou.parent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ly-search</artifactId>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.leyou.service</groupId>
<artifactId>ly-item-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
配置文件
application.yml
server:
port: 8083
spring:
application:
name: search-service
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.160.128:9300
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
prefer-ip-address: true
ip-address: 127.0.0.1
instance-id: ${spring.application.name}:${server.port}
然后是启动类
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @Author: Pandy
* @Date: 2019/3/29 21:10
* @Version 1.0
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LySearchApplication {
public static void main(String[] args) {
SpringApplication.run(LySearchApplication.class);
}
}
目前我们具有商品表的spu 和 sku 究竟为哪个建立索引?
新建pojo类
package com.leyou.search.pojo;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.persistence.Id;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Document(indexName = "goods", type = "docs", shards = 1, replicas = 0)
public class Goods {
@Id
private Long id; // spuId
@Field(type = FieldType.text, analyzer = "ik_max_word")
private String all; // 所有需要被搜索的信息,包含标题,分类,甚至品牌
@Field(type = FieldType.keyword, index = false)
private String subTitle;// 卖点
private Long brandId;// 品牌id
private Long cid1;// 1级分类id
private Long cid2;// 2级分类id
private Long cid3;// 3级分类id
private Date createTime;// 创建时间
private List<Long> price;// 价格
@Field(type = FieldType.keyword, index = false)
private String skus;// sku信息的json结构
private Map<String, Object> specs;// 可搜索的规格参数,key是参数名,值是参数值
}
上面的fieldTypej后面的属性竟然是小写..
项目中的功能实现
根据多个商品的id 查询商品的集合
/**
* 根据多个商品分类的id 查询商品的集合
* @return
*/
@GetMapping("list/ids")
public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
return ResponseEntity.ok(categoryService.queryByIds(ids));
}
service层
/**
* 根据多个商品分类的id 查询商品分类的集合
* @param ids
* @return
*/
public List<Category> queryByIds(List<Long> ids){
List<Category> categories = categoryMapper.selectByIdList(ids);
if (CollectionUtils.isEmpty(categories)){
throw new LyException(ExceptionEnums.CATEGORY_NOT_FOUND);
}
return categories;
}
其中根据ids查询 底层还是不可见 因为是框架自身提供的方法
但是在控制台中可以看见相应的sql查询语句
image.png
测试发现
image.png
根据商品的id查询商品的集合成功
根据id查询对应的品牌
/**
* 根据id查询品牌
* @param id
* @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中
* @return
*/
@GetMapping("{id}")
public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
return ResponseEntity.ok(brandService.queryById(id));
}
根据商品id查询
/**
* 根据商品id查询
* @param id
* @return
*/
public Brand queryById(Long id){
Brand brand = brandMapper.selectByPrimaryKey(id);
if (brand == null){
throw new LyException(ExceptionEnums.BRAND_NOT_FOUND);
}
return brand;
}
测试发现
image.png
测试是成功的只不过没有查到数据而已
控制台的语句
image.png