全文檢索工具:第一章:Spring-data-elasticSearch搜索
快速上手:導(dǎo)入刪除查詢
引入依賴:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
控制層:
@Autowired
private EsProductService esProductService;
@ApiOperation(value = "簡單搜索:根據(jù)關(guān)鍵字,品牌名稱或者產(chǎn)品名稱,產(chǎn)品編號(hào),副標(biāo)題搜索(字符串:Text類型最大拆分)")
@RequestMapping(value = "/search/keyword", method = RequestMethod.GET)
@ResponseBody
public CommonResult<CommonPage<EsProduct>> searchKeyword(@RequestParam(required = false) String keyword,
@RequestParam(required = false, defaultValue = "0") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize) {
Page<EsProduct> esProductPage = esProductService.searchKeyword(keyword, pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(esProductPage));
}
@ApiOperation(value = "刪除索引庫")
@ApiImplicitParam(name = "indexName", value = "索引庫名稱",
defaultValue = "product", paramType = "query", dataType = "String")
@RequestMapping(value = "/deleteAll", method = RequestMethod.GET)
@ResponseBody
public CommonResult<Object> deleteAll(String indexName) {
int i = esProductService.deleteAll(indexName);
return CommonResult.success(i);
}
@ApiOperation(value = "導(dǎo)入所有產(chǎn)品信息數(shù)據(jù)庫中商品到ES")
@RequestMapping(value = "/importAll", method = RequestMethod.GET)
@ResponseBody
public CommonResult<Integer> importAllList() {
int count = esProductService.importAll();
return CommonResult.success(count);
}
service接口:
public interface EsProductService {
/**
* 從數(shù)據(jù)庫中導(dǎo)入所有商品到ES
*/
int importAll();
/**
* 根據(jù)關(guān)鍵字,品牌名稱或者產(chǎn)品名稱搜索(字符串:Text類型最大拆分)
* @param keyword
* @param pageNum
* @param pageSize
* @return
*/
Page<EsProduct> searchKeyword(String keyword, Integer pageNum, Integer pageSize);
/**
* 刪除索引庫
* @return
*/
int deleteAll(String indexName);
}
業(yè)務(wù)實(shí)現(xiàn)類:
@Service
public class EsProductServiceImpl implements EsProductService {
private static final Logger LOGGER = LoggerFactory.getLogger(EsProductServiceImpl.class);
@Autowired
private EsProductDao productDao;
@Autowired
private EsProductRepository productRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public int importAll() {
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);
Iterator<EsProduct> iterator = esProductIterable.iterator();
int result = 0;
while (iterator.hasNext()) {
result++;
iterator.next();
}
return result;
}
/**
* 根據(jù)關(guān)鍵字,品牌名稱或者產(chǎn)品名稱,產(chǎn)品編號(hào)搜索(字符串:Text類型最大拆分)
* @param keyword
* @param pageNum
* @param pageSize
* @return
*/
@Override
public Page<EsProduct> searchKeyword(String keyword, Integer pageNum, Integer pageSize) {
Pageable pageable = PageRequest.of(pageNum, pageSize);
return
productRepository.findByKeywordsOrProductNameOrBrandNameOrProductSnOrSubTitle(keyword,keyword,keyword,keyword,keyword,pageable);
}
/**
* 刪除索引庫
* @return
*/
@Override
public int deleteAll(String indexName) {
boolean product = elasticsearchTemplate.deleteIndex(indexName);
if(product){
return 1;
}
return 0;
}
}
EsProductDao接口
public interface EsProductDao {
List<EsProduct> getAllEsProductList(@Param("id") Long id);
}
EsProductDao.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.macro.mall.search.dao.EsProductDao">
- <resultMap id="esProductListMap" type="com.macro.mall.search.domain.EsProduct">
- <id column="productId" jdbcType="BIGINT" property="id" />
- <result column="productSn" jdbcType="VARCHAR" property="productSn"/>
- <result column="brandId" jdbcType="BIGINT" property="brandId"/>
- <result column="brandName" jdbcType="VARCHAR" property="brandName"/>
- <result column="productCategoryId" jdbcType="BIGINT" property="productCategoryId"/>
- <result column="productName" jdbcType="VARCHAR" property="productName"/>
- <result column="sale" jdbcType="BIGINT" property="sale"/>
- <result column="subTitle" jdbcType="VARCHAR" property="subTitle"/>
- <result column="price" jdbcType="DECIMAL" property="price"/>
- <result column="keywords" jdbcType="VARCHAR" property="keywords"/>
- <association property="productCategorie" columnPrefix="pc"
- javaType="com.macro.mall.search.domain.EsProductCategory">
- <id column="productCategoryId" property="id" jdbcType="BIGINT"/>
- <result column="productCategoryName" property="productCategoryName" jdbcType="VARCHAR"/>
- </association>
- <collection property="attributeList" ofType="com.macro.mall.search.domain.EsProductAttribute"
javaType="java.util.ArrayList"> - <id column="paProductAttributeId" property="paProductAttributeId" jdbcType="BIGINT"/>
- <result column="paProductAttributeName" property="paProductAttributeName" jdbcType="VARCHAR"/>
- <collection property="attributeValues" ofType="com.macro.mall.search.domain.EsProductAttributeValue"
- javaType="java.util.ArrayList">
- <id column="pavProductAttributeValueId" property="pavProductAttributeValueId" jdbcType="BIGINT"/>
- <result column="pavProductAttributeValue" property="pavProductAttributeValue" jdbcType="VARCHAR"/>
- </collection>
- </collection>
-
- </resultMap>
-
- <select id="getAllEsProductList" resultMap="esProductListMap">
- SELECT
- p.id productId,
- p.product_sn productSn,
- p.brand_id brandId,
- p.brand_name brandName,
- p.product_category_id productCategoryId,
- p.name productName,
- p.sale sale,
- p.sub_title subTitle,
- p.price price,
- p.keywords keywords,
- pav.id pavProductAttributeValueId,
- pav.`value` pavProductAttributeValue,
- pa.id paProductAttributeId,
- pa.`name` paProductAttributeName,
- pc.id pcProductCategoryId,
- pc.`name` pcProductCategoryName
- FROM pms_product p
- LEFT JOIN pms_product_attribute_value pav ON p.id = pav.product_id
- LEFT JOIN pms_product_attribute pa ON pav.product_attribute_id= pa.id
- LEFT JOIN pms_product_category pc ON p.`product_category_id` = pc.`id`
- WHERE delete_status = 0 AND publish_status = 1
- <if test="id!=null">
- and p.id=#{id}
- </if>
- </select>
- </mapper>
EsProductRepository接口
public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {
/**
* 根據(jù)關(guān)鍵字,產(chǎn)品名稱,品牌名稱,產(chǎn)品編號(hào)搜索
* @param keywords
* @param productName
* @param brandName
* @param page
* @return
*/
Page<EsProduct>
findByKeywordsOrProductNameOrBrandNameOrProductSnOrSubTitle(String
keywords,String productName,String brandName,String productSn,String
subTitle,Pageable page);
}
EsProduct實(shí)體類:
@Document(indexName = "product", type = "productInfo",shards = 2,replicas = 1,refreshInterval = "-1")
public class EsProduct implements Serializable {
private static final long serialVersionUID = 2372551074091780419L;
@Id
private Long id;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String productSn;
private Long brandId;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String brandName;
private Long productCategoryId;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String productName;
private Long sale;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String subTitle;
private BigDecimal price;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String keywords;
private List<EsProductAttribute> attributeList;
private EsProductCategory productCategorie;
//提供一下get和set方法,我就不寫了
}
EsProductAttribute實(shí)體類:
public class EsProductAttribute implements Serializable {
private static final long serialVersionUID = 4965902919813623705L;
private Long paProductAttributeId;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String paProductAttributeName;//屬性名稱
private List<EsProductAttributeValue> attributeValues;
//提供get和set方法
}
EsProductAttributeValue實(shí)體類:
public class EsProductAttributeValue implements Serializable {
private static final long serialVersionUID = 6713756365860464751L;
private Long pavProductAttributeValueId;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String pavProductAttributeValue;
//提供get 和set方法
}
測(cè)試一下:
刪除測(cè)試
導(dǎo)入數(shù)據(jù)到es測(cè)試
無條件全部搜索測(cè)試
有條件搜索測(cè)試
如果啟動(dòng)報(bào)錯(cuò),可以將原來的
@Document(indexName = "search", type = "article",shards = 1,replicas = 0)
改一下索引庫的名稱
@Document(indexName = "search11", type = "article",shards = 1,replicas = 0)
再次啟動(dòng)刪除索引庫再重新導(dǎo)入,之后就不會(huì)報(bào)錯(cuò)了。