简体   繁体   English

ElasticSearch 5.x Context Suggester NEST .Net

[英]ElasticSearch 5.x Context Suggester NEST .Net

I'm trying to create an Index with a context suggester with Nest 5.0 on ElasticSearch 5.1.2. 我正在尝试使用ElasticSearch 5.1.2上的Nest 5.0创建一个带有上下文建议的索引。

Currently, I can create the mapping: 目前,我可以创建映射:

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

But in the POCO class i dont know what object type must be Suggest Property marked with ????? 但在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; }
}

In NEST 5.0 CompletionField Property has been removed (That was the method to do context suggester on elasticsearch 2.X) 在NEST 5.0中, CompletionField属性已被删除(这是在elasticsearch 2.X上执行上下文建议的方法)

Please, can anyone provide an example about how to do it? 请问,任何人都可以举例说明如何做到这一点吗?

The documentation is all about querying. 文档都是关于查询的。 Suggester NEST 建议NEST

Thanks. 谢谢。

The Completion and Context Suggester are able to return the _source in the response in 5.x+, so no longer require a payload. Completion和Context Suggester能够在5.x +的响应中返回_source ,因此不再需要有效负载。 Because of this, the type in NEST 5.x is now CompletionField , as opposed to CompletionField<TPayload> in NEST 2.x, where TPayload is the payload type. 因此,NEST 5.x中的类型现在是CompletionField ,而不是NEST 2.x中的CompletionField<TPayload> ,其中TPayload是有效负载类型。

Here's an example with NEST 5.x to get you up and running 这是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"];
}

The searchResponse returns 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#"
              ]
            }
          }
        ]
      }
    ]
  }
}

suggesting documents with ids "1" and "3" . 用ids "1""3"建议文件。 You can also use Source Filtering to only return the fields that you are interested in from _source . 您还可以使用源过滤仅从_source返回您感兴趣的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM