【问题标题】:In Nest, how do I specify a child document's parent document while indexing?在 Nest 中,如何在索引时指定子文档的父文档?
【发布时间】:2015-04-17 04:30:07
【问题描述】:

ProductCompany 是多对一的子父关系:

[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; }
}

Product的映射中,我做到了:

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

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

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

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

es.Index(company);

那么问题来了,我如何索引p2?只有Index方法,我只能做es.Index(p2)。但是我在哪里指出p2 应该在父company 下被索引?

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

到目前为止我找到的最接近的答案是https://stackoverflow.com/a/23953742/1124270。但答案使用如下的批量插入,其中您在链接中有一个 .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)));

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

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

    var indexResponse = client.Index(p2, descriptor => descriptor
        .Parent(company.CompanyId.ToString()));
    

    生成以下对 elasticsearch 的请求

    StatusCode : 400,
    Method : PUT,
    Url : http : //localhost:9200/indexname/product/2?parent=1,
    Request : {
        "productId" : 2,
        "title" : "ABC Product"
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 2018-07-19
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 2020-09-11
      • 1970-01-01
      相关资源
      最近更新 更多