首页 > 解决方案 > 未找到 Elasticsearch NEST Suggester 解析器

问题描述

我正在尝试利用 Elasticsearch 的“ Suggester ”功能。

使用短语、术语或完成,我总是得到以下错误变化。

unable to parse SuggestionBuilder with name [COMPLETION]: parser not found"
unable to parse SuggestionBuilder with name [TERM]: parser not found"
unable to parse SuggestionBuilder with name [PHRASE]: parser not found"

我尝试了多个 6.x NEST 版本,它们都有相同的问题。升级到 7.0alpha1 确实会改变错误,但似乎会导致无数其他问题,我宁愿不在生产中使用 alpha 库。

我目前正在关注本教程并将其应用于我现有的代码中:https ://github.com/elastic/elasticsearch-net-example/tree/6.x-codecomplete-netcore#part-6-suggestions

目前使用 NEST 6.1

模型:

public class SearchResult {

      public SearchResult()
                {
                    TitleSuggest = new CompletionField {Input = new List<string>(Title.Split(' '))};
                }
                public CompletionField TitleSuggest { get; set; }
        //etc
        }

索引方法:

public async Task<IActionResult> CreateIndex()
        {
            await _searchClient.CreateIndexAsync(SearchIndexName, indexSelector =>
                indexSelector
                    .Mappings(mappingsDescriptor =>
                        mappingsDescriptor.Map<Models.SearchResult>(y => y.AutoMap().Properties(pr=>pr.Completion(c => c.Name(p => p.TitleSuggest)
                        ))))

建议方法:

public async Task<ISearchResponse<SearchResult>> Suggest(string keyword)
        {
return await _searchClient.SearchAsync<SearchResult>(
                s =>
                        s.Suggest(ss => ss
                            .Completion("title", cs => cs
                                .Field(f => f.TitleSuggest)
                                .Prefix(keyword)
                                .Fuzzy(f => f
                                    .Fuzziness(Fuzziness.Auto)
                                )
                                .Size(5))
}

我很难破译错误。似乎 NEST 库缺少 Suggester 解析器?任何帮助都会很棒,谢谢!

标签: c#elasticsearchnest

解决方案


试试这个:

var searchResponse = await _searchClient.SearchAsync<SearchResult>(s => s
                .Index(ConfigurationManager.AppSettings.Get("index"))
                .Type(ConfigurationManager.AppSettings.Get("indextype"))
                .Suggest(su => su
                    .Completion("suggest", cs => cs
                        .Size(20)
                        .Field(f => f.TitleSuggest)
                        .Fuzzy(f => f
                                .Fuzziness(Fuzziness.Auto))
                        .Size(5))));

推荐阅读