Elasticsearch actual combat (3): Springboot implements Elasticsearch search recommendation

Series Article Index

Elasticsearch actual combat (1): Springboot implements Elasticsearch unified retrieval function
Elasticsearch actual combat (2): Springboot implements Elasticsearch automatic Chinese character and pinyin completion, Springboot implements automatic spelling error correction
Elasticsearch actual combat (3): Springboot implements Elasticsearch search recommendation
Elasticsearch actual combat (4) : Springboot implements Elasticsearch index aggregation and drill-down analysis
Elasticsearch actual combat (5): Springboot implements Elasticsearch e-commerce platform log embedding points and search hot words

1. What is search recommendation

For example: Keyword input [Adidas Nike jacket sports shoes socks]

Wang~No products related to "Adidas Nike Jacket Sports Shoes Socks" were found. Recommend related products of "Adidas Nike Sports Shoes" for you, or try:
insert image description here

2. Add test data

/*
 * @Description: 批量新增文档,可自动创建索引、自动创建映射
 * @Method: bulkAddDoc
 * @Param: [indexName, map]
 *
 */
public static RestStatus bulkAddDoc(CommonEntity commonEntity) throws Exception {
    
    
    //通过索引构建批量请求对象
    BulkRequest bulkRequest = new BulkRequest(commonEntity.getIndexName());
    //循环前台list文档数据
    for (int i = 0; i < commonEntity.getList().size(); i++) {
    
    
        bulkRequest.add(new IndexRequest().source(XContentType.JSON, SearchTools.mapToObjectGroup(commonEntity.getList().get(i))));
    }
    //执行批量新增
    BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    return bulkResponse.status();
}

public static void main(String[] args) throws Exception {
    
    
    // 批量插入
    CommonEntity commonEntity = new CommonEntity();
    commonEntity.setIndexName("product_completion_index"); // 索引名
    List<Map<String, Object>> list = new ArrayList<>();
    commonEntity.setList(list);
    list.add(new CommonMap<String, Object>().putData("searchkey", "阿迪达斯袜子").putData("name", "阿迪达斯袜子"));
    list.add(new CommonMap<String, Object>().putData("searchkey", "阿迪达斯外套").putData("name", "阿迪达斯外套"));
    list.add(new CommonMap<String, Object>().putData("searchkey", "阿迪达斯运动鞋").putData("name", "阿迪达斯运动鞋"));
    list.add(new CommonMap<String, Object>().putData("searchkey", "耐克运动鞋").putData("name", "耐克运动鞋"));
    bulkAddDoc(commonEntity);
}

Inquire:

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

insert image description here

3. Realization of search recommendation

1. es official website

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

insert image description here
insert image description here

2. Java implements search recommendation

Define word breaker: analyzer("ik_smart")
define algorithm: .stringDistance(TermSuggestionBuilder.StringDistanceImpl.NGRAM);

/*
 * @Description: 自动补全 根据用户的输入联想到可能的词或者短语
 * @Method: suggester
 * @Param: [commonEntity]
 * @Update:
 * @since: 1.0.0
 * @Return: org.elasticsearch.action.search.SearchResponse
 * >>>>>>>>>>>>编写思路简短总结>>>>>>>>>>>>>
 * 1、定义远程查询
 * 2、定义查询请求(评分排序)
 * 3、定义自动完成构建器(设置前台建议参数)
 * 4、将自动完成构建器加入到查询构建器
 * 5、将查询构建器加入到查询请求
 * 6、获取自动建议的值(数据结构处理)
 */
public static String tSuggest(CommonEntity commonEntity) throws Exception {
    
    
    //定义返回
    String tSuggestString = new String();
    //定义查询请求
    SearchRequest  searchRequest=new SearchRequest(commonEntity.getIndexName());
    //定义查询构建器
    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
    searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));

    //构造词条建议语句,搜索条件字段
//        TermSuggestionBuilder termSuggestiontextBuilder = SuggestBuilders.termSuggestion(commonEntity.getSuggestFileld());
    TermSuggestionBuilder termSuggestiontextBuilder=new TermSuggestionBuilder(commonEntity.getSuggestFileld());
    //搜索关键字
    termSuggestiontextBuilder.text(commonEntity.getSuggestValue());
    //输入的建议词分词
    termSuggestiontextBuilder.analyzer("ik_smart");
    //建议文本术语必须包含的最小长度。默认值为4。(旧名称“ min_word_len”已弃用)
    termSuggestiontextBuilder.minWordLength(2);
    //用于比较建议术语的相似程度的字符串距离实现
    termSuggestiontextBuilder.stringDistance(TermSuggestionBuilder.StringDistanceImpl.NGRAM);
    //将词条建议器加入到查询构建器中
    searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion("common-suggest",termSuggestiontextBuilder));
    //将查询构建器加入到查询请求中
    searchRequest.source(searchSourceBuilder);
    //定义查找响应
    SearchResponse suggestResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    //定义完成建议对象
    TermSuggestion termSuggestion = suggestResponse.getSuggest().getSuggestion("common-suggest");
    //获取返回数据
    List<TermSuggestion.Entry.Option> optionsList = termSuggestion.getEntries().get(0).getOptions();
    //从optionsList取出结果
    if (!CollectionUtils.isEmpty(optionsList) && optionsList.get(0).getText()!=null) {
    
    
        tSuggestString = optionsList.get(0).getText().toString();
    }
    return tSuggestString;
}
public static void main(String[] args) throws Exception {
    
    
    CommonEntity suggestEntity = new CommonEntity();
    suggestEntity.setIndexName("product_completion_index"); // 索引名
    suggestEntity.setSuggestFileld("name"); // 自动补全查找列
    suggestEntity.setSuggestValue("阿迪达斯 耐克 外套 运动鞋 袜子"); //  自动补全输入的关键字
    System.out.println(tSuggest(suggestEntity)); // 结果:阿迪达斯外套
}

3. Summary

1. min_word_length, the minimum length that a suggested text term must contain. The default value is 4. Remember!
2. string_distance, used to compare the string distance implementation of the similarity of suggested terms, using ngram

Guess you like

Origin blog.csdn.net/A_art_xiang/article/details/132260902