【发布时间】:2018-12-19 15:28:36
【问题描述】:
我在弹性中有以下文档类型:
public class ProductDto
{
public Int64 Id { get; set; }
public String Title{ get; set; }
public bool Visible { get; set; }
public IList<ProductSupplierDto> ProductSuppliers { get; set; }
}
public class ProductSupplierDto
{
public Int64 Id { get; set; }
public String Title{ get; set; }
public bool Enabled { get; set; }
}
如何使用 Nest libray 编写以下 linq 查询:
var products = db.products.where(p=> p.Visible
&& p.ProductSuppliers.Any(ps=>ps.Enabled)
).ToList();
在嵌套库中我有以下查询:
var baseQuery = Query<ProductDto>.Term(qt => qt.Field(f =>
f.Visible).Value(true));
如何在 baseQuery 中添加 productsuppliers 过滤器?
我用这个方法来创建索引:
private async Task CreateIndexIfItDoesntExist<T>(string index) where T: class
{
if (!this.client.IndexExists(index).Exists)
{
var indexDescriptor = new CreateIndexDescriptor(index)
.Settings(x => x.NumberOfReplicas(0))
.Mappings(mappings => mappings
.Map<T>(m => m.AutoMap()));
await this.client.CreateIndexAsync(index, i => indexDescriptor);
// Max out the result window so you can have pagination for >100 pages
await this.client.UpdateIndexSettingsAsync(index, ixs => ixs
.IndexSettings(s => s
.Setting("max_result_window", int.MaxValue)));
}
}
并像这样调用:
await CreateIndexIfItDoesntExist<ProductDto>("products");
索引数据的方法:
private async Task<IndexResult> IndexDocuments<T>(T[] datas, string index) where T:class
{
int batchSize = 1000; // magic
int totalBatches = (int)Math.Ceiling((double)datas.Length / batchSize);
for (int i = 0; i < totalBatches; i++)
{
var response = await this.client.IndexManyAsync(datas.Skip(i * batchSize).Take(batchSize), index);
if (!response.IsValid)
{
return new IndexResult
{
IsValid = false,
ErrorReason = response.ServerError?.Error?.Reason,
Exception = response.OriginalException
};
}
else
{
Debug.WriteLine($"Successfully indexed batch {i + 1}");
}
}
return new IndexResult
{
IsValid = true
};
}
【问题讨论】:
-
请您也提供
ProductDto的索引映射,因为它需要正确回答 -
@RussCam 我的问题已更新。
-
感谢@Iraj - 只需要索引中存在的映射 JSON,但提供的代码表明
标签: c# .net linq elasticsearch nest