user6408649
user6408649

Reputation: 1305

How dynamically add query using ElasticSearch .NET client?

The following query calculates the count of documents in index:

public async Task GetCount(DateTimeOffset? startDate, DateTimeOffset? endDate, string eventName)
{
    var count = await _elasticClient.CountAsync<OemCatalogModel>(c => c.Index(indexName)
                .Query(q => q.DateRange(p => p.Field(t => t.Timestamp).Format("epoch_second")
                             .GreaterThanOrEquals(startDate)
                             .LessThanOrEquals(endDate)
                             ))
                .Query(q => q.Match(m => m.Field(f => f.Event).Query($"{eventName}")))
                );
}

Problem is CountAsync ignore Query with DateRange and return count of all documents in index when eventName is empty.

How to dynamically add the following filter

Query(q => q.Match(m => m.Field(f => f.Event).Query(eventName))

if eventName is not empty string? I think that it can implement using `Func<long, ...> but don't understand how...

Upvotes: 0

Views: 167

Answers (0)

Related Questions