Copied to clipboard

Flag this post as spam?

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


  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 07:51
    pantryfight
    0

    Strange Examine query Range behaviour

    Hi folks,

    I have a news article document type which has an dateAuthored datepicker property.

    I'm experiencing some strange behaviour with the Range filter in Examine. I'm building up a query which searches my news articles. I'm trying to filter by year by using the following code:

    int year = 2014;    
    DateTime yearDate = new DateTime(year, 1, 1);
    query = query.And().Range("dateAuthored", yearDate, yearDate.AddYears(1));
    

    What I have found is that if I set "year" to 2014, all of the 2015 news articles are returned. If "year" is 2013, all 2014 items are returned, if I set it to 2012, all 2013 items return and so on and so on.

    Is this the expected behaviour for .Range? Seems like a bug to me.

    thanks

  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 10:21
    Charles Afford
    0

    Hi I had the same thing with a predicate builder for sitecore using solr. My problem was trying to do a query on with .addyears() What I needed to do, was instance a new DateTime object with added years, and then pass that to my query.

    Charlie :)

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 10:30
    pantryfight
    0

    Cheers for your reply! But unfortunately that's not the case in my situation. (as much as I really wanted it to be!)

    I just tried the following with the same result:

    DateTime fromYear = new DateTime(year, 1, 1);
    DateTime toYear = new DateTime(year + 1, 1, 1);
    query = query.And().Range("dateAuthored", fromYear, toYear);
    
  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 10:35
    Charles Afford
    0

    Can you paste the whole query please :) what happens if you switch the date parameters?

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 10:52
    pantryfight
    0
    var query = ExamineManager.Instance.CreateSearchCriteria().NodeTypeAlias("NewsArticle");
    year = 2014;
    DateTime fromDate = new DateTime(year, 1, 1);
    DateTime toDate = new DateTime(year + 1, 1, 1);
                            query = query.And().Range("dateAuthored", fromDate, toDate);
    

    What's more, if I change my "toDate" to 31/12/year (difference of one day) instead I get no results at all. What the heck.

  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 10:54
    Charles Afford
    0

    Ok so you could remove the range and just use And(date1 < date2) :). Where date1 would be start of 2014 and date2 would be start of 2015 if that makes sense

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 10:59
    pantryfight
    0

    Not sure I get what you mean; And() doesn't accept any parameters.

  • Charles Afford 1163 posts 1709 karma points
    Aug 10, 2015 @ 11:01
    Charles Afford
    0

    Sorry I got the syntax wrong. I meant you should be able to a conditional check with out the range? Would that help?

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 11:33
    pantryfight
    0

    Yeah I'm not sure what you're talking about exists. :) I mean I can execute the search, and then filter out the results by year with Linq easy enough, but I'd prefer to do it with lucene/examine.

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 23:07
    pantryfight
    0

    I wonder if this is a bug with how DatePicker dates are stored/indexed.

    Looking in my ExternalIndex with Luke I can see that the Umbraco default propery "createDate" is indexed like this: 20150529004820000

    But, my "dateAuthored" datepicker property is indexed like this: 2014-08-18T00:00:00

  • pantryfight 60 posts 84 karma points
    Aug 10, 2015 @ 23:28
    pantryfight
    0

    OK son I don't trust .Range() in this circumstance especially with the inconsistencies in index date formatting. Since at the end of the day I only want to filter on year and not on a specific date range I've opted to use RawQuery instead.

    criteria = criteria.RawQuery(string.Format(@"+dateAuthored:\{0}*", year.ToString()));
    

    Just using RawQuery to check if the value starts with my chosen year. That way it should still work even if a future Umbraco update changes the DatePicker formatting in the index to match createdDate.

  • Charles Afford 1163 posts 1709 karma points
    Aug 11, 2015 @ 05:51
    Charles Afford
    0

    Yea it's not the way the date is stored. I have had the exact same issue with predicate builder for Sitecore. Glad you got it fixed. Beowulf be interesting to know what it is doing with the Range(). What are the parameters?

    https://our.umbraco.org/forum/using/ui-questions/16888-Examine-Search-Range-Values Not sure if that irk sheds anymore light :)

  • Charles Roper 48 posts 200 karma points
    May 11, 2016 @ 15:59
    Charles Roper
    0

    I've come across similar odd behaviour. In my case, the following StackOverflow answer provided the clue: http://stackoverflow.com/a/1644592/1944

    So what I did to solve the problem was convert my dates into SortableDateTimePattern (i.e., .ToString("s"). Here's a snippet of my code:

    var communityEventsSearcher = ExamineManager.Instance.SearchProviderCollection["CommunityEventsSearcher"];
    var query = communityEventsSearcher.CreateSearchCriteria()
        .Field("__NodeTypeAlias", "fsccommunityevent")
        .And().Range("startDate", DateTime.Now.ToString("s"), new DateTime(2999, 12, 31).ToString("s"))
        .And().OrderBy("startDate");
    

    This sorted out the weird date behaviour nicely.

Please Sign in or register to post replies

Write your reply to:

Draft