Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1444 posts 3371 karma points c-trib
    Feb 15, 2012 @ 19:48
    Simon Dingley
    0

    Examine - Searching for Events that fall between a given date

    I have a custom search index for events and if someone enters a date I want to write a query that locates all events that the date falls during. I'm not sure how this is possible as examples I have seen all require you to pass in the 2 dates for the range whereas I want to check that the date passed in falls between the dates defined in eventStartDate and eventEndDate.

    Can anyone offer a starting point?

    Thanks, Simon

  • Nigel Wilson 944 posts 2076 karma points
    Feb 16, 2012 @ 20:31
    Nigel Wilson
    0

    Hi Simon

    Think I have read somewhere that date comparisons (as done within xlst) are not as straight forward / possible using search indexes.

    One possible way is to manipulate all date variables into a number (so today being 17 February 2012 would convert to 20120217).

    This then enables simple number comparisons.

    I may be totally in the wrong ball park with this idea but thought I'd mention it just in case.

    Cheers

    Nigel

  • Simon Dingley 1444 posts 3371 karma points c-trib
    Feb 17, 2012 @ 08:02
    Simon Dingley
    0

    I think I now have the Examine syntax I need to use but getting no results so possibly my date format is wrong?

    searchCriteria.Range("startDate", queryDate.AddDays(-1).ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture),
                  queryDate.AddDays(1).ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture));
  • Ismail Mayat 4511 posts 10061 karma points MVP 2x admin c-trib
    Feb 17, 2012 @ 10:10
    Ismail Mayat
    0

    Simon,

    Open luke and take a look in your index at how the startDate and endDate fields are being stored make your dates are in format yyyyMMddHHmmssfff in lucene.

    Regards

    Ismail

  • Simon Dingley 1444 posts 3371 karma points c-trib
    Feb 17, 2012 @ 10:16
    Simon Dingley
    1

    Thanks Ismail, I have it working now with the following:

    searchCriteria.Range("startDate", queryDate.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture), queryDate.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture))

    It's still not ideal though as what I really want is to return all events that happen to include a specified date e.g. Return all events that will be running on 19th February 2012. The query above just checks to see if the event start date matches the entered date.

  • Ismail Mayat 4511 posts 10061 karma points MVP 2x admin c-trib
    Feb 17, 2012 @ 10:20
    Ismail Mayat
    0

    Simon,

    Then its not a range query you want.  Add a new field into the index call it filterStartDate and inject into that the event start date as yyyyMMdd only.  Then do a search where filterStartDate contains queryDate.ToString("yyyyMMdd") that will then get all events with that start date.

    When working with examine/lucene it is quite normal to fudge the index to get it bend to your will lol!

    Regards

     

    Ismail

  • Simon Dingley 1444 posts 3371 karma points c-trib
    Feb 17, 2012 @ 10:23
    Simon Dingley
    0

    I don't think that will work either, basically I want to return all events where date x (my param) is between startDate and endDate. Does that make sense?

  • Ismail Mayat 4511 posts 10061 karma points MVP 2x admin c-trib
    Feb 17, 2012 @ 10:27
    Ismail Mayat
    0

    Simon,

    Ok here is how i did it although its not on a date field but you could do somethign similar

                        //in lucene you have to mimic > and < with a range as range is TO only see http://lucene.apache.org/java/3_0_0/queryparsersyntax.html#Range Searches

                        if (fieldValueFrom > 0)

                        {

                            queryToBuild = queryToBuild.And().Range("__" + key, (fieldValueFrom).ToString("D6"), 100000.ToString("D6"), true, true);

                        }

                        if (fieldValueTo > 0)

                        {

                            queryToBuild = queryToBuild.And().Range("__" + fieldToName, 0.ToString("D6"), (fieldValueTo).ToString("D6"), true, true);

                        }

    Points to note are that in the value from i have a made up upper limit which is the 10000.ToString("D6") and in the valueTo i have made up lower limit 0.ToString so in your case you could have upper blag date set to 100yrs from now and lower limit set to -100 that will then constrain the events returned.

    Regards

     

    Ismail

  • Halima Koundi 8 posts 79 karma points
    Mar 10, 2016 @ 15:52
    Halima Koundi
    1

    Hi, Thank you Ismail your answer helped me to figure out how to build my search on dates. I am posting my solution here in case it can help .

    I used Unix dates to store the date field when indexing and then transform the upper and lower bounds to Unix dates and perform the Range query like the following :

    var movein = ToUnixTime(DateTime.ParseExact(paramsObj.data.movein, "M/yy", CultureInfo.InvariantCulture));
    query = query.And().Range("unixAvailabilityDate", movein.ToString("D12"), ToUnixTime(DateTime.Now.AddYears(1)).ToString("D12"));
    

    And the Indexing event handler :

    e.Fields.Add("unixAvailabilityDate", ToUnixTime(DateTime.Parse(availabilityDate)).ToString("D12"));
    

    The to Unix method :

        public static long ToUnixTime(DateTime dateT)
    {
        dynamic epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return Convert.ToInt64((dateT - epoch).TotalSeconds);
    }
    

    Cheers

  • Alastair Todd 44 posts 142 karma points
    Jun 08, 2016 @ 07:40
    Alastair Todd
    1

    Late to the party but I find old posts VERY misleading. Here's how to EASILY search for a date range.

    The big one for me was telling Examine that a field is a date (!?DRY) since its incapable apparently of working that out itself.

    In the IndexSet config:

     <IndexUserFields>
          <add Name="postDate" Type="DateTime" /> 
    

    I used Luke to check that without the type those fields are stored in a strange unsearchable format.

    This code is useful to run now

    ExamineManager.Instance.IndexProviderCollection["PostsIndexer"].RebuildIndex();
    

    Then the actual searching:

    var searcher = ExamineManager.Instance.SearchProviderCollection["PostsSearcher"];
    var postsCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
    var postDateFilter = postsCriteria.Range("postDate", startDate, endDate).Compile();
    
    var results = searcher.Search(postDateFilter);
    
  • Manish 373 posts 930 karma points
    Feb 21, 2017 @ 13:38
    Manish
    0

    Hi @Alastair Todd

    I am using the same thing that you are using but it is returning noting to me.

    Here is my code

      var Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"];
    
    
            var searchCriteria = Searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
            var startDate = DateTime.Now;
            var endDate = DateTime.Now.AddMonths(-4);
    
            var postDateFilter = searchCriteria.Range("updateDate", startDate, endDate).Compile();
    
            var results = Searcher.Search(postDateFilter);
    

    What i am missing here?

    Thanks Manish

  • Ismail Mayat 4511 posts 10061 karma points MVP 2x admin c-trib
    Feb 23, 2017 @ 09:13
    Ismail Mayat
    0

    Manish,

    Can you step through the code then just before you do Searcher.Search can you do searchCriteria.ToString() this will give you the generated lucene query can you post that back here.

    I suspect that there is issue with date formats. The date that is stored in the index is stored as string. The query generated will also create string format of the date. If there is inconsistancy between the 2 formats the search will return no results.

    You may need to update the date formats as they go into the index. See https://our.umbraco.org/forum/developers/extending-umbraco/25975-Examine-range-createDate and http://thecogworks.co.uk/blog/2013/04/11/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux

    Regards

    Ismail

Please Sign in or register to post replies

Write your reply to:

Draft