java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Data Elasticsearch单词纠错补全

Spring Data Elasticsearch 5.x实现单词纠错和自动补全

作者:TinyThing

这篇文章主要为大家介绍了Spring Data Elasticsearch 5.x实现单词纠错和自动补全示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

单词纠正功能

        //单词纠正
        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);

自动补全

自动补全首先要有Complition字段

    /**
     * 自动补全功能
     */
    @CompletionField(analyzer = IK_ANALYSER, searchAnalyzer = IK_ANALYSER)
    private Completion completionName;

自动补全查询

        //自动补全
        String completionField = "completionName";
        CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion()
                .field(completionField)
                .skipDuplicates(true)
                .size(size)
                .build();
        FieldSuggester cs = new FieldSuggester.Builder()
                .text(name)
                .completion(completionSuggester)
                .build();

以上就是Spring Data Elasticsearch 5.x实现单词纠错和自动补全的详细内容,更多关于Spring Data Elasticsearch单词纠错补全的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文