0
votes

I am trying to add these filters to an already reasonable complex Umbraco Examine query, and have seen that you can't mix the API with raw lucene query, so the whole thing might have to be done raw, which I was trying to avoid since its a query builder that has quite a few dimensions.

Is this sort of thing possible with the API? I saw the GroupedOr/And but I don't see how that cuts it since those are exclusive/inclusive sql "In" type queries.

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

1 Answers

1
votes

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;
        }
    }