I am trying to use NEST to create search query dynamically based on user's input.
I have a Filter class with user inputs:
Public class ProductFilter
{
public string Name { get; set; }
public DateTime? PublishDateFrom { get; set; }
}
But for the properties like Name and PublishDateFrom , they could be empty or null if a user doesn't specify.
So when doing search using NEST like the following code:
var response1 = await client.SearchAsync<ProjectDocument>(s => s
.Index(Indices.Parse("products"))
.From(0)
.Size(10000)
.Type("product")
.Query(q => q
.....
)
);
I want to cover the case when Name or PublishDateFrom is empty or null in one search all.
Right now if I use:
...
.Query(q => q
.Term(p => p.Name, filter.Name)
When filter.Name is empty or null, then the search result is empty. I want to something like: if the filter.Name is empty or null, the Term Query related to Name is not executed or included in the SearchAsync call. When both Name and PublishDateFrom are empty or null, then the query should be using .MatchAll().
I am trying to use Bool Query, but can't handle this case either.
Is there any good way to solve this problem?