【问题标题】:How to index sub-content in Sitecore with Lucene?如何使用 Lucene 索引 Sitecore 中的子内容?
【发布时间】:2014-12-05 15:04:13
【问题描述】:

我正在使用带有 MVC 和组件方法的 Sitecore 7.2 来构建页面。这意味着页面大部分是空的,内容来自页面上放置的各种渲染。但是,我希望搜索结果返回主页,而不是单个内容。

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

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

【问题讨论】:

    标签: asp.net-mvc lucene sitecore


    【解决方案1】:

    您可以在索引中创建一个计算字段,该字段查看页面上的渲染并解析每个渲染​​的数据源项。一旦你有了这些项目中的每一个,你就可以索引它们的字段并将所有这些数据连接在一起。

    一种选择是使用原生“内容”计算字段来执行此操作,该字段本身就是全文搜索使用的。

    【讨论】:

      【解决方案2】:

      另一种解决方案是将 HttpRequest 返回到您已发布的站点并从本质上抓取 HTML。这样可以确保所有渲染都包含在索引中。

      您可能不想索引常用部分,例如菜单和页脚,因此使用HTMLAgilityPackFizzlerEx 仅返回特定父容器的内容。如果需要,您可以更聪明地移除内部容器。请记住也去掉 html 标签 :)

      using HtmlAgilityPack;
      using Fizzler.Systems.HtmlAgilityPack;
      
      //get the page
      var web = new HtmlWeb();
      var document = web.Load("http://localsite/url-to-page");
      var page = document.DocumentNode;
      
      var content = page.QuerySelector("div.main-content");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多