Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
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.
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),
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.
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.
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!
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?
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.
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 :
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);
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:
<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
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);
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?
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
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted