SpringBoot知识体系(四)springboot整合Elasticsearch(4)

suggest的自动补全与IK分词

IK分词安装方式可见 ElasticSearch安装中文分词

AnalyzeRequestBuilder实现IK分词

Java代码

// 调用 IK 分词
AnalyzeRequestBuilder requestBuilder= new AnalyzeRequestBuilder(this.esClient,AnalyzeAction.INSTANCE,INDEX_NAME,
        “title”,
        “name”);
requestBuilder.setAnalyzer("ik_smart");

AnalyzeResponse response = requestBuilder.get();
List<AnalyzeResponse.AnalyzeToken> tokens = response.getTokens();
if(tokens ==null){
    logger.warn("Can not analyze token for person: "+indexTemplate.getId());
    return false;
}
// 循环赋值
List<PersonSuggest> suggests = new ArrayList<>();
for (AnalyzeResponse.AnalyzeToken token : tokens) {
    // 排除数字类型 & 小于2个字符的分词结果
    if("".equals(token.getType())||token.getTerm().length()<2){
        continue;
    }

    PersonSuggest suggest = new PersonSuggest();
    suggest.setInput(token.getTerm());
    suggests.add(suggest);
}
// 定制化小区自动补全
PersonSuggest suggest = new PersonSuggest();
suggest.setInput(indexTemplate.getTitle());
suggests.add(suggest);

indexTemplate.setSuggest(suggests);

indexTemplate为索引模板
PersonSuggest 为自动补全模板
INDEX_NAME,这里是指在 ES 设置的索引名称。
从容器注入的 ElasticsearchTemplate Bean 中获取 Client ,再通过 AnalyzeRequestBuilder 分析请求类型中进行分词并获取分词结果 AnalyzeResponse.AnalyzeToken 列表。

completion suggest实现搜索提示

官网的Elasticsearch 请求DSL:

www.elastic.co地址: completion-suggester
SpringBoot知识体系(四)springboot整合Elasticsearch(4)_第1张图片

Java代码实现
 // 构建CompletionSuggestionBuilder 
 CompletionSuggestionBuilder suggestionBuilder = SuggestBuilders
 		 .completionSuggestion("autocomplete") // 设置查询属性名称,可以自定义任意名,与下面的getSuggestion一致
 		 .field("suggest")   // 自动补全的字段名
         .text(prefix)		 // 自动补全内容
         .size(5);			 // 获取条数限制
         	
 SuggestBuilder suggestBuilder = new SuggestBuilder();
 suggestBuilder.addSuggestion(suggestionBuilder);
// suggestBuilder.setText("autocomplete");

 // 设置SearchRequestBuilder 的suggestionBuilder
 SearchRequestBuilder requestBuilder = this.esClient.prepareSearch(INDEX_NAME)
         .setTypes(INDEX_TYPE)
         .addSuggestion(suggestionBuilder);
 logger.debug(requestBuilder.toString());
 SearchResponse response = requestBuilder.get();
 Suggest suggest = response.getSuggest();
 Suggest.Suggestion result = suggest.getSuggestion("autocomplete"); // 获取返回结果

 int maxSuggest = 0;
 Set<String> suggestSet = new HashSet<>();

 // 循环所有的结果
 for (Object term : result.getEntries()) {
     if(term instanceof CompletionSuggestion.Entry){
         CompletionSuggestion.Entry item = (CompletionSuggestion.Entry) term;
         if(item.getOptions().isEmpty()){
             continue;
         }
         for (CompletionSuggestion.Entry.Option option : item.getOptions()) {
             String tip = option.getText().string();
             if(suggestSet.contains(tip)){
                 continue;
             }
             suggestSet.add(tip);   // 保存返回的补全内容
             maxSuggest++;
         }
     }
     if(maxSuggest>5){
         break;
     }

 }
 List<String> suggests = Lists.newArrayList(suggestSet.toArray(new String[]{}));

你可能感兴趣的:(SpringBoot知识体系(四)springboot整合Elasticsearch(4))