I'm working on the Sitecore ContentSearch API.I have indexed a set of fields in an Item.is it possible to query more than one fields at the same time?
public static List<MovieSearchResultItem> SearchResults(string txtQuery)
{
using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
var query = context.GetQueryable<MovieSearchResultItem>().Where(result => result.Body.Contains(txtQuery)).ToList();
return query;
}
}
In the above query I'm just using the Body field.how to include more than one fields.My search data might be title or body or someother field so i want to check all three fields.
Try to add another condition to the Where
clause:
var query = context.GetQueryable<MovieSearchResultItem>()
.Where(result => result.Body.Contains(txtQuery)
|| result.Title.Contains(txtQuery)
|| result.OtherField.Contains(txtQuery)).ToList();
Simple enough, you can add it expression like so:
var query = context.GetQueryable<MovieSearchResultItem>().Where(result => result.Body.Contains(txtQuery) || result.Title.Contains(txtQuery))
Or you can use the PredicateBuilder, more info on that right here: http://www.sitecore.net/Learn/Blogs/Technical-Blogs/Sitecore-7-Development-Team/Posts/2013/05/Sitecore-7-Predicate-Builder.aspx
I can recommend the PredicateBuilder. Example:
Expression<Func<SearchResultItem, bool>> predicate = PredicateBuilder.True<SearchResultItem>();
predicate = predicate.And(p => p.TemplateName.Equals("News"));
predicate = predicate.And(p => p.Language == Context.Language.Name);
List<SearchResultItem> results = context
.GetQueryable<SearchResultItem>()
.Where(predicate)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.