Smart Search Index | Progammatically add fields

Sean Dooley asked on February 18, 2021 18:12

Using the information on https://docs.xperience.io/k12sp/configuring-kentico/setting-up-search-on-your-website/customizing-the-content-of-search-indexes I am able to append content of an indexed item.

I was wondering if it is possible to programmatically add fields to a search index. For example, I would like to add DocumentPageTitle, DocumentPageDescription and DocumentPageKeyWords to a Smart Search Pages index. I've tried the following examples

private void GetContent_Execute(object sender, DocumentSearchEventArgs e)
{
  TreeNode indexedPage = e.Node;
  if (indexedPage == null) return;

  // doesn't appear in the index
  e.SearchDocument.Add("documentpagetitle", indexedPage.DocumentPageTitle, true, false);

  // doesn't appear in the index        
  var pageTitle = new SearchField
  {
    FieldName = "documentpagetitle",
    Value = indexedPage.DocumentPageTitle
  };

  e.SearchDocument.AddSearchField(pageTitle);
}

Version Kentico 12 SP MVC

Recent Answers


Juraj Ondrus answered on February 19, 2021 10:45

Are you using a custom smart search index? Or, why you want to add those fields to the index again? Those are system fields and you just need to set them basically as any other search fields for a page type fields. The only difference is that it has to be done in Modules -> edit Pages module -> Classes -> edit Page class -> Search tab.
Anyway, those fields are already part of the search index - so why do you want to add them again? This is the default setup: screenshot. The fields are by default marked as Content and Tokenized. Unless someone unchecked them, they should be already available in the pages index.
Have you tried using Luke tool to inspect the index files?

0 votesVote for this answer Mark as a Correct answer

Sean Dooley answered on February 19, 2021 10:57

When using Luke, I can't see those fields in the Index.

Checking Modules | Edit Pages module | Classes | Edit Page class | Search, I can see that those fields are marked as Content. Assuming if I wanted to target those specific fields in a Lucene query, I would have to mark them as searchable?

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 19, 2021 11:27

What are the index settings, please? When you set some value in the metadata field and then use the search preview and search for the term, is the right record returned?

0 votesVote for this answer Mark as a Correct answer

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