I have a search that I’m wanting to exclude any search result that has the name of the “Title” field in the search. For example, say I type in “Contact” in the search bar. I don’t want the Contact Us page to come up, but if someone wants to search something that has words in the Contact Us page, then its ok. I am able to get templateName and IDs but can’t seem to get fields…
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");
This is what I have, but I want to add something to it to exclude the “Title” field and maybe the “SEO” field. So probably something like:
query = query.And(i => i.Fields["Title"];
But in this case its including not excluding it. And I can’t do:
query = query.And(i != i.Fields["Title"];
It won’t accept that answer.
2
Answers
Try to use code like that
i => !i["Title"].Contains(searchTerm)
:If you want to strongly type it, you just need to extend the SearchResultItem class with your field name.
Then your code would be like this