【问题标题】:Sitecore 7: Sorting Lucene results by fieldSitecore 7:按字段对 Lucene 结果进行排序
【发布时间】:2013-10-04 09:36:47
【问题描述】:

我们正在尝试按 Title 字段对 Lucene 结果进行排序。

根据我对 Lucene 的了解,这要求该字段为 NOT_ANALYZED。

从我在论坛上看到的,这也需要我们使用LowerCaseKeywordAnalyzer。 (here)

我无法弄清楚如何将它们组合在一起,这就是我现在所拥有的,并且排序不起作用:

在 Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration:

<fields hint="raw:AddCustomField">
  <!--...-->
  <field luceneName="titleForSorting"     storageType="yes" indexType="untokenized">Title</field>
</fields>

我们的搜索结果类:

public class ContentSearchResultItem : SearchResultItem
{
    public virtual string Title { get; set; }

    [IndexField("titleForSorting")] 
    public virtual string TitleForSorting { get; set; }
}

我们的搜索实现:

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();
}

如前所述,我还读到我们应该使用 LowerCaseKeywordAnalyzer。但无法弄清楚在哪里配置它。似乎没有提供任何添加选项的地方。

欢迎任何帮助,谢谢!

【问题讨论】:

    标签: search lucene sitecore sitecore7


    【解决方案1】:

    您是对的,如果您对其进行最佳排序,则该字段不会被标记为,如果您有空格等,它会将其分解为小标记并对其进行排序。

    您可以在fieldMap 部分中将自定义分析器指定为field 元素的子元素......例如

    <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>
    

    (自 Sitecore 7.0 rev. 130918 起有效)

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但在ContentSearchManager.GetIndex(Context.Indexname).CreateSearchContext() 上得到了 NullReferenceException。 TitleForSorting 实际上不是 Sitecore 中的字段,它是我在raw:AddCustomField 部分手动添加的字段
    • 您是否同时拥有fieldMap 条目和自定义字段条目?如果没有,请尝试这样做,因为 fieldMap 应该在索引时给出分析器提示。
    • 是的。但我现在看到:NullRef 异常是因为我简化了 fieldNames 部分下的字段条目。现在,我正在使用您的示例,并且该异常消失了。但是,排序仍然不起作用...
    • 另外,在重新索引后,当用 Luke 检查时,titleforsorting 字段的术语不是小写的。那么问题出在哪里?
    • 有没有办法使用自定义 lucene 字段名称来做到这一点?我正在尝试对已经索引标记的“标题”进行排序,我不想更改它。理想情况下,我想添加一个名为“title_for_sort”之类的新的未标记字段。
    【解决方案2】:

    对我有用的是在 where 子句之后添加 .ToList

    var query = context.GetQueryable<ContentSearchResultItem>()
            .Where(x => x.Title == "New York")
            .ToList()
            .OrderBy(x => x.TitleForSorting);
    

    【讨论】:

    • 确实,您对搜索查询的排序应该始终发生在查询上,而不是结果堆栈上。将它放在 GetResults 调用之前会将排序传递给搜索索引,从而确保在返回结果之前进行排序。排序后仅对您返回的结果进行排序。在这种情况下,对所有内容进行排序的唯一方法就是获取所有内容,这对性能非常不友好。
    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多