找不到Elasticsearch NEST建议者解析器

时间:2019-05-06 14:36:10

标签: c# elasticsearch nest

我正在尝试利用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解析器? 任何帮助都会很棒,谢谢!

2 个答案:

答案 0 :(得分:0)

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))));

希望它能奏效,让我知道您是否仍然遇到任何问题。谢谢

答案 1 :(得分:0)

作为后续活动,@ RussCam回答了我的问题here

我的ConnectionSetting(DefaultFieldNameInferrer)大写了我的建议

private IElasticClient ElasticClient(IConfiguration _config, string defaultIndex)
{
  var settings = new ConnectionSettings(new Uri(_config.GetSection("Search:Url").Value))
    .BasicAuthentication(_config.GetSection("Search:User").Value, _config.GetSection("Search:Password").Value)
    .DefaultIndex(_config.GetSection(defaultIndex).Value);
  //settings.DefaultFieldNameInferrer(p => p.ToUpper(CultureInfo.CurrentCulture));

  //Enable ElasticSearch Debugging
  settings.PrettyJson().DisableDirectStreaming();

  return new ElasticClient(settings);
}