Copied to clipboard

Flag this post as spam?

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


  • Thomas Dolberg 74 posts 95 karma points
    Oct 17, 2011 @ 22:26
    Thomas Dolberg
    0

    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

  • Thomas 49 posts 78 karma points c-trib
    Oct 29, 2011 @ 16:15
    Thomas
    0

    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;

           

  • Thomas Dolberg 74 posts 95 karma points
    Oct 30, 2011 @ 23:11
    Thomas Dolberg
    0

    hi,

     

    I get an exception:

    +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?
    thanks
    Thomas
  • Thomas 49 posts 78 karma points c-trib
    Oct 31, 2011 @ 09:57
    Thomas
    1

    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.

  • Thomas 49 posts 78 karma points c-trib
    Oct 31, 2011 @ 09:59
    Thomas
    0

    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"]));

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 31, 2011 @ 10:37
    Ismail Mayat
    0

    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

      ExamineManager.Instance.IndexProviderCollection[Indexer].GatheringNodeData
    += ExamineEvents_GatheringNodeData;

    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


    e.Fields.Add("__" + FieldAlias,e.Field[FieldAlias].ToString("D6"));
    }
    }

    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

     

  • Thomas 49 posts 78 karma points c-trib
    Oct 31, 2011 @ 12:28
    Thomas
    0

    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]"?

     

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 31, 2011 @ 12:43
    Ismail Mayat
    0

    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

  • Lee 1130 posts 3088 karma points
    Jan 07, 2012 @ 15:33
    Lee
    0

    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"??

  • Thomas Dolberg 74 posts 95 karma points
    Jan 12, 2012 @ 23:28
    Thomas Dolberg
    0

    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.

     

     

  • Lee 1130 posts 3088 karma points
    Jan 13, 2012 @ 07:30
    Lee
    0

    I pretty much ended up doing the same :)

  • Ernesto Chavez 3 posts 23 karma points
    Feb 05, 2013 @ 03:34
    Ernesto Chavez
    0

    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.

  • Mark 255 posts 612 karma points
    Dec 09, 2014 @ 18:06
    Mark
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft