Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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);
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.
Have you stumbled across this?
essentially I think your new field needs to be added of the type you are sorting by, eg NumericField rather than Field?
Thanks for the heads up, will give this a go and let you know if I get it to work.
Using luke can you show me what field looks like in the index.
In the past I have injected it in as
so if you have 20 that would end up as 000020 then it all works sorting and range searches.
So we can only sort alphabetically then?
Found this as well but didnt work:
I have this in the Document Writing Event:
NumericField sortFieldProductCurrentPrice = new NumericField("__Sort_productCurrentPrice", Field.Store.YES, true);
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
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.
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
// Add product price to search
decimal price = 0;
price = decimal.Parse(e.Fields["productCurrentPrice"]);
var sortFieldproductCurrentPrice = new Field("__Sort_productCurrentPrice", price.ToString("D6"), Field.Store.YES, Field.Index.NOT_ANALYZED);
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
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;
This appears to not be needed and was causing the issue. I got this code from:
I now have the following code in the Document Writing event:
double price = 0;
price = double.Parse(e.Fields["productCurrentPrice"]);
var sortFieldProductCurrentPrice = new NumericField("__Sort_productCurrentPrice", Field.Store.YES, true);
And I use the following sort criteria:
.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.
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!
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...
Write your reply to:
Image will be uploaded when post is submitted