一尘不染

ElasticSearch 5.x上下文建议器NEST .Net

elasticsearch

我正在尝试在ElasticSearch 5.1.2上的Nest 5.0中创建带有上下文建议器的索引。

目前,我可以创建映射:

elasticClient.MapAsync<EO_CategoryAutocomplete>(m => m
                .Properties(p => p
                    .Completion(c => c
                        .Contexts(ctx => ctx
                            .Category(csug => csug
                                .Name("lang")
                                .Path("l")
                            )
                            .Category(csug => csug
                                .Name("type")
                                .Path("t")
                            )
                            .Category(csug => csug
                                .Name("home")
                                .Path("h")
                            )
                        )
                        .Name(n => n.Suggest)
                    )
                )
            );

但在POCO类我不知道对象类型必须是什么 本稿 标明物业 ?????

public class EO_CategoryAutocomplete
{
    public string Id { get; set; }
    public ????? Suggest { get; set; }
}

public class EO_CategoryAC
{
    public int Id { get; set; }
    public string Name { get; set; }
}

在NEST 5.0中, CompletionField 属性已被删除(这是在Elasticsearch 2.X上执行上下文提示的方法)

请,任何人都可以提供有关该方法的示例吗?

该文档全部关于查询。推荐人NEST

谢谢。


阅读 256

收藏
2020-06-22

共1个答案

一尘不染

完成和上下文建议程序可以_source在5.x +的响应中返回,因此不再需要有效负载。因此,NEST
5.x中的类型现在是CompletionField,而不是CompletionField<TPayload>NEST
2.x中TPayload的有效负载类型。

这是NEST 5.x的示例,可帮助您启动和运行

public class EO_CategoryAutocomplete
{
    public string Id { get; set; }
    public IEnumerable<string> L { get; set; }
    public CompletionField Suggest { get; set; }
}

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var connectionSettings = new ConnectionSettings(pool)
            .DefaultIndex("autocomplete");

    var client = new ElasticClient(connectionSettings);

    if (client.IndexExists("autocomplete").Exists)
        client.DeleteIndex("autocomplete");

    client.CreateIndex("autocomplete", ci => ci
        .Mappings(m => m
            .Map<EO_CategoryAutocomplete>(mm => mm
                .AutoMap()
                .Properties(p => p
                    .Completion(c => c
                        .Contexts(ctx => ctx
                            .Category(csug => csug
                                .Name("lang")
                                .Path("l")
                            )
                            .Category(csug => csug
                                .Name("type")
                                .Path("t")
                            )
                            .Category(csug => csug
                                .Name("home")
                                .Path("h")
                            )
                        )
                        .Name(n => n.Suggest)
                    )
                )
            )
        )
    );

    client.IndexMany(new[] {
        new EO_CategoryAutocomplete 
        {
            Id = "1",
            Suggest = new CompletionField
            {
                Input = new [] { "async", "await" },
                // explicitly pass a context for lang
                Contexts = new Dictionary<string, IEnumerable<string>>
                {
                    { "lang", new [] { "c#" } }
                }
            }
        },
        new EO_CategoryAutocomplete
        {
            Id = "2",
            Suggest = new CompletionField
            {
                Input = new [] { "async", "await" },
                // explicitly pass a context for lang
                Contexts = new Dictionary<string, IEnumerable<string>>
                {
                    { "lang", new [] { "javascript" } }
                }
            }
        },
        new EO_CategoryAutocomplete
        {
            Id = "3",
            // let completion field mapping extract lang from the path l
            L = new [] { "typescript" },
            Suggest = new CompletionField
            {
                Input = new [] { "async", "await" },
            }
        }
    }, "autocomplete");

    client.Refresh("autocomplete");

    var searchResponse = client.Search<EO_CategoryAutocomplete>(s => s
        .Suggest(su => su
            .Completion("categories", cs => cs
                .Field(f => f.Suggest)
                .Prefix("as")
                .Contexts(co => co
                    .Context("lang", 
                        cd => cd.Context("c#"), 
                        cd => cd.Context("typescript"))
                )
            )
        )
    );

    // do something with suggestions
    var categorySuggestions = searchResponse.Suggest["categories"];
}

searchResponse收益

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "suggest" : {
    "categories" : [
      {
        "text" : "as",
        "offset" : 0,
        "length" : 2,
        "options" : [
          {
            "text" : "async",
            "_index" : "autocomplete",
            "_type" : "eo_categoryautocomplete",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "id" : "3",
              "l" : [
                "typescript"
              ],
              "suggest" : {
                "input" : [
                  "async",
                  "await"
                ]
              }
            },
            "contexts" : {
              "lang" : [
                "typescript"
              ]
            }
          },
          {
            "text" : "async",
            "_index" : "autocomplete",
            "_type" : "eo_categoryautocomplete",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "id" : "1",
              "suggest" : {
                "input" : [
                  "async",
                  "await"
                ],
                "contexts" : {
                  "lang" : [
                    "c#"
                  ]
                }
              }
            },
            "contexts" : {
              "lang" : [
                "c#"
              ]
            }
          }
        ]
      }
    ]
  }
}

建议使用ID "1"和的文档"3"。您还可以使用“
源过滤”仅从中返回您感兴趣的字段_source

2020-06-22