小编典典

使用Nest Client加载Elasticsearch中的完成字段

elasticsearch

我想使用Nest将完成建议字段填充到索引中。阅读了有关实现补全字段的ElasticSearch博客文章后,我看到您可以具有以下属性:

  • 输入数组
  • 单输出
  • 重量
  • 有效载荷

我假设要将这些数据加载到索引中,我需要在包含上述字段的搜索对象上包括一个实体吗?


阅读 356

收藏
2020-06-22

共1个答案

小编典典

我终于可以通过创建几个类并遵循FluentMappingFullExample单元测试(特别是以下部分)来加载完成字段:

                    .Completion(s=>s
                    .Name(p=>p.Name.Suffix("completion"))
                    .IndexAnalyzer("standard")
                    .SearchAnalyzer("standard")
                    .MaxInputLength(20)
                    .Payloads()
                    .PreservePositionIncrements()
                    .PreserveSeparators()
                )

对于我的搜索类型实体,我创建了一个建议字段,并将其设置为CompletionField类型。

 public class CompletionField
{
    public CompletionField()
    {
        Input = new List<string>();
    }

    public List<string> Input { get; set; }
    //public string Output { get; set; }
    public int Weight { get; set; }
    public Payload Payload { get; set; }
}

public class Payload
{
    public int ID { get; set; }
}

使用dapper从数据库加载实体后,然后遍历结果,并在完成字段中加载所需的适当输入。然后,我能够成功调用建议API并查询该数据。我希望这可以帮助其他人。

2020-06-22