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
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
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
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.
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:
I know you can do this via the ExamineIndex.config file with:
Just dont know how to do this in code.
Cheers,
Marc
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
Hi Marc,
Thanks for the heads up, will give this a go and let you know if I get it to work.
Cheers,
Marc
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
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:
and this for my sort criteria setup:
Using Luke I can see the new field within my index so no issues there.
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
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
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
The contents of the field what does it look like? Did you d6 format it?
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.
price.ToString("D6") will do it
This gives me an error "System.FormatException: Format specifier was invalid. " The error is on price.ToString("D6")
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 ?
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.
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
Finally got this to work and I think I know what was causing me the issue before. In the ApplicationStarted method I had:
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:
And I use the following sort criteria:
Hopefully I have got this right now and it helps someone else out if they come across this.
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
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.
is working on a reply...