用最优秀的Lucence 查询构建器 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();
输出:
+((+__NodeTypAlias:Event +eventDate:[42920 TO 79444]) (-__NodeTypAlias:Event))
并将其传递给 Umbraco 检查:
ISearchCriteria criteria = searcher.CreateSearchCriteria();
var filter = criteria.RawQuery(q);
var results = searcher.Search(filter, MaxResults);
运算符翻译为“ Should
OR”,同时Must
翻译为 AND(Lucene 命名法)。
作为参考,您还需要在收集节点数据时将新的可比较日期字段写入索引:
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;
}
}