本文介绍spring-data-elasticsearch:jar:4.0.0.RELEASE版本completion suggest的使用。
主要用到CompletionSuggestionBuilder
,SuggestBuilder
这两个类
同QueryBuilders
一样suggest也有一个SuggestBuilders
用于构建term
,phrase
,completion
三种类型的suggest构造器
CompletionSuggestionBuilder textBuilder =
SuggestBuilders.completionSuggestion("goodsName.suggest")
而对于Completion的Context Suggest,我们就要用到CategoryQueryContext
和GeoQueryContext
如下:
CategoryQueryContext categoryQueryContext =
CategoryQueryContext.builder().setCategory(cityCode).build();
注意,这里的cityCode是从前端传递过来的一个动态参数,用于限定城市。
接下来将其封装到Map中
Map<String, List<? extends ToXContent>> contexts = new HashMap<>();
List<CategoryQueryContext> list = new ArrayList<>();
list.add(categoryQueryContext);
contexts.put("city", list); // 这里要和mapping中配置的name保持一致
最后我们把拼装好的contexts放到CompletionSuggestionBuilder
中
textBuilder.contexts(contexts);
完整代码如下:
public List<String> suggest(String keyword, String cityCode) {
HashSet<String> returnSet = new LinkedHashSet<>(); // 用于存储查询到的结果
// 创建CompletionSuggestionBuilder
CompletionSuggestionBuilder textBuilder = SuggestBuilders.completionSuggestion("goodsName.suggest") // 指定字段名
.size(10) // 设定返回数量
.skipDuplicates(true) // 去重
// 配置context
CategoryQueryContext categoryQueryContext = CategoryQueryContext.builder().setCategory(cityCode).build();
Map<String, List<? extends ToXContent>> contexts = new HashMap<>();
List<CategoryQueryContext> list = new ArrayList<>();
list.add(categoryQueryContext);
contexts.put("city", list);
textBuilder.contexts(contexts); // 设置contexts
// 创建suggestBuilder并将completionBuilder添加进去
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_text", textBuilder)
.setGlobalText(keyword);
// 执行请求
Suggest suggest = elasticsearchRestTemplate.suggest(suggestBuilder, elasticsearchRestTemplate.getIndexCoordinatesFor(Goods.class)).getSuggest();
// 取出结果
Suggest.Suggestion<Entry<Option>> textSuggestion = suggest.getSuggestion("suggest_text");
for (Entry<Option> entry : textSuggestion.getEntries()) {
List<Option> options = entry.getOptions();
for (Suggest.Suggestion.Entry.Option option : options) {
returnSet.add(option.getText().toString());
}
}
return new ArrayList<>(returnSet);
}
引入的相关类
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.elasticsearch.search.suggest.completion.context.CategoryQueryContext;
Goods.java
@Document(indexName = "goods", shards = 1, replicas = 0)
@Mapping(mappingPath = "/mappings/goods.json")
@Data
public class Goods {
@Id
private Long goodsId;
private String goodsName;
private String cityCode;
}
由于此版本中FieldType并没有提供Completion类型,使用@MultiField无法达到目的,因此用@Mapping方式进行配置
goods.json
{
"properties": {
"goodsName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"fields": {
"suggest": {
"type": "completion",
"analyzer": "standard",
"contexts": [
{
"name": "city",
"type": "category",
"path": "cityCode"
}
]
}
}
}
}
}
本文重点介绍4.x版本Completion Suggest的使用,如何做到更精确的查找,网上有很多,就不在这里介绍了。
如果您觉得有帮助,还望留下一个赞呗。
网友评论