Reputation: 5033
We're trying to sort our Lucene results by a Title field.
From what I understand from Lucene, this requires the field to be NOT_ANALYZED.
From what I read on the forum, this also requires us using the LowerCaseKeywordAnalyzer. (here)
I cannot figure out how to get it all together, this is what I have now, and sorting is not working:
In Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration:
<fields hint="raw:AddCustomField">
<!--...-->
<field luceneName="titleForSorting" storageType="yes" indexType="untokenized">Title</field>
</fields>
Our Search result class:
public class ContentSearchResultItem : SearchResultItem
{
public virtual string Title { get; set; }
[IndexField("titleForSorting")]
public virtual string TitleForSorting { get; set; }
}
Our Search implementation:
using (var context = ContentSearchManager.GetIndex(Context.Indexname).CreateSearchContext())
{
var query = context.GetQueryable<ContentSearchResultItem>()
.Where(x => x.Title == "New York")
.OrderBy(x => x.TitleForSorting);
var searchResult = query.GetResults();
var hitsQuery = searchResult.Hits;
// Or sort here ??
// hitsQuery = hitsQuery.OrderBy(x => x.Document.TitleForSorting);
var results = hitsResults.Select(x => x.Document).ToArray();
}
As said, I also read we should use the LowerCaseKeywordAnalyzer. But cannot figure out where to configure that. The doesn't seem to provide any place to add the option.
Any help welcome, thanks!
Upvotes: 4
Views: 2944
Reputation: 1469
What works for me is to add .ToList after the where clause
var query = context.GetQueryable<ContentSearchResultItem>()
.Where(x => x.Title == "New York")
.ToList()
.OrderBy(x => x.TitleForSorting);
Upvotes: 1
Reputation: 3551
You're correct, if you are sorting its best the the field is not tokenized as, if you have spaces etc, it break it up into small tokens and sort on those.
You can specify a custom analyzer as a child of the field
element in the fieldMap
section .. e.g
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="titleForSorting" 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>
(This is valid as of Sitecore 7.0 rev. 130918)
Upvotes: 4