我有一个搜索,我想要排除任何名称为"标题"的搜索结果。搜索中的字段。例如,假设我输入"联系"在搜索栏中。我不希望联系我们页面出现,但如果有人想在“联系我们”页面中搜索有文字的内容,那么就可以了。我能够获得templateName和ID,但似乎无法获得字段......
Item homeItem = Main.Utilities.SitecoreUtils.getHomeItem();
var query = PredicateBuilder.True<SearchResultItem>();
query = query.And(i => i.Paths.Contains(homeItem.ID));
query = query.And(i => i.Content.Contains(searchTerm));
query = query.And(i => i.TemplateName != "MenuFolder");
这就是我所拥有的,但我想添加一些东西来排除&#34;标题&#34;领域,也许是&#34; SEO&#34;领域。所以可能是这样的:
query = query.And(i => i.Fields["Title"];
但在这种情况下,其中包括不排除它。而我无法做到:
query = query.And(i != i.Fields["Title"];
它不会接受这个答案。
答案 0 :(得分:3)
尝试使用类似i => !i["Title"].Contains(searchTerm)
的代码:
Item homeItem = Main.Utilities.SitecoreUtils.getHomeItem();
var query = PredicateBuilder.True<SearchResultItem>();
query = query.And(i => i.Paths.Contains(homeItem.ID));
query = query.And(i => i.Content.Contains(searchTerm));
query = query.And(i => i.TemplateName != "MenuFolder");
query = query.And(i => !i["Title"].Contains(searchTerm));
答案 1 :(得分:3)
如果你想强烈输入它,你只需要用你的字段名扩展SearchResultItem类。
public class SitecoreItem : SearchResultItem
{
[IndexField("title")]
public string Title { get; set; }
[IndexField("__smallcreateddate")]
public DateTime PublishDate { get; set; }
[IndexField("has_presentation")]
public bool HasPresentation { get; set; }
}
然后你的代码就像这样
IQueryable<SitecoreItem> query = context.GetQueryable<SitecoreItem>();
SearchResults<SitecoreItem> results = null;
query = query.Where(x => x.Title.Contains(searchText);
results = query.GetResults();