Umbraco检查,嵌套多个布尔运算符

时间:2017-07-04 20:50:27

标签: umbraco examine

我正在尝试将这些过滤器添加到已经合理的复杂Umbraco Examine查询中,并且已经看到您无法将API与原始lucene查询混合,因此整个过程可能必须以原始方式完成,我将我试图避免,因为它的查询构建器有很多维度。

API可以做到这一点吗?我看到GroupedOr/And,但我不知道它是如何削减它的,因为它们是独占/包含sql" In"类型查询。

AND ((_nodeTypAlias: 'Event' AND eventDate:(0xx TO 0xx)) OR (NOT _nodeTypAlias: 'Event'))
AND ((_nodeTypAlias: 'Article' AND postDate:(0xx TO 0xx)) OR (NOT _nodeTypAlias: 'Article'))

1 个答案:

答案 0 :(得分:1)

使用最优秀的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();

输出:

  

+((+ __ 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;
        }
    }