I have the following code but need to make it sort the results in descending date order. There is a field added to the index called "date" -
var sc = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].CreateSearchCriteria(global::Examine.SearchCriteria.BooleanOperation.Or);
var terms = searchTerm.Split(' ');
var query = sc
.Field("title", searchTerm)
.Or()
.Field("introduction", searchTerm)
.Or()
.Field("shortintro", searchTerm);
foreach (var term in terms)
{
if (term != "")
{
query = query.Or().NodeName(term);
}
}
var results = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].Search(query.Compile());
I have tried all sorts of things but everything seems to give the error:
String was not recognized as a valid DateTime.
I have seen a number of places via Google that sem to offer a solution but nothing seems to work ... ??!!
thanks - it appears that the main reason I couldn't get it to work was that I had included Type="DateTime" on the index definition. I took that out and updated my code as below and it now seems to work :-)
var results = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].Search(query.Compile()).OrderByDescending((r => r.Fields["date"]));
How do I sort Examine query by Date
I have the following code but need to make it sort the results in descending date order. There is a field added to the index called "date" -
I have tried all sorts of things but everything seems to give the error:
String was not recognized as a valid DateTime.
I have seen a number of places via Google that sem to offer a solution but nothing seems to work ... ??!!
Hi Gordon
First of all, in your /config/ExamineIndex.config you need to add your date field to the <IndexUserFields> section to make it sortable, like this:
Note the EnableSorting="true" parameter...
I tend to use raw lucene syntax queries, rather than the dot notation, so I use this (where sb.ToString() is my query syntax):
var results = searchProvider.Search(searchCriteria.RawQuery(sb.ToString())).OrderBy((r => r.Fields["myDateField"]));
or
var results = searchProvider.Search(searchCriteria.RawQuery(sb.ToString())).OrderByDescending((r => r.Fields["myDateField"]));
Not sure if this works with the dot notation though...
Mike
Hi Mike,
thanks - it appears that the main reason I couldn't get it to work was that I had included Type="DateTime" on the index definition. I took that out and updated my code as below and it now seems to work :-)
Excellent - glad you got it working :-)
is working on a reply...