Smart Seach MVC Custmization AddSearchField

Mohamad Ramadan asked on October 6, 2020 18:34

I've configured local search index

I needed to add custom search fields, where I load the parent's Page types names in those fields

what I need is to add filter by those fields on the MVC site when calling the search index

(i/e need to filter articles by parent subcategory and parent category names)

here is my code

private void OnGetPageContent(object sender, DocumentSearchEventArgs e)
    {
        TreeNode indexedPage = e.Node;
        string categoryName = "";
        string subCategoryName = "";
        bool isAllParentPublished = false;
        if (indexedPage != null)
        {
            try
            {
                if (e.IndexInfo.IndexCodeName.Equals("ArticlesSearchIndex"))
                {
                    subCategoryName = indexedPage.Parent.DocumentName;
                    categoryName = indexedPage.Parent.Parent.DocumentName;
                    if (indexedPage.Parent.IsPublished && indexedPage.Parent.Parent.IsPublished)
                        isAllParentPublished = true;
                }

                SearchField sfCategory = new SearchField();
                sfCategory.FieldName = "CustomCategoryName";
                sfCategory.Value = categoryName;
                e.SearchDocument.AddSearchField(sfCategory);

now I don't know how to use those search fields on MVC Site, i've try the below code but not working

 string strCondition += string.Format("({0}:{1})", "CustomCategoryName", "\"" + advancedSearchDto.ArticleTitle + "\"").ToLower();

    var condition = new SearchCondition(strCondition , SearchModeEnum.ExactPhrase, SearchOptionsEnum.NoneSearch);
            searchText = SearchSyntaxHelper.CombineSearchCondition(searchText, condition);

Recent Answers


Keio Kwan answered on October 7, 2020 12:10

Please also check what search analyser you are using. I would suggest whitespace as it can manage better with space, number and symbol characters.

0 votesVote for this answer Mark as a Correct answer

Mohamad Ramadan answered on October 8, 2020 10:52 (last edited on October 8, 2020 10:52)

can you provide an answer

on how to use the added search field "CustomCategoryName" as a filter

and how to use documents fields like [DocumentPublishFrom], DocumentKeywords as filters

I can use the searchable fields on the page type definition but how to add extra filters from the document fields or custom ones

0 votesVote for this answer Mark as a Correct answer

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