1

Using Lucene, and content search I cannot sort items by their name nor custom title field.

The items are imported from a read-only data provider and are successfully indexed (I've inspected it manually)

The results I get back are semi-in-order but not but incoherent. Without the orderby clause, the items are more in-order than without it which is unusual.

Here's the simplified code:

ProviderSearchContext
  .GetQueryable<MyCustomSearchResultItem>()
  .Filter(m => m.TemplateId == "{SomeGuid}") 
  .OrderBy(m => m.Title) 
  .Skip(20 * (Page - 1))
  .Take(20)   
  .GetResults() 

public class MyCustomSearchResultItem: SearchResultItem {
        [IndexField("title")]
        public string Title { get; set; }
}

2 Answers 2

2

Lucene uses tokenization for the fields. Since most string fields have spaces so it will not work with OrderBy without proper configuration.

In the Lucene configuration file's fieldNames block section, you will need to make sure the field you are using for "ordering by" is specified. In addition, an analyzer is needed to ensure it is added to the index documents correctly. You can specify a custom analyzer as a child of the field element in the fieldMap section. it will look like this -

<fieldNames hint="raw:AddFieldByFieldName">
   <field fieldName="sortingField" storageType="YES" indexType="UN_TOKENIZED"    vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
       <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
   </field>
   ...
</fields>

the indexType value should be “UN_TOKENIZED”, to make sure OrderBy clause to use the entire string for ordering and not the tokens.

5
  • I should also mention, we search on this field too. Wouldn't that also affect search? Does the same thing apply to the Item Name? Because in our context ItemName == Title where possible. I ordering was causing a problem there too.
    – Adam Hess
    Aug 20, 2019 at 18:43
  • 2
    @AdamHess you have the same field with different name tokenized and untokenized, so the tokenized used for search and the untokenized used for ordering. Aug 21, 2019 at 4:31
  • @TamásTárnok is correct, you can define the same field with a different name. Aug 21, 2019 at 7:39
  • Google solr copyfield
    – josedbaez
    Aug 21, 2019 at 13:22
  • @TamásTárnok it looks like the untokenized is specified in one of the default sitecore configs. If i understand things correctly the fieldName has to match the item's field name...however this will cause a lot of conflict with other items (they all use the same generic name of title) is there a way to indicate it should be indexed based on the fieldid instead? My current solution is to create a computed field, but i feel like there should be something out of the box to solve this.
    – Adam Hess
    Aug 28, 2019 at 19:40
0

It appears that Sitecore automatically tokenizes the field I'm interested in.

Found inside Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config:

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

This in tern causes the field to be broken up into multidimensional commponents rather than a single field to be sorted on.

More about this can be found here: https://community.sitecore.net/technical_blogs/b/sitecore_7_development_team/posts/sorting-and-ordering-results-in-sitecore-7

Your Answer

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

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