【发布时间】: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