Hey all, I am seeing some weird stuff when implementing my search controller. If I test my index in Luke, it return pretty good relevant results. Same thing with the "Search Preview" section in Kentico itself. However, the real results using SearchHelper.Search() are not ideal and not matching the search preview.
Can someone help me understand what I'm doing wrong here? I have tried AnyWords, AnyWordsWithSynonyms for the search modes, and BasicSearch and FullSearch for the search options.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using CLIENT.Models.Search;
using CMS.Helpers;
using CMS.Membership;
using CMS.Search;
namespace CLIENT.Controllers
{
public class SearchController : Controller
{
// GET: Search
[ValidateInput(false)]
public ActionResult Index()
{
return View();
}
[Route("Search/Results/{page?}")]
public ActionResult Results(string q, string type, int page = 1)
{
int skip = 0;
int take = 10;
if (page > 1)
{
skip = ((page - 1) * take);
}
q = HttpUtility.UrlDecode(q);
if (String.IsNullOrWhiteSpace(q))
{
// Creates a model representing empty search results
SearchResultViewModel emptyModel = new SearchResultViewModel
{
Items = new List<SearchResultItem>(),
Query = String.Empty
};
return View(emptyModel);
}
DocumentSearchCondition docCondition = null;
if (!String.IsNullOrWhiteSpace(type))
{
docCondition = new DocumentSearchCondition(type, "en-us", "en-us", true);
} else
{
docCondition = null;
}
// Gets the search index
string indexes = "SiteIndex";
SearchResultViewModel model = new SearchResultViewModel();
var condition = new SearchCondition(null, SearchModeEnum.AnyWord, SearchOptionsEnum.BasicSearch, docCondition, true);
string searchCondition = SearchSyntaxHelper.CombineSearchCondition(q, condition);
if (indexes != null)
{
// Prepares the search parameters
SearchParameters parameters = new SearchParameters()
{
SearchFor = searchCondition,
SearchSort = "##SCORE##",
Path = "/%",
CurrentCulture = "EN-US",
DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
CombineWithDefaultCulture = false,
CheckPermissions = false,
SearchInAttachments = false,
User = (UserInfo)MembershipContext.AuthenticatedUser,
SearchIndexes = indexes,
StartingPosition = skip,
DisplayResults = 10,
NumberOfProcessedResults = 1000,
NumberOfResults = 0,
AttachmentWhere = String.Empty,
AttachmentOrderBy = String.Empty,
ClassNames = ""
};
// Performs the search and returns the matching results as a SearchResult object
//SearchResult countResult = SearchHelper.Search(countparameters);
SearchResult results = SearchHelper.Search(parameters);
model.PageSize = 10;
model.CurrentPage = page;
model.TotalItems = results.TotalNumberOfResults;
model.Items = results.Items;
model.Query = q;
}
return View(model);
}
}
}