集成ik分析器

This commit is contained in:
zhh 2018-06-22 11:25:03 +08:00
parent 9523533c99
commit a76e46e57b
4 changed files with 27 additions and 5 deletions

View File

@ -1,7 +1,11 @@
package com.macro.mall.search.domain;
import com.macro.mall.model.PmsProductAttributeValue;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.io.Serializable;
import java.math.BigDecimal;
@ -11,18 +15,25 @@ import java.util.List;
* 搜索中的商品信息
* Created by macro on 2018/6/19.
*/
@Document(indexName = "pms", type = "product")
@Document(indexName = "pms", type = "product",shards = 1,replicas = 0)
public class EsProduct implements Serializable {
private static final long serialVersionUID = -1L;
@Id
private Long id;
@Field(index = FieldIndex.not_analyzed,type = FieldType.String)
private String productSn;
private Long brandId;
@Field(index = FieldIndex.not_analyzed,type = FieldType.String)
private String brandName;
private Long productCategoryId;
@Field(index = FieldIndex.not_analyzed,type = FieldType.String)
private String productCategoryName;
private String pic;
@Field(analyzer = "ik_max_word",type = FieldType.String)
private String name;
@Field(analyzer = "ik_max_word",type = FieldType.String)
private String subTitle;
@Field(analyzer = "ik_max_word",type = FieldType.String)
private String keywords;
private BigDecimal price;
private Integer sale;

View File

@ -105,11 +105,11 @@ public class EsProductServiceImpl implements EsProductService {
}
//搜索
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.matchPhraseQuery("name", keyword),
.add(QueryBuilders.matchQuery("name", keyword),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.matchPhraseQuery("subTitle", keyword),
.add(QueryBuilders.matchQuery("subTitle", keyword),
ScoreFunctionBuilders.weightFactorFunction(500))
.add(QueryBuilders.matchPhraseQuery("keywords", keyword),
.add(QueryBuilders.matchQuery("keywords", keyword),
ScoreFunctionBuilders.weightFactorFunction(200))
.scoreMode("sum").setMinScore(10f);
if (StringUtils.isEmpty(keyword)) {

View File

@ -23,7 +23,7 @@
p.recommand_status recommandStatus,
p.stock stock,
p.promotion_type promotionType,
P.keywords keywords
P.keywords keywords,
p.sort sort,
a.id attr_id,
a.value attr_value,

View File

@ -2,19 +2,24 @@ package com.macro.mall.search;
import com.macro.mall.search.dao.EsProductDao;
import com.macro.mall.search.domain.EsProduct;
import com.macro.mall.search.repository.EsProductRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MallSearchApplicationTests {
@Autowired
private EsProductDao productDao;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void contextLoads() {
}
@ -23,5 +28,11 @@ public class MallSearchApplicationTests {
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
System.out.print(esProductList);
}
@Test
public void testEsMapping(){
elasticsearchTemplate.putMapping(EsProduct.class);
Map mapping = elasticsearchTemplate.getMapping(EsProduct.class);
System.out.println(mapping);
}
}