Lucene Sorting

Vote:
 

Hi,

I've added a field to products for sorting in lucene based product search:

[Display(
            GroupName = SystemTabNames.Settings,
            Order = 100,
            Name = "Product Sort Order",
            Description = "The sort order value")]
[Editable(true)]
[DefaultValue(0)]
[IncludeValuesInSearchResults]
[Searchable]
public virtual int ProductSortOrder { get; set; }

The code for building the search criteria includes setting the sort order like:

SearchSortField[] sNew =
                            {
                                new SearchSortField(Constants.LuceneConstants.LUCENE_INTERNAL_NEW_FIELD, true),
                                new SearchSortField(Constants.LuceneConstants.LUCENE_INTERNAL_PRODUCT_SORTORDER_FIELD, false)
                            };
                    _catalogCriteria.Sort = new SearchSort(sNew);

However, it seems to be sorting based on the string representation of the value so coming out as 1, 10, 4, and 5 rather than 1, 4, 5, 10.

Any ideas on how to fix this would be appreciated.

#146721
Mar 22, 2016 4:42
Vote:
 

Hi,

I think you need to add [SortableInSearchResult] attribute to your field and of course, rebuild your index. 

Not entirely sure - I'll have to take a look at our code base, but that my best guess.

Regards,

/Q

#146726
Mar 22, 2016 8:51
Vote:
 

Hi,

I tried what you suggested:

[Display(
            GroupName = SystemTabNames.Settings,
            Order = 100,
            Name = "Product Sort Order",
            Description = "The sort order value")]
[Editable(true)]
[DefaultValue(0)]
[IncludeValuesInSearchResults]
[Searchable]
[SortableInSearchResults]
public virtual int ProductSortOrder { get; set; }

However the order the products being returned is:

<ul>
<li data-order="1">...</li>
<li data-order="2">...</li>
<li data-order="200">...</li>
<li data-order="3">...</li>
<li data-order="4">...</li>
</ul>

rather than

<ul>
<li data-order="1">...</li>
<li data-order="2">...</li>
<li data-order="3">...</li>
<li data-order="4">...</li>
<li data-order="200">...</li>
</ul>

*note: data-order is the ProductSortOrder int value assigned to each product.

#147578
Edited, Apr 19, 2016 10:21
Vote:
 

It is sorting as text. You could probably add type in your sorting field. Can you please share the code where you are adding Sorting object.

Create a SearchSort Field with type Numeric and add that in SearchOrder Object and use that. In theory this should work

Regards

/K 

#147793
Edited, Apr 25, 2016 18:25
Vote:
 

I don't think that you can create SearchSortField with type - only with name and if the sort should be descending or not.

I was able to reproduce the issue which OP has - and it looks like a missing feature/bug for me. I'll try to dig further when time permits.

Regards,

/Q

#147995
Apr 29, 2016 12:19
Vote:
 

Hi, a bit late but we had a simular problem with sorting in Lucene. The problem seems to be that all properties are indexed as text (or that is what I saw when looking in the index using Luke).  We solved it by inheriting from CatalogIndexbuilder and overriding the AddMetaField function. In there I append some leading zero's to certain numerical properties. That way the sorting works like:

00001

00002

00003

00020

etc.

We used this blog post as a reference. http://world.episerver.com/blogs/Lee-Crowe/Dates/2011/7/EPiCommerce-and-Multiline-Dictionary-Meta-Field-Bug/

#148340
May 10, 2016 14:43
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.