【发布时间】:2015-04-17 04:30:07
【问题描述】:
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; }
}
在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