I have an interesting challenge, which I think there is an easy answer to.
I know that NEST filters work correctly when syntactically you do something like this:
var andFilter = FilterFactory.AndFilter(
FilterFactory.TermFilter("name.first", "shay1"),
FilterFactory.TermFilter("name.first", "shay4")
);
My base services should allow an the caller to pass in some sort of enumerable list of items to filter.
I’d basically like to be able programmatically achieve something like this (filters is passed into the method):
var andFilter = new FilterDescriptor();
foreach (var filter in filters)
{
andFilter = filter concatenated to andFilter
}
In other words if I passed in an array of { {“first.name”, “joe”}, {“first.name”, “jim”}, {“first.name”, “frank”}} I would like to produce the equivalent of
var andFilter = FilterFactory.AndFilter(
FilterFactory.TermFilter("name.first", "joe"),
FilterFactory.TermFilter("name.first", "joe"),
FilterFactory.TermFilter("name.first", "frank")
);
Using the lambda based DSL you can do the following:
Which i think reads a bit better 🙂
Nest now also supports
conditionlessqueries so if anytp.SearchValuesisnull,emptyorall empty stringsortp.SearchFieldisnull or emptyit will skip that terms/prefix query.You can revert this behavior easily though:
which will throw a
DslExceptionif an empty query is generated.As a last note
client.Search()will return aQueryResult<dynamic>if you can strongly type your documents so can do aclient.Search<MyDocument>().