I have a field on my doctype called averageRating which is an int. I have added the field in my ExamineIndex.config in the <IndexUserFieds>-section and set EnableSorting=true and Type="Int32". But when I use it to sort by in my search result, it is treated as a string. I have also tried setting the Type="Number", but it is still treated as a string.
+IndexerData'ExamineManager.Instance.IndexerData' threw an exception of type 'System.NotImplementedException'Examine.IIndexCriteria {System.NotImplementedException}
I am just using the standard Examine-stuff. Any ideas why it throws this exception?
Hi - well NotImplementedException basically means that someone intended to implement that but never got round to it :0)
It would be interesting to see the full exception, or at least the type of the ExamineManager.Instance - just to check you have this configured right.
I did have problems with Examine once - found some old code where I did the sorting after I got the IEnumerable search results - so something like this:
Hi - sorry of course you need to assign when you order (wrote this up a bit quick :0) - or you could do this on the fly before databinding - so assume unordered, expose some constants (field names) and do the sorting in your ascx.cs.
The underlying search engine with examine is lucene and you have to be aware of lucenism when working with examine. I have similar issue sorting on numeric value. What you need to do is implement GatheringNodeData method and for that field inject into the index field value that can be sorted
void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e) { //check if this is 'Content' (as opposed to media, etc...) if (e.IndexType == IndexTypes.Content) { //some field here you want to make searchable
So what does the two underscores do, is this adding an additional sortable field next to the one that's already been configured..? You then have to order by "__[fieldname]"?
I have a sorting issue still on sortOrder... I have specified it as sortable, and I have a '__Sort_sortOrder' field which I am doing the following on
var query = Criteria.ParentId(categoryNodeId)
.And().NodeTypeAlias(NodeTypeCategory)
.And().OrderBy("__Sort_sortOrder");
But its still not sorting correctly? Looking via Luke its shown as 1085 the same as the 'sortOrder' field itself? Are we saying this just will not work, and you have to string format is to "D6"??
I wanted to use Lucene sorting badly so I ended up with something not so elegant but works for numeric values, using umbraco 4.11.2. Code has been kept simple to make it short and to show the point.
using Lucene.Net.Search;
/*Omitting code*/
public ISearchCriteria Sort(ISearchCriteria criteria, IEnumerablefields)
It injects the new collection of SortFields directly into the criteria object, then just use it as normal, e.g: ISearcher.Search(criteria);
By digging through the source code it seems Umbraco.Examine only sorts by strings (SortField.STRING) although I'm still curious as why Date types seemed to be correctly sorted - for later.
I might be missing the issue here, but I found setting the "sortOrder" field to Type="Number" in the ExamineIndex.config fixed my sorting issues, just in case anyone else has issues when getting ordering like: 1,10,11,2,3,4,5,6,7,8,9...
Sorting in Examine on int
Hi,
I have a field on my doctype called averageRating which is an int. I have added the field in my ExamineIndex.config in the <IndexUserFieds>-section and set EnableSorting=true and Type="Int32". But when I use it to sort by in my search result, it is treated as a string. I have also tried setting the Type="Number", but it is still treated as a string.
I define my search criteria like this:
Examine.SearchCriteria.ISearchCriteria criteria = ExamineManager.Instance.CreateSearchCriteria(BooleanOperation.And);
criteria.OrderByDescending(new string[] { "averageRating" });
And when I inspect the query, the sortfield is added as a string.
How do I change it to treat my field as an int?
thanks
Thomas
Hi - try debugging the below to see if your settings have taken effect:
var field = ExamineManager.Instance.IndexerData.UserFields.SingleOrDefault(f => f.Name == "averageRating");
string debugme = field.Type;
bool debugmemore = field.EnableSorting;
hi,
I get an exception:
+IndexerData'ExamineManager.Instance.IndexerData' threw an exception of type 'System.NotImplementedException'Examine.IIndexCriteria {System.NotImplementedException}
Hi - well NotImplementedException basically means that someone intended to implement that but never got round to it :0)
It would be interesting to see the full exception, or at least the type of the ExamineManager.Instance - just to check you have this configured right.
I did have problems with Examine once - found some old code where I did the sorting after I got the IEnumerable search results - so something like this:
IEnumerable<SearchResult> searchResults = ExamineManager.Instance.SearchProviderCollection[CL_SEARCHER_NAME].Search(q, useWildCards);
searchResults.OrderBy(s => Int32.Parse(s.Fields["yourfield"]));
//searchResults.OrderBy(s => Int32.Parse(s.Fields["yourfield"] ?? "0"));
//searchResults.OrderBy(s => Int32.Parse(String.IsNullOrEmpty(s.Fields["yourfield"]) ? "0" : s.Fields["yourfield"]));
Last two is if you have a nullable int and need to null check, if not the top should do it.
Hi - sorry of course you need to assign when you order (wrote this up a bit quick :0) - or you could do this on the fly before databinding - so assume unordered, expose some constants (field names) and do the sorting in your ascx.cs.
searchResults = searchResults.OrderBy(s => Int32.Parse(s.Fields["yourfield"]));
Guys,
The underlying search engine with examine is lucene and you have to be aware of lucenism when working with examine. I have similar issue sorting on numeric value. What you need to do is implement GatheringNodeData method and for that field inject into the index field value that can be sorted
Something along those lines then it will be sortable. You can also sort as you have it set but its not as efficient as getting lucene to do it.
Regards
Ismail
So what does the two underscores do, is this adding an additional sortable field next to the one that's already been configured..?
You then have to order by "__[fieldname]"?
it is yes. you then sort by __fieldname. I just suggested this to Kenny via msn as he had same issue and its working for him.
Regards
Ismail
I have a sorting issue still on sortOrder... I have specified it as sortable, and I have a '__Sort_sortOrder' field which I am doing the following on
But its still not sorting correctly? Looking via Luke its shown as 1085 the same as the 'sortOrder' field itself? Are we saying this just will not work, and you have to string format is to "D6"??
I ended up doing the following:
IEnumerable<SearchResult> SearchResults = SearchCurrentIndex(criteria);
SearchResults = SearchResults.OrderByDescending(r => r.Fields["averageRating"]);
I know I am not using Examine to sort, but this was the only way I could make it sort my searchresults.
I pretty much ended up doing the same :)
I wanted to use Lucene sorting badly so I ended up with something not so elegant but works for numeric values, using umbraco 4.11.2. Code has been kept simple to make it short and to show the point.
using Lucene.Net.Search;
/*Omitting code*/
public ISearchCriteria Sort(ISearchCriteria criteria, IEnumerablefields)
{
var customSorts = new List();
foreach(var f in fields)
customSorts.Add(new SortField(LuceneIndexer.SortedFieldNamePrefix + f, SortField.INT, true));
var luceneCriteria = criteria as LuceneSearchCriteria; //Underlying implementation used inside Umbraco.Examine
var sortFieldCollection = luceneCriteria.GetType().GetField("SortFields", System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance);
sortFieldCollection.SetValue(luceneCriteria, customSorts);
return luceneCriteria;
}
It injects the new collection of SortFields directly into the criteria object, then just use it as normal, e.g: ISearcher.Search(criteria);
By digging through the source code it seems Umbraco.Examine only sorts by strings (SortField.STRING) although I'm still curious as why Date types seemed to be correctly sorted - for later.
I might be missing the issue here, but I found setting the "sortOrder" field to Type="Number" in the ExamineIndex.config fixed my sorting issues, just in case anyone else has issues when getting ordering like: 1,10,11,2,3,4,5,6,7,8,9...
Then rebuilt the index
is working on a reply...