我正在尝试将字段排序添加到ContentSearch查询中的日期字段。我能够正确地过滤索引字段,所以我假设字段正在填充正确的值,但是,结果没有正确排序。有什么想法吗?这是我用来查询的代码:
public static IEnumerable<Episode> GetPastEpisodes(Show show, bool includeMostRecent = false, int count = 0)
{
IEnumerable<Episode> pastEpisodes;
using (var context = _index.CreateSearchContext())
{
// querying against lucene index
pastEpisodes = context.GetQueryable<Episode>().Where(GetPastAirDatePredicate(show));
if(!includeMostRecent)
{
pastEpisodes = pastEpisodes.Where(item => item.Id != GetMostRecentEpisode(show).Id);
}
pastEpisodes = pastEpisodes.OrderByDescending(ep => ep.Latest_Air_Date);
if (count > 0)
{
pastEpisodes = pastEpisodes.Take(count);
}
pastEpisodes = pastEpisodes.ToList();
// map the lucene documents to Sitecore items using the database
foreach (var episode in pastEpisodes)
{
_database.Map(episode);
}
}
return pastEpisodes;
}
private static Expression<Func<Episode,bool>> GetPastAirDatePredicate(Show show)
{
var templatePredicate = PredicateBuilder.Create<Episode>(item => item.TemplateId == IEpisodeConstants.TemplateId);
var showPathPredicate = PredicateBuilder.Create<Episode>(item => item.FullPath.StartsWith(show.FullPath));
var airDatePredicate = PredicateBuilder.Create<Episode>(item => item.Latest_Air_Date < DateTime.Now.Date.AddDays(1));
var fullPredicate = PredicateBuilder.Create<Episode>(templatePredicate).And(showPathPredicate).And(airDatePredicate);
return fullPredicate;
}
使用LowerCaseKeywordAnalyzer
<field fieldName="latest_air_date" storageType="YES" indexType="UN_TOKENIZED" vectorType="NO" boost="1f" type="System.DateTime" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
<analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider"/>
</field>
Episode类设置了IndexField
属性:
[IndexField("latest_air_date")]
public virtual DateTime Latest_Air_Date {get; set; }
答案 0 :(得分:1)
凯尔,
据我所知,您的配置和代码看起来一切正常。我在一个vanilla Sitecore 7.2实例中嘲笑了一些非常相似的东西,Dates排序没有问题。
有一点需要注意,这可能是给你一些问题的,是Sitecore的DateTime的FieldReader只存储DateTime的日期部分。如果您希望能够按真实DateTime排序,则需要添加自定义FieldReader或一些计算字段。
请参阅此博客讨论该问题并说明更换自定义字段阅读器的过程:http://reasoncodeexample.com/2014/01/30/indexing-datetime-fields-sitecore-7-content-search/
我还建议用Luke查看索引,以验证索引中实际存在哪些数据。 https://code.google.com/p/luke/
最后,您可能希望在Sitecore中启用搜索调试,以准确了解Sitecore如何在Lucene中执行查询。
Sitecore.ContentSearch.config:
<setting name="ContentSearch.EnableSearchDebug" value="true" />