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)));
答案 0 :(得分:7)
如果您正在寻找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"
}