【问题标题】:Sitecore 7 Linq Taxonomy style querySitecore 7 Linq Taxonomy 样式查询
【发布时间】:2014-06-19 09:21:10
【问题描述】:

更新

原来我的配置不起作用,我恢复使用 sitecore 的 contentsearch 定义中的 defaultindexconfiguration。

一旦我这样做了,一切都会神奇地工作。我刚刚在查询中添加了模板条件,一切都很好。


2 天以来我一直在努力解决这个问题,但我无处可去。

场景:

我的文章标记有树/多列表中的项目。

我定义了一个索引来在整个站点中查找特定的模板类型,然后存储一些值(名称、描述、路径、标签)。这个索引使用了一个小的 indexConfiguration。我正在避免使用默认索引配置,因为它会过度膨胀我的索引,我只需要一组苗条的结果。

<myConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
    <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
        <indexAllFields>true</indexAllFields>
        <Analyzer ref="contentSearch/configuration/defaultIndexConfiguration/analyzer" />
        <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
            <fieldNames hint="raw:AddFieldByFieldName">
                <field fieldName="Article Tags" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Teaser Title" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Teaser Description" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Article Date" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.DateTime" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="Views" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.Int32" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                <field fieldName="__Workflow state" storageType="yes" indexType="untokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
            </fieldNames>
        </fieldMap>
        <fields hint="raw:AddComputedIndexField">
            <field fieldName="isfinal" storageType="yes" indexType="tokenized">Core.Search.ComputedWorkflowState, Core</field>
            <field fieldName="Teaser Image" storageType="yes" indexType="tokenized">Core.Search.ComputedTeaserImage, Core</field>
            <field fieldName="Article Url" storageType="yes" indexType="tokenized">Core.Search.ComputeUrl, Core</field>
        </fields>
        <include hint="list:IncludeTemplate">
            <articlePage>{28432890-0F71-4E2F-8577-7848F90FCBCC}</articlePage>
        </include>
    </defaultIndexConfiguration>
</myConfiguration>

通过使用 Luke 查看索引,我可以看到标签已正确编入索引。每个标签在文档中都有自己的行。

我创建了一个扩展 SearchResultItem 的类(ArticleItem),然后我用装饰实现了适当的属性。

[IndexField("article_tags")]
public List<ID> tags{get;set;}

现在我尝试使用 sitecore linq 和谓词构建器进行查询。

using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
    IQueryable<ArticleItem> query = context.GetQueryable<ArticleItem>();

    var predicate = PredicateBuilder.True<ArticleItem>();

    foreach (var id in tags)
    {
        var tempTerm = id;
        predicate = predicate.Or(p => p.Tags.Contains(id));
    }

    var results = context.GetQueryable<ArticleItem>().Where(predicate).GetResults();

    if (results != null)
    {
        if (results.Hits.Any())
        {
            return results.Hits.Select(x => x.Document);
        }
    }
}

当我自己调用 context.GetQueryable() ,然后遍历结果,标签都存在,肯定有匹配。

我是否配置错误?

*附加信息

使用其他查询测试搜索: p.Tags.Contains("3") 不返回任何结果(肯定有一个项目的 guid 包含字符 3。

通过 luke 查找时,我的文章标签的 termcount 为 0。

通过将字段转换为计算字段,我的搜索结果现在可以正常工作(尽管这些值存储为整个字符串而不是被标记化)并且出现了术语计数。

【问题讨论】:

    标签: c# linq sitecore lucene.net sitecore7


    【解决方案1】:

    如果您将谓词简化为对其中一个标签的简单检查,您会得到任何结果吗?

    var results = context.GetQueryable<ArticleItem>().Where(p=>p.Tags.Contains(tags[0])).GetResults();
    

    这将帮助您确定您的谓词构建是否是问题。

    如果这也不起作用,您是否检查过以确保传入的“标签”集合中的值与存储在对象上的值有效等价?可能是“包含”检查失败,因为您的集合中的值与加载到您的对象上的值不匹配。

    另一种调试方法是从可查询对象中删除“Where”子句并获取整个集合的结果。一旦您选择了所有文档并获得了所有项目的完整列表,您是否可以构建一个 Where 子句,使用您的标签集合过滤项目列表?

    如果这也不起作用,这可能是确认输入的“标签”列表或 Where 子句本身有问题。

    【讨论】:

    • 感谢小杰的建议。我已经使用字符 3 进行了测试(guid 中有几个带有字符 3 的标签)并将返回类型更改为字符串。 query.Where(i=> i["article_tags"].Contains("3"));这什么都不返回。查看结果本身,我得到的所有值都非常好。
    【解决方案2】:

    我认为有几件事需要检查。您提到您的标签字段正在搜索索引中填充,因此我们知道这不是索引/抓取问题。

    首先查看您的 Sitecore 搜索日志,以便确定搜索查询的格式。您将搜索字段定义为:

    <field fieldName="Article Tags" storageType="yes" indexType="tokenized" vectorType="no" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
    

    但是您将模型中的字段定义为:

    [IndexField("article_tags")]
    

    这可能只是 sitecore 在“articles_taqs”上搜索的字段名称不匹配,但索引已将其存储为“文章标签”。当我模拟这个时,Sitecore 正在发送下划线。

    要检查的另一件事是您的 GUID 的格式。使用 .Contains(ID) 是在没有破折号或花括号的小写 GUID 上进行搜索。使用 LUKE 查看事物是如何存储在您的索引中的,然后将其与 Sitecore 在 Sitecore 搜索日志中搜索它的方式进行比较。

    【讨论】:

    • 我在 Luke 中注意到的是概览选项卡中的“术语计数”为 0。这是什么迹象吗?顺便说一句,我在自定义配置中定义的所有字段都有 0“术语计数”,即使当我查看每个文档时有多个条目。我已经尝试了所有的 guid、shorted、lower case、shortened-lower case,但没有返回任何内容。
    【解决方案3】:

    将您的“标签”属性更改为:

    [IndexField("article_tags"), TypeConverter(typeof(IndexFieldEnumerableConverter))]
    public virtual System.Collections.Generic.IEnumerable<ID> tags{ get; set; }
    

    我认为这可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 1970-01-01
      • 2010-09-05
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多