我正在使用sitecore和solr显示搜索结果。场景是如果用户搜索某些内容,搜索代码将首先检查该关键字是否在"关键字" sitecore项目的字段,如果它发现它将显示结果,如果找不到,它将在标题和描述字段中检查该关键字。
现在的问题是 -
尽管关键字字段中存在值,但下面的条件始终为false且永远不会给出结果。
var Query = searchContext.GetQueryable<SearchResultItem>()
.Where(i => (i["keywords"].Contains(SearchQuery)));
其中标题和说明的相同查询工作正常
var Query2 = searchContext.GetQueryable<SearchResultItem>()
.Where(i => (i["title"].Contains(SearchQuery)
|| i["description"].Contains(SearchQuery)));
对于每个sitecore项目,我都有标题,说明和关键字字段。
以下是详细代码段。
public class SearchModel
{
public string SearchQuery { get; set; }
List<WebSiteSearchResult> lst = new List<WebSiteSearchResult>();
public IEnumerable<WebSiteSearchResult> SiteSearchResult()
{
var searchContext = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext();
var Query = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["keywords"].Contains(SearchQuery)));
var result = Query.GetResults();
if (result.TotalSearchResults != 0)
{
//some operations
}
else
{
var Query2 = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["title"].Contains(SearchQuery) || i["description"].Contains(SearchQuery)));
var result2 = Query2.GetResults();
if (result2.TotalSearchResults != 0)
{
//some operations
}
}
lst = lst.OrderBy(i => i.itemBoostingRatio).ToList();
return lst;
}
}
public class WebSiteSearchResult
{
public string itemKeywords { get; set; }
public int itemBoostingRatio { get; set; }
public string itemTitle { get; set; }
public string itemDescription { get; set; }
}
而且,这是我的sitecore项目:http://i.stack.imgur.com/liw59.png
答案 0 :(得分:3)
鉴于您的Sitecore项目关键字位于SLT字段(&#34;贷款,汽车贷款,自行车贷款和#34;),请记住此字段是标记化索引。这意味着在存储处理期间,查询将使用空格和标点字符作为分隔符分割为标记,如下所示:
现在,如果我们使用Equals
和Contains
进行搜索,搜索日志将为我们提供适当的序列化查询以在solr中进行测试:
等于:?q=keywordstest_t:("bike loan")
"scooter loan"
,我们就不会得到任何结果。 包含:?q=keywordstest_t:(*bike loan*)
"scooter L"
,我们会因为 L 而得到结果。
Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config
?中设置为true
<indexAllFields>true</indexAllFields>
&#13;
要获得最佳做法,请创建一个继承自Sitecore基础SearchResultItem
的新搜索结果项类,以便您可以使用此实例直接调用字段属性&#39; :
public class BasePageSearchResultItem : SearchResultItem
{
[IndexField("keywords")]
public string Keywords { get; set; }
}
Equals
查询应该用于在索引字段中查找一个这些字词:
var Query = searchContext.GetQueryable<SearchResultItem>()
.Where(i => i.Keywords.Equals(SearchQuery));
结果集取决于搜索查询"scooter loan"
规定的要求,并提供了示例项的关键字字段值"Loans, Car Loan, Bike Loan"
: