其他分享
首页 > 其他分享> > 技术中台下终搜技术解决方案(day02-3)

技术中台下终搜技术解决方案(day02-3)

作者:互联网

写在前面:
文章为作者原创
如需转载,请注明出处

3 电商平台产品推荐

3.1 什么是搜索推荐

背景介绍。并非千人千面

在新的系统里面,早期都是没有很多数据,很难直接拿来做推荐系统,这就是有些算法存在冷启动的问题,所以在系统早期推荐都是基于热度(流行度)或者基于运营策略的推荐





//搜索【迪奥】,本站无该品牌商品
没有找到 "迪奥" 相关的商品, 为您推荐 "香水" 的搜索结果。或者试试 "香氛"  "眼镜" 
 
//搜索【puna 运动鞋 上衣】
汪~没有找到与“puna 运动鞋 上衣”相关的商品,为您推荐“ 运动上衣”的相关商品,或者试试:运动鞋、上衣
 
//搜索【puma 上衣】,结果太少
"puma 上衣" 搜索结果太少了,试试 "上衣"  "PUMA"  "PUMA 休闲" 关键词搜索
 
 
 
//搜索【blackjauk】,拼写错误
没有找到 "blackjauk" 相关的商品, 为您推荐 "BLACKJACK" 的搜索结果。或者试试 "BLACKJACK T恤"  "BLACKJACK 休闲裤" 



我们此处案例是一个搜索热词推荐,如同百度右侧热词推荐

例如:关键词输入【阿迪达斯 耐克 外套 运动鞋 袜子】

汪~没有找到与“阿迪达斯 耐克 外套 运动鞋 袜子”相关的商品,为您推荐“ 阿迪达斯耐克运动鞋”的相关商品,或者试试:

3.2 产品推荐OpenAPI

GET product_completion_index/_search
{
  "query": {
    "match": {
      "name": "阿迪达斯 耐克 外套 运动鞋 袜子"
    }
  },
  "suggest": {
    "czbk-suggestion": {
      "text": "阿迪达斯 耐克 外套 运动鞋 袜子",
      "term": {
        "field": "name",
        "min_word_length": 2,
        "string_distance": "ngram",
        "analyzer": "ik_smart"
      }
    }
  }
}
 

注意的地方,查看官网

https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-suggesters.html#term-suggester

1、定义搜索推荐接口

package com.itheima.service;

import com.itheima.commons.pojo.CommonEntity;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;

import java.util.List;
import java.util.Map;

/**
 * @Class: ElasticsearchDocumentService
 * @Package com.itheima.service
 * @Description: 文档操作接口
 * @date 2020/3/19 23:13
 * @Company: http://www.itheima.com/
 */
public interface ElasticsearchDocumentService {

   //搜索推荐(当输入的关键词过多的时候系统进行推荐)
    public String tSuggest(CommonEntity commonEntity) throws Exception;


}

2、定义搜索推荐实现

 /*
     * @Description: 搜索推荐(当输入的关键词过多的时候系统进行推荐)
     * @Method: tSuggest
     * @Param: [commonEntity]
     * @Date: 2020/4/1 23:27
     * @Update:
     * @since: 1.0.0
     * @Return: java.util.List<java.lang.String>
     *
     */
    public String tSuggest(CommonEntity commonEntity) throws Exception {
        //定义返回
        String tSuggestString = new String();
        //构造词条建议语句,搜索条件字段
        TermSuggestionBuilder termSuggestiontextBuilder = SuggestBuilders.termSuggestion(commonEntity.getSuggestFileld());
        //搜索关键字
        termSuggestiontextBuilder.text(commonEntity.getSuggestValue());
        //匹配数量
//        termSuggestiontextBuilder.size(commonEntity.getSuggestCount());
        //输入的建议词分词
        termSuggestiontextBuilder.analyzer("ik_smart");
        //建议文本术语必须包含的最小长度。默认值为4。(旧名称“ min_word_len”已弃用)
        termSuggestiontextBuilder.minWordLength(2);
        //用于比较建议术语的相似程度的字符串距离实现
        termSuggestiontextBuilder.stringDistance(TermSuggestionBuilder.StringDistanceImpl.NGRAM);
        //czbk-suggest为返回的字段,所有返回将在czbk-suggest里面,可写死,sort按照评分排序
        SearchRequest searchRequest = new SearchRequest().indices(commonEntity.getIndexName()).source(new SearchSourceBuilder().sort(new ScoreSortBuilder().order(SortOrder.DESC)).suggest(
                new SuggestBuilder().addSuggestion("czbk-suggest", termSuggestiontextBuilder)
        ));

        //定义查找响应
        SearchResponse suggestResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        //定义完成建议对象
        TermSuggestion termSuggestion = suggestResponse.getSuggest().getSuggestion("czbk-suggest");
        //获取返回数据
        List<TermSuggestion.Entry.Option> optionsList = termSuggestion.getEntries().get(0).getOptions();
        //从optionsList取出结果
        if (!CollectionUtils.isEmpty(optionsList)) {
            tSuggestString = optionsList.get(0).getText().toString();
        }
        return tSuggestString;
    }

3、定义搜索推荐控制器

    /*
     * @Description: 搜索推荐(当输入的关键词过多的时候系统进行推荐)
     * @Method: tSuggest
     * @Param: [commonEntity]
     * @Date: 2020/4/1 23:22
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "/tsuggest")
    public ResponseData tSuggest(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            rData.setResultEnum(ResultEnum.param_isnull);
            return rData;
        }
        //批量查询返回结果
        String result = null;
        try {
            //通过高阶API调用批量新增操作方法
            result = elasticsearchDocumentService.tSuggest(commonEntity);
            //通过类型推断自动装箱(多个参数取交集)
            rData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.tsuggest_get_doc_success.getMessage());
        } catch (Exception e) {
            //打印到控制台
            e.printStackTrace();
            //日志记录
            logger.error(TipsEnum.tsuggest_get_doc_fail.getMessage());
            //构建错误返回信息
            rData.setResultEnum(ResultEnum.error);
        }
        return rData;
    }

4、语言处理调用验证

http://172.17.0.225:6666/v1/docs/tsuggest

参数

{
    "indexName": "product_completion_index",
    "suggestFileld": "name",
    "suggestValue": "阿迪达斯 耐克 外套 运动鞋 袜子"
}

indexName索引名称

suggestFileld:自动补全查找列

suggestValue:自动补全输入的关键字

返回

{
    "code": "200",
    "desc": "操作成功!",
    "data": "阿迪达斯外套"
}

3.3 总结

1、min_word_length,建议文本术语必须包含的最小长度。默认值为4。切记!

2、string_distance,用于比较建议术语的相似程度的字符串距离实现,使用ngram

写在后面:
技术中台下终搜技术解决方案 课程合计分为三天
持续更新

ps:当前页面为第一天的【第一章节】

标签:运动鞋,day02,推荐,技术,termSuggestiontextBuilder,搜索,commonEntity,import,台下
来源: https://www.cnblogs.com/xingpu/p/14037705.html