6

I am trying to sort results by a decimal field, but it doesn't work. I need to use the property name indexer on the standard SearchResultItem so that it can be dynamic.

return queryable.OrderBy(i => i["price"]);

I have a string & DateTime fields that work fine using:

return queryable.OrderBy(i => i["date"]);
return queryable.OrderBy(i => i["title"]);

Config:

<field fieldName="price" 
   storageType="YES" 
   indexType="UNTOKENIZED" 
   vectorType="NO" 
   boost="1f" 
   type="System.Single" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />`

I have also tried System.Double , System.Decimal for the type but still no luck.

Update: field in Luke:

Luke

9

1 Answer 1

2

If your property name has to be dynamic, You could try the following code for sorting on numeric field:

return queryable.OrderBy(i => (double)i[(ObjectIndexerKey)"price"]);

Set Your field config to use System.Double :

<field fieldName="price" 
   storageType="YES" 
   indexType="TOKENIZED" 
   vectorType="NO" 
   boost="1f" 
   type="System.Double" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

You can ask Sitecore to use IndexFieldFloatingPointNumberValueConverter as default converter for any double value using this Patch file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration>
          <indexFieldStorageValueFormatter>
            <converters>
              <converter handlesType="System.Double" typeConverter="Sitecore.ContentSearch.LuceneProvider.Converters.IndexFieldFloatingPointNumberValueConverter , Sitecore.ContentSearch.LuceneProvider" />
            </converters>
          </indexFieldStorageValueFormatter>
        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

EDIT: I changed the field to be TOKENIZED as this is how it should be

1
  • 1
    This actually did work (without the converter config). I'm not sure why I couldn't get it to work when I first tried using this, I must have had the config wrong or forgot to rebuild the index. Commented Nov 23, 2017 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.