public PagedSearchResult <ISearchResult> SearchAhead(string query, SiteContext siteContext, PaginationOptions paginationOptions = null) { if (paginationOptions == null) { paginationOptions = new PaginationOptions { PageNumber = 1, PageSize = 4 }; } var terms = _searchTermProvider.GetTermsFromQuery(query).ToList(); // must provide some search terms if (!terms.Any()) { return(new PagedSearchResult <ISearchResult>(paginationOptions.PageNumber, paginationOptions.PageSize)); } var predicate = PredicateBuilder.True <LuceneSearchResultItem>(); // change to AndAllAutocomplete for simple autocomplete (no search ahead) predicate = predicate.AndAllContentOrBoostedContentOrAutocompleteTitle(terms); using (var context = ContentSearchManager.GetIndex(SiteContext.Current.GetIndexName()).CreateSearchContext()) { var queryable = context.GetQueryable <LuceneSearchResultItem>() .Where(predicate) .AddFilter(new DefaultFilter <LuceneSearchResultItem>(siteContext)); // execute query return(_searchResultFactory.ToPagedSearchResult(queryable, paginationOptions)); } }
private Expression <Func <T, bool> > Query <T>(Expression <Func <T, bool> > where, string value) where T : LuceneSearchResultItem { if (string.IsNullOrWhiteSpace(value)) { return(where); } var terms = _searchTermProvider.GetTermsFromQuery(value).ToList(); return(where.AndAllContentOrBoostedContent(terms)); }