我有一個函數在基於布爾型標誌的時間間隔內搜索文檔,NEST查詢將使用不同的LessThanOrEqual in if statment and 在else語句中使用的是。如何在NEST C中編寫動態彈性搜索查詢#
public IEnumerable<Book> GetBooks(DateTime startTime, DateTime endTime, string author, bool includeEnd)
{
var readRecords;
if (includeEnd){
readRecords = elastic.Search<Book>(s => s.Index(IndexName)
.Query(q => q.Term(book => book.Author, author) &&
q.DateRange(i => i.Field(book => book.Author)
.GreaterThanOrEquals(startTime)
.LessThanOrEquals(endTime)))).Documents.ToArray();
}
else{
readRecords = elastic.Search<Book>(s => s.Index(IndexName)
.Query(q => q.Term(book => book.Author, author) &&
q.DateRange(i => i.Field(book => book.Author)
.GreaterThanOrEquals(startTime)
.LessThan(endTime)))).Documents.ToArray();
}
return readRecords;
}
如何使這個NEST查詢動態使用布爾標誌「includeEnd」,以便我不需要使用if語句?
請注意,在if語句中,日期範圍查詢包含結束時間(通過在NEST查詢中使用「LessThanOrEqual」),否則語句通過在NEST查詢中僅使用LessThan來排除結束時間。 –