Achieved it with the most excellent Lucence query builder API
var q = new QueryBuilder()
.Must
.MatchSubQuery(and => and
.Should
.MatchSubQuery(qq => qq
.Must
.MatchTerm("__NodeTypAlias", "Event")
.MatchRange(
"comparableEventDate",
DateTime.Now.ToString("yyyyMMddHHmm00000"),
DateTime.Now.AddYears(100).ToString("yyyyMMddHHmm00000"))
.MatchSubQuery(qq => qq
.MustNot
.MatchTerm("__NodeTypAlias", "Event"))
)
.Query
.ToString();
Outputs:
+((+__NodeTypAlias:Event +eventDate:[42920 TO 79444]) (-__NodeTypAlias:Event))
and pass that into Umbraco Examine:
ISearchCriteria criteria = searcher.CreateSearchCriteria();
var filter = criteria.RawQuery(q);
var results = searcher.Search(filter, MaxResults);
The Should
operator translates to "OR" whilst Must
translates to AND (Lucene nomenclature).
For reference, you need also to write new comparable date fields to the index in gathering node data:
private void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper umbraco)
{
if (e.IndexType != IndexTypes.Content) return;
try
{
var content = new Node(e.NodeId);
if (e.Fields.ContainsKey("postDate"))
e.Fields.Add("comparablePostDate", DateTime.Parse(e.Fields["postDate"]).ToString("yyyyMMddHHmm00000"));
if (e.Fields.ContainsKey("eventDate"))
e.Fields.Add("comparableEventDate", DateTime.Parse(e.Fields["eventDate"]).ToString("yyyyMMddHHmm00000"));
AddAuthor(e.Fields, content);
}
catch (Exception ex)
{
LogHelper.Error(this.GetType(), "Error in Umbers custom ExamineEvents_GatheringNodeData: ", ex);
//does nowt!
throw;
}
}