Ted*_*erg 5 elasticsearch nest
我想用NEST附加多个bool过滤器,但我不能(实际上)在一个语句中执行它,因为我想根据不同的条件构建一组Bool过滤器.
像这样的伪代码:
// Append a filter
searchDescriptor.Filter(f => f.Bool(b => b.Must(m => m.Term(i => i.SomeProperty, "SomeValue"))));
if (UserId.HasValue)
{
// Optionally append another filter (AND condition with the first filter)
searchDescriptor.Filter(f => f.Bool(b => b.Must(m => m.Term(i => i.AnotherProperty, "MyOtherValue"))));
}
var result = Client.Search(searchDescriptor);
Run Code Online (Sandbox Code Playgroud)
现在看来,当附加第二个可选过滤器时,它基本上取代了第一个过滤器.
我确定我在语法上遗漏了一些东西,但我无法弄明白,过滤器DSL上的NEST文档有点薄.:)
Mar*_*man 12
编写查询的部分也非常适用于过滤器:http: //nest.azurewebsites.net/nest/writing-queries.html
您最终得到的解决方案不太理想,因为您将bool过滤器包装在and具有非常不同的缓存机制的过滤器内(并且过滤器不使用缓存的位集,因此在大多数情况下执行比常规bool过滤器更差).
强烈建议阅读http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/,因为它很好地解释了和/或/不是过滤器与bool过滤器之间的区别.
你可以像这样重写这个:
Client.Search(s=>s
.Filter(f=> {
BaseFilter ff = f.Term(i => i.MyProperty, "SomeValue");
if (UserId.HasValue)
ff &= f.Term(i => i.AnotherProperty, "AnotherValue");
return ff;
})
);
Run Code Online (Sandbox Code Playgroud)
如果第二个术语是使用UserId进行搜索,您可以利用NEST的优势 conditionless queries
Client.Search(s=>s
.Filter(f=>
f.Term(i => i.MyProperty, "SomeValue")
&& f.Term(i => i.AnotherProperty, UserId)
)
);
Run Code Online (Sandbox Code Playgroud)
如果UserId为null,则术语查询将不会作为查询的一部分生成,在这种情况下,嵌套甚至不会将单个剩余术语包装在bool过滤器中,而只是将其作为简单术语过滤器发送.
| 归档时间: |
|
| 查看次数: |
10439 次 |
| 最近记录: |