我已将搜索索引添加到Sitecore 6.6网站。为了测试,我创建了两个与索引参数匹配的项目,即使用索引的单个模板。
此模板有一个DropLink字段,名为" Story Data Item"而我想要做的是返回该字段的值与提供的Guid匹配的所有项目。
以下代码返回所有索引文档,当我运行它时,我返回两个项目,证明它们已编入索引。此外,第一项符合此条件:items.First().Fields["Story Data Item"].Value == "{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}"
Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
SearchHits hits = searchContext.Search(new Lucene.Net.Search.MatchAllDocsQuery(), int.MaxValue);
results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();
如果我现在将查询更改为以下内容,据我所知,它应该搜索同一个Guid的相关字段,但我没有得到任何结果。我不明白。我已经遵循了关于确保案件降低并且Guid转换为ShortID的所有建议。谁能告诉我我做错了什么?
Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
FieldQuery fldQuery = new FieldQuery("Story Data Item".ToLowerInvariant(), ShortID.Encode(new Guid("{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}")).ToLowerInvariant());
SearchHits hits = searchContext.Search(fldQuery, int.MaxValue);
results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();
索引配置如下:
<index id="hos" type="Sitecore.Search.Index, Sitecore.Kernel">
<param desc="name">$(id)</param>
<param desc="folder">__hos</param>
<Analyzer ref="search/analyzer" />
<locations hint="list:AddCrawler">
<master type="Sitecore.Search.Crawlers.DatabaseCrawler, Sitecore.Kernel">
<Database>master</Database>
<Tags>master hos</Tags>
<Root>/sitecore/content/Home/my_root</Root>
<include hint="list:IncludeTemplate">
<story>{AB3023C3-331D-4DD0-86B2-F8F48E1287EC}</story>
</include>
<Boost>2.0</Boost>
</master>
<master type="Sitecore.Search.Crawlers.DatabaseCrawler, Sitecore.Kernel">
<Database>web</Database>
<Tags>web hos</Tags>
<Root>/sitecore/content/Home/my_root</Root>
<include hint="list:IncludeTemplate">
<story>{AB3023C3-331D-4DD0-86B2-F8F48E1287EC}</story>
</include>
<Boost>2.0</Boost>
</master>
</locations>
</index>
我也遵循了这里的建议 - droplink and treelist values in sitecore search - 关于使用BooleanQuery
,但没有区别。
此外,Luke确认索引包含我需要的数据:
所有人都非常感谢。
答案 0 :(得分:3)
只需将FieldQuery
更改为TermQuery
,您的代码即可运行:
Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
TermQuery termQuery = new TermQuery(new Term("story data item", "{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}".ToLower()));
SearchHits hits = searchContext.Search(termQuery, int.MaxValue);
results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();