如何根据父页面创建“类别”构面?

时间:2017-01-11 08:13:55

标签: c# episerver episerver-find

我有一个页面层次结构,例如

- Root
   - Category 1
        - Page 1
        - Page 2
   - Category 2
        - Page 3

我想使用“查找”根据“类别”页面名称创建过滤器。这是我到目前为止所得到的,我无法弄清楚的是第4行

var result = _searchClient.Search()
                  .For(query)
                  .Filter(x => x.Ancestors().Match(rootPageLink.ID.ToString()))
                  .FilterFacet("Categories", x => x.ParentLink) // This doesn't work
                  .HistogramFacetFor(x => x.Price, 100)
                  .Skip((pageNumber - 1) * pageSize)
                  .Take(pageSize)
                  .GetContentResult();

显然这不起作用,因为Filter()希望将Filter作为第二个参数,但是你可以看到我正在尝试做什么。它与GROUP BY ParentLink之类的SQL查询大致相似,然后显示类似

的信息
  

第1类(2页)
  第2类(1页)

1 个答案:

答案 0 :(得分:1)

考虑

  • 你的结构看起来像那样
  • 您正在使用UnifiedSearch

UnifiedSearch的ISearchContent接口在您的方案中包含两个有用的属性

  • SearchSection
  • SearchSubsection

考虑你有这样的路径

/returns/misc/yourstuff

当查看您的资料作为统一搜索时, SearchSection 返回 SearchSubsection 其他

查看其他时,点击 SearchSection 返回 SearchSubsection 点击时会null或不存在。

这意味着

  • SearchSection 总是下面的第一个节点,除非您的结果是第一级节点
  • SearchSubsection 始终 SearchSection 下面的第一个节点,除非您的结果是第一级或第二级节点

在您的方案中,类别页面将始终表示为SearchSections。

我目前无法对此进行测试,但是这样的事情应该让你开始。

var result = _searchClient.Search()
                  .For(query)
                  .Filter(x => x.SearchSection.Exists())
                  .Filter(x => x.SearchSection.Match("yourcategorypage"))
                  .TermsFacetFor(x => x.SearchSection) // create facets if you like, fun stuff
                  //.FilterFacet("Categories", x => x.ParentLink) // This doesn't work
                  //.Filter(x => x.Ancestors().Match(rootPageLink.ID.ToString()))
                  .HistogramFacetFor(x => x.Price, 100)
                  .Skip((pageNumber - 1) * pageSize)
                  .Take(pageSize)
                  .GetContentResult();

==编辑如下==

要使用投影自定义SearchSection,应该实现与此类似的内容,假设您希望SearchSection始终是类型CategoryPage中最接近的父类。

在查找初始化中实施

// use the appropriate datatype if PageData isn't applicable, i.e. ArticlePage
SearchClient.Instance.Conventions.ForInstancesOf<PageData>()
    .ExcludeField(x => x.SearchSection()) // remove the default mapping
    .IncludeField(x => x.SearchSection(true)); // add your own mapping

SearchSection扩展方法

public static class FieldExtensions
{
    public static string SearchSection<T>(this T page, bool overriding) where T : PageData
    {
        // we need to check if this page is a CategoryPage first
        if(page is CategoryPage) {
            return ""; // I assume nothing?
        }

        var ancestors = loader.GetAncestors(page.ContentLink).OfType<CategoryPage>();
        if(ancestors.any()){
            var closestCategoryPage = ancestors.First();
            return closestCategoryPage.whateverproperty;
        }

        // customs: nothing to declare
        return "";
    }
}

或类似的东西,你会弄明白:)