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 273 posts 658 karma points
    1 week ago
    Marc Love
    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 737 posts 4764 karma points MVP 3x c-trib
    1 week ago
    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 273 posts 658 karma points
    1 week ago
    Marc Love
    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 4215 posts 8872 karma points MVP admin c-trib
    6 days ago
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    0

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

  • Marc Love 273 posts 658 karma points
    6 days ago
    Marc Love
    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 4215 posts 8872 karma points MVP admin c-trib
    6 days ago
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    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 4215 posts 8872 karma points MVP admin c-trib
    6 days ago
    Ismail Mayat
    0

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

  • Marc Love 273 posts 658 karma points
    6 days ago
    Marc Love
    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 4215 posts 8872 karma points MVP admin c-trib
    6 days ago
    Ismail Mayat
    0

    price.ToString("D6") will do it

  • Marc Love 273 posts 658 karma points
    6 days ago
    Marc Love
    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 4215 posts 8872 karma points MVP admin c-trib
    6 days ago
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    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 273 posts 658 karma points
    6 days ago
    Marc Love
    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.

Please Sign in or register to post replies

Write your reply to:

Draft