Copied to clipboard

Flag this post as spam?

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


  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 08, 2019 @ 16:27
    Marc Love (uSkinned.net)
    0

    Sorting in Examine - adding fields via Document Writing Event

    Hi,

    I am trying to sort my examine search results based on a numeric value.

    How do you add a new sortable field via the Document Writing event and define its type.

    I am using the following code:

    var sortFieldPrice = new Field("__Sort_price", price, Field.Store.YES, Field.Index.NOT_ANALYZED);
                e.Document.Add(sortFieldPrice);
    

    I know you can do this via the ExamineIndex.config file with:

    <IndexUserFields><add Name="price" EnableSorting="true" Type="Double"/></IndexUserFields>
    

    Just dont know how to do this in code.

    Cheers,

    Marc

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Jan 08, 2019 @ 22:26
    Marc Goodson
    0

    Hi Marc

    Have you stumbled across this?

    https://github.com/Shazwazza/Examine/issues/92

    essentially I think your new field needs to be added of the type you are sorting by, eg NumericField rather than Field?

    regards

    Marc

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 09, 2019 @ 10:27
    Marc Love (uSkinned.net)
    0

    Hi Marc,

    Thanks for the heads up, will give this a go and let you know if I get it to work.

    Cheers,

    Marc

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 10, 2019 @ 14:07
    Ismail Mayat
    0

    Marc,

    Using luke can you show me what field looks like in the index.

    In the past I have injected it in as

    price.ToString("D6")

    so if you have 20 that would end up as 000020 then it all works sorting and range searches.

    Regards

    Ismail

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 14:24
    Marc Love (uSkinned.net)
    0

    Hi Ismail,

    So we can only sort alphabetically then?

    Found this as well but didnt work:

    https://github.com/Shazwazza/Examine/wiki/Sorting-results

    I have this in the Document Writing Event:

            NumericField sortFieldProductCurrentPrice = new NumericField("__Sort_productCurrentPrice", Field.Store.YES, true);
            sortFieldProductCurrentPrice.SetDoubleValue(double.Parse(e.Fields["productCurrentPrice"]));
            e.Document.Add(sortFieldProductCurrentPrice);
    

    and this for my sort criteria setup:

    criteria.GroupedOr(fields, query)
                        .And()
                        .Field(USNShopConstants.ProductSearchablePath, productLandingPage.Id.ToString())
                        .Not()
                        .Field("umbracoNaviHide", "1")
                         .And().OrderBy("productCurrentPrice[TYPE=DOUBLE]");
    
  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 14:48
    Marc Love (uSkinned.net)
    0

    Using Luke I can see the new field within my index so no issues there.

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 15:05
    Marc Love (uSkinned.net)
    0

    Interestingly I have added a test field using the above code and made the type an integer. In this scenario the sort works, its the double type that isnt working

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 10, 2019 @ 15:27
    Ismail Mayat
    0

    In luke what format is it stored? In Lucene its all strings I have only over got sorting / range searches working when I have formatted numbers as D6 dates i always put in as yyyyMMddHHmmss

    Regards

    Ismail

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 15:35
    Marc Love (uSkinned.net)
    0

    Hi Ismail,

    How can I tell what format the field is in Luke, I can just see the contents of the cell in the Search Tab.

    I do ordering in our blog with the format stored as a string yyyyMMddHHmmss which will work because its still alphabetical.

    This is the first time I have attempted ordering based on a true numerical value.

    Cheers,

    Marc

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 10, 2019 @ 16:31
    Ismail Mayat
    0

    The contents of the field what does it look like? Did you d6 format it?

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 16:37
    Marc Love (uSkinned.net)
    0

    I cant D6 format it? as its a price I dont know how many leading zeros it would need to have as the price could be anything.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 10, 2019 @ 16:43
    Ismail Mayat
    0

    price.ToString("D6") will do it

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 17:05
    Marc Love (uSkinned.net)
    0
            // Add product price to search
            decimal price = 0;
    
            if (e.Fields.ContainsKey("productCurrentPrice"))
                price = decimal.Parse(e.Fields["productCurrentPrice"]);
    
            var sortFieldproductCurrentPrice = new Field("__Sort_productCurrentPrice", price.ToString("D6"), Field.Store.YES, Field.Index.NOT_ANALYZED);
            e.Document.Add(sortFieldproductCurrentPrice);
    

    This gives me an error "System.FormatException: Format specifier was invalid. " The error is on price.ToString("D6")

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 10, 2019 @ 17:22
    Ismail Mayat
    0

    Arrgh you have decimal this works with ints see https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#DFormatString

    If you parse to int would that work ?

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 17:25
    Marc Love (uSkinned.net)
    0

    Problem is losing the numbers after the decimal point and not being able to sort on them.

    so if something is £9.98 and £9.99 I cant sort. Although I could multiply by 100.

    Think that might be the solution.

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 18:32
    Marc Love (uSkinned.net)
    0

    No joy, I think I am going to have to try this with the hard coded fields in the examine config file. It looks like the alphabetical sort is placing a 0 after 9 so

    1234567890

    instead of

    0123456789

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 10, 2019 @ 19:21
    Marc Love (uSkinned.net)
    0

    Finally got this to work and I think I know what was causing me the issue before. In the ApplicationStarted method I had:

     foreach (var field in websiteIndexer.IndexerData.UserFields)
                {
                    if (field.Name == "productCurrentPrice")
                    {
                        field.EnableSorting = true;
                        break;
                    }
                }
    

    This appears to not be needed and was causing the issue. I got this code from:

    https://github.com/Shazwazza/Examine/issues/92

    I now have the following code in the Document Writing event:

    double price = 0;
    
                if (e.Fields.ContainsKey("productCurrentPrice"))
                    price = double.Parse(e.Fields["productCurrentPrice"]);
    
    
            var sortFieldProductCurrentPrice = new NumericField("__Sort_productCurrentPrice", Field.Store.YES, true);
            sortFieldProductCurrentPrice.SetDoubleValue(price);
            e.Document.Add(sortFieldProductCurrentPrice);
    

    And I use the following sort criteria:

    criteria.GroupedOr(fields, query)
                        .And()
                        .Field(USNShopConstants.ProductSearchablePath, productLandingPage.Id.ToString())
                        .Not()
                        .Field("umbracoNaviHide", "1")
                        .And().OrderBy(new SortableField("productCurrentPrice", SortType.Double));
    

    Hopefully I have got this right now and it helps someone else out if they come across this.

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Jan 20, 2019 @ 20:13
    Marc Goodson
    0

    Hi Marc

    Glad you got it working!, sorry to have been vague in my reply, but yes, that's the path I was thinking that the issue would take you down!

    regards

    Marc

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jan 21, 2019 @ 10:14
    Marc Love (uSkinned.net)
    0

    Cheers Marc, always good to get input from others in the community, you and Ismail guided me in the right direction. Helps to know that you are not completely off track when investigating.

Please Sign in or register to post replies

Write your reply to:

Draft