Corey Burnett
Corey Burnett

Reputation: 7340

In Sitecore how to run a date range query using Lucene?

I need to be able to run a date range query in Sitecore using Lucene and the newest Sitecore.Search framework.

Does anyone know how to do this?

I haven't been able to find a good example. I have Googled a bit and found bits and pieces here and there.

Upvotes: 1

Views: 1265

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27132

You can find similar question here: How to do a Lucene search with Sitecore item with specific date?

Just treat those fields as a standard string fields and execute Range Query starting from e.g. 20130418T000000 including this value and ending on 20130819T000000

This is a method which works for me:

    private static void CreateCreatedDatesQueryPart(BooleanQuery query, string createdFrom, string createdTo)
    {
        if (!String.IsNullOrEmpty(createdFrom) || !String.IsNullOrEmpty(createdTo))
        {
            if (String.IsNullOrEmpty(createdFrom))
            {
                createdFrom = "000000t000000";
            }
            if (String.IsNullOrEmpty(createdTo))
            {
                createdTo = "999999t999999";
            }
            query.Add(new TermRangeQuery("__created", createdFrom, createdTo, true, true), BooleanClause.Occur.MUST);
        }
    }

Here you can find more information about solving Sitecore and Lucene problems.

Upvotes: 1

Related Questions