我有一个方法,将查询作为参数,如:
public ISearchResponse<Object> SearchComponent(SearchDescriptor<Object> query)
{
...
}
在此查询中,我想添加一个源过滤器,如:
public ISearchResponse<Object> SearchComponent(SearchDescriptor<Object> query)
{
query = query.Source(sf =>
sf.Exclude(e => e
.Field("SomeField")
));
...
}
但是如果查询已经有源过滤器会发生什么?这个过滤器会覆盖那个过滤器吗?如何更新现有查询源过滤器?
答案 0 :(得分:0)
这似乎有效,但它并不是最美丽的解决方案。任何人都可以提出更好的选择吗?
public ISearchResponse<Object> SearchComponent(ISearchRequest query)
{
var excludeFields = new List<string>();
excludeFields.Add("SomeField");
if (query.Source == null)
{
query.Source = new SourceFilter {Include = "*", Exclude = excludeFields.ToArray()};
}
else if (query.Source.Exclude == null)
{
query.Source.Exclude = excludeFields.ToArray();
}
else
{
query.Source.Exclude.And(excludeFields.ToArray());
}
...
}
答案 1 :(得分:0)
You are using an older version of Nest than what I have and this has changed a bit, but I figure you can do something along these lines:
var exclude = query.Source?.Exclude;
query.Source = new SourceFilter() { Excludes = (exclude ?? new Field[0]).Union(moreFields) };