在Nest中,如何在编制索引时指定子文档的父文档?

for*_*tyj 3 elasticsearch nest

Product并且Company是多对一的亲子关系:

[ElasticType(Name = "product", IdProperty = "ProductId")]
internal class Product
{
    public int ProductId { get; set; }
    public string Title { get; set; }
}

[ElasticType(Name = "company", IdProperty = "CompanyId")]
public class Company
{
   public int CompanyId { get; set; }
   public string CompanyName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在映射中Product,我做了:

Func<PutMappingDescriptor<Product>, PutMappingDescriptor<Product>> descriptor = m => m
               .MapFromAttributes()
               .AllField(a => a.Enabled(false))
               .SetParent<Company>();
Run Code Online (Sandbox Code Playgroud)

我创建了一个父母和孩子:

var company = new Company {
    CompanyId = 1,
    CompanyName = "XYZ Company"
};

var p2 = new Product{
    ProductId = 2,
    Title = "ABC Product"
};

es.Index(company);
Run Code Online (Sandbox Code Playgroud)

那么问题是,我如何编制索引p2?只有Index方法,我只能这样做es.Index(p2).但是,我在哪里指出p2应该在父母下编入索引company

基本上我想要一个NEST解决方案PUT /myindex/product/2?parent=1.

到目前为止,我找到的最接近的答案是/sf/answers/1676761971/.但答案使用大量插入,如下所示,您.Parent在链接中有一个方法来指定父ID:

var bulkResponse = _client.Bulk(b => b
            .Index<Address>(bd => bd.Object(new Address { Name = "Tel Aviv", Id = 1 }).Index("test"))
            .Index<Person>(bd => bd.Index("test").Object(new Person {Id = 5, Address = 1, Name = "Me"}).Parent(1)));
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 7

如果您正在寻找PUT /myindex/product/2?parent=1请求.您可以通过以下方式在NEST中执行此操作:

var indexResponse = client.Index(p2, descriptor => descriptor
    .Parent(company.CompanyId.ToString()));
Run Code Online (Sandbox Code Playgroud)

它会生成以下对elasticsearch的请求

StatusCode : 400,
Method : PUT,
Url : http : //localhost:9200/indexname/product/2?parent=1,
Request : {
    "productId" : 2,
    "title" : "ABC Product"
}
Run Code Online (Sandbox Code Playgroud)