Search in sources :

Example 6 with EsProduct

use of com.macro.mall.search.domain.EsProduct in project mall-swarm by macrozheng.

the class EsProductServiceImpl method create.

@Override
public EsProduct create(Long id) {
    EsProduct result = null;
    List<EsProduct> esProductList = productDao.getAllEsProductList(id);
    if (esProductList.size() > 0) {
        EsProduct esProduct = esProductList.get(0);
        result = productRepository.save(esProduct);
    }
    return result;
}
Also used : EsProduct(com.macro.mall.search.domain.EsProduct)

Example 7 with EsProduct

use of com.macro.mall.search.domain.EsProduct in project mall-swarm by macrozheng.

the class EsProductServiceImpl method delete.

@Override
public void delete(List<Long> ids) {
    if (!CollectionUtils.isEmpty(ids)) {
        List<EsProduct> esProductList = new ArrayList<>();
        for (Long id : ids) {
            EsProduct esProduct = new EsProduct();
            esProduct.setId(id);
            esProductList.add(esProduct);
        }
        productRepository.deleteAll(esProductList);
    }
}
Also used : ArrayList(java.util.ArrayList) EsProduct(com.macro.mall.search.domain.EsProduct)

Example 8 with EsProduct

use of com.macro.mall.search.domain.EsProduct in project mall by macrozheng.

the class EsProductServiceImpl method create.

@Override
public EsProduct create(Long id) {
    EsProduct result = null;
    List<EsProduct> esProductList = productDao.getAllEsProductList(id);
    if (esProductList.size() > 0) {
        EsProduct esProduct = esProductList.get(0);
        result = productRepository.save(esProduct);
    }
    return result;
}
Also used : EsProduct(com.macro.mall.search.domain.EsProduct)

Example 9 with EsProduct

use of com.macro.mall.search.domain.EsProduct in project mall by macrozheng.

the class EsProductServiceImpl method recommend.

@Override
public Page<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    List<EsProduct> esProductList = productDao.getAllEsProductList(id);
    if (esProductList.size() > 0) {
        EsProduct esProduct = esProductList.get(0);
        String keyword = esProduct.getName();
        Long brandId = esProduct.getBrandId();
        Long productCategoryId = esProduct.getProductCategoryId();
        // 根据商品标题、品牌、分类进行搜索
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword), ScoreFunctionBuilders.weightFactorFunction(8)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword), ScoreFunctionBuilders.weightFactorFunction(2)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", keyword), ScoreFunctionBuilders.weightFactorFunction(2)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("brandId", brandId), ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId), ScoreFunctionBuilders.weightFactorFunction(3)));
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders).scoreMode(FunctionScoreQuery.ScoreMode.SUM).setMinScore(2);
        // 用于过滤掉相同的商品
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.mustNot(QueryBuilders.termQuery("id", id));
        // 构建查询条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withFilter(boolQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
        if (searchHits.getTotalHits() <= 0) {
            return new PageImpl<>(null, pageable, 0);
        }
        List<EsProduct> searchProductList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return new PageImpl<>(searchProductList, pageable, searchHits.getTotalHits());
    }
    return new PageImpl<>(null);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) ArrayList(java.util.ArrayList) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) Pageable(org.springframework.data.domain.Pageable) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) EsProduct(com.macro.mall.search.domain.EsProduct)

Example 10 with EsProduct

use of com.macro.mall.search.domain.EsProduct in project mall by macrozheng.

the class EsProductServiceImpl method delete.

@Override
public void delete(List<Long> ids) {
    if (!CollectionUtils.isEmpty(ids)) {
        List<EsProduct> esProductList = new ArrayList<>();
        for (Long id : ids) {
            EsProduct esProduct = new EsProduct();
            esProduct.setId(id);
            esProductList.add(esProduct);
        }
        productRepository.deleteAll(esProductList);
    }
}
Also used : ArrayList(java.util.ArrayList) EsProduct(com.macro.mall.search.domain.EsProduct)

Aggregations

EsProduct (com.macro.mall.search.domain.EsProduct)10 ArrayList (java.util.ArrayList)6 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)6 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)6 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)4 PageImpl (org.springframework.data.domain.PageImpl)4 Pageable (org.springframework.data.domain.Pageable)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)2