1. 单词纠正功能
//单词纠正
PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase()
.field("name")
.size(1)
.build();
FieldSuggester ps = new FieldSuggester.Builder()
.text(name)
.phrase(phraseSuggester)
.build();
//查询
Suggester nameSuggester = Suggester.of(s -> s.suggesters("phrase", ps));
NativeQuery nativeQuery = NativeQuery.builder()
.withSuggester(nameSuggester)
.withTrackTotalHits(true)
.build();
log.debug("es suggester: {}", nativeQuery.getSuggester());
//这里需要重试,因为es有时候会报连接异常
SearchHits<EsPojo> hits = es.search(nativeQuery, EsPojo.class);
2. 自动补全
2.1 自动补全首先要有Complition字段
/**
* 自动补全功能
*/
@CompletionField(analyzer = IK_ANALYSER, searchAnalyzer = IK_ANALYSER)
private Completion completionName;
2.2 自动补全查询
//自动补全
String completionField = "completionName";
CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion()
.field(completionField)
.skipDuplicates(true)
.size(size)
.build();
FieldSuggester cs = new FieldSuggester.Builder()
.text(name)
.completion(completionSuggester)
.build();