Kentico 12 MVC Smart Search API with multiple indexes

Michael Lawrence asked on November 23, 2021 16:50

We have a Kentico 12 MVC site which features a smart search which is designed to search over multiple search indexes. One issue we are noticing is that sometimes search queries do not appear to be finding matches in all the indexes as expected. For example: we have a site index which uses the Pages type, and a People index which is a custom index - this index pulls in data from an external api. This page also allows the user to choose a specific index or utilize all indexes.

So a search for 'henry', should find matches in both the site and People index but only returns results from the site index. If I search specifically against the People index, the matches are returning as expected.

But a search for 'smith', will find matches in both the site and People index.

We have other custom indexes as well and we have noticed similar behavior.

Not sure if I'm missing something or if there maybe some other issue. Any help/guidance would be appreciated!

Thanks Michael

Example code:

string q = "QUERY TERM";
string type = "INDEX NAME or EMPTY STRING FOR ALL"
string extraConditions = " (documentname:" + q + ")^2 (classname:institute)^0.3 (classname:service)^0.1";


string[] words = q.Split(' ');
string newPhrase = "";
model.SpellingSuggestion = false;
string directory = AppDomain.CurrentDomain.BaseDirectory;
var dictionary = WordList.CreateFromFiles(directory + @"Build\Dictionaries\English (American).dic");
foreach(var word in words)
{
    bool test = dictionary.Check(word);
    if(test == false && keepTerms == "no")
    {
        IEnumerable<string> suggestions =  dictionary.Suggest(word);
        newPhrase += suggestions.FirstOrDefault() + " ";
        model.SpellingSuggestion = true;
    } else
    {
        if(words.Count() == 1)
        {
            newPhrase += word;
        } else
        {
            newPhrase += word + " ";
        }
    }
}
searchFriendlyTerms = newPhrase.ToLower();
searchFriendlyTerms = searchFriendlyTerms.Replace(" and ", " ");
searchFriendlyTerms = searchFriendlyTerms.Replace(" the ", " ");
searchFriendlyTerms = searchFriendlyTerms.Replace(" or ", " ");
searchFriendlyTerms = searchFriendlyTerms.Replace("  ", " ");
searchFriendlyTerms = searchFriendlyTerms.TrimStart().TrimEnd();
string indexes = "SiteIndex;JobsIndex;PeopleIndex;LocationsIndex;EventsIndex";
if(type == "jobs")
{
    indexes = "JobsIndex";
}
if(type == "locations")
{
    indexes = "LocationsIndex";
}
if(type == "people")
{
    indexes = "PeopleIndex";
}
if(type == "events")
{
    indexes = "EventsIndex";
}
var condition = new SearchCondition(extraConditions, SearchModeEnum.AllWords, SearchOptionsEnum.FullSearch, null, true);
string searchCondition = SearchSyntaxHelper.CombineSearchCondition(searchFriendlyTerms, condition);
SearchParameters parameters = new SearchParameters()
{
    SearchFor = searchCondition,
    SearchSort = "##SCORE##",
    Path = "/%",
    CurrentCulture = "EN-US",
    DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
    CombineWithDefaultCulture = false,
    CheckPermissions = false,
    SearchInAttachments = false,
    User = MembershipContext.AuthenticatedUser,
    SearchIndexes =  indexes,
    StartingPosition = skip,
    DisplayResults = 25,
    NumberOfProcessedResults = 1000,
    NumberOfResults = 0,
    AttachmentWhere = String.Empty,
    AttachmentOrderBy = String.Empty,
    ClassNames = ""
};
SearchResult results = SearchHelper.Search(parameters);

   Please, sign in to be able to submit a new answer.