如何使用Lucene索引Sitecore中的子内容? [英] How to index sub-content in Sitecore with Lucene?

查看:68
本文介绍了如何使用Lucene索引Sitecore中的子内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Sitecore 7.2与MVC结合使用,并且使用一种组件方法来构建页面.这意味着页面基本上是空的,内容来自放置在页面上的各种渲染.但是,我希望搜索结果返回主页,而不是单个内容.

I'm using Sitecore 7.2 with MVC and a component approach to page building. This means that pages are largely empty and the content comes from the various renderings placed on the page. However, I would like the search results to return the main pages, not the individual content pieces.

这是我到目前为止拥有的基本代码:

Here is the basic code I have so far:

public IEnumerable<Item> GetItemsByKeywords(string[] keywords)
{
    var index = ContentSearchManager.GetIndex("sitecore_master_index");
    var allowedTemplates = new List<ID>();
    IEnumerable<Item> items;

    // Only Page templates should be returned
    allowedTemplates.Add(new Sitecore.Data.ID("{842FAE42-802A-41F5-96DA-82FD038A9EB0}"));

    using (var context = index.CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck))
    {
        var keywordsPredicate = PredicateBuilder.True<SearchResultItem>();
        var templatePredicate = PredicateBuilder.True<SearchResultItem>();
        SearchResults<SearchResultItem> results;

        // Only return results from allowed templates
        templatePredicate = allowedTemplates.Aggregate(templatePredicate, (current, t) => current.Or(p => p.TemplateId == t));

        // Add keywords to predicate
        foreach (string keyword in keywords)
        {
            keywordsPredicate = keywordsPredicate.And(p => p.Content.Contains(keyword));
        }

        results = context.GetQueryable<SearchResultItem>().Where(keywordsPredicate).Filter(templatePredicate).GetResults();
        items = results.Hits.Select(hit => hit.Document.GetItem());
    }

    return items;
}

推荐答案

您可以在索引中创建一个计算字段,该字段将查看页面上的渲染并解析每个渲染​​的数据源项.在拥有所有这些项目之后,您就可以为其字段建立索引并将所有这些数据连接在一起.

You could create a computed field in the index which looks at the renderings on the page and resolves each rendering's data source item. Once you have each of those items you can index their fields and concatenate all of this data together.

一种选择是使用本地内容"计算字段执行此操作,该字段本身就是全文搜索使用的字段.

One option is to do this with the native "content" computed field which is natively what full text search uses.

这篇关于如何使用Lucene索引Sitecore中的子内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆