Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > How to add extra Fields in search result View modes: 
User avatar
Member
Member
Nel215 - 8/10/2013 1:45:48 AM
   
How to add extra Fields in search result
Hi,
I created custom smart search index for a database view(created by user table a some other tables). Here is code of custom search Index:


public class MyIndex:ICustomSearchIndex
{
public void Rebuild(SearchIndexInfo srchInfo) {
if (srchInfo != null) {
IndexWriter iw = srchInfo.GetWriter(true);

if (iw != null) {
try
{
SearchIndexSettingsInfo sisi = srchInfo.IndexSettings.Items[SearchHelper.CUSTOM_INDEX_DATA];
GeneralConnection conn = ConnectionHelper.GetConnection();
DataSet ds = conn.ExecuteQuery("cms.user.selectalltest", null);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
Document doc = SearchHelper.CreateDocument(SearchHelper.CUSTOM_SEARCH_INDEX, Guid.NewGuid().ToString(), SearchHelper.INVARIANT_FIELD_VALUE, DateTime.Now, SearchHelper.INVARIANT_FIELD_VALUE);
string text = ValidationHelper.GetString(row["FullName"], "");
text = text.ToLower();
// Adds a content field. This field is processed when the search looks for matching results.
SearchHelper.AddField(doc, SearchHelper.CONTENT_FIELD, text, false, true);

SearchHelper.AddField(doc, SearchHelper.CUSTOM_TITLE, ValidationHelper.GetString(row["Email"], ""), true, false);
SearchHelper.AddField(doc, SearchHelper.CUSTOM_CONTENT, TextHelper.LimitLength(text, 200), true, false);
SearchHelper.AddField(doc, SearchHelper.CUSTOM_DATE, DateTime.Now, true, false);
SearchHelper.AddField(doc, "UserName", ValidationHelper.GetString(row["UserName"], "abc"), true, false);
iw.AddDocument(doc);
}
iw.Optimize();
}

}
catch(Exception ex) {
EventLogProvider.LogException("CustomTextFileIndex", "Rebuild", ex);

}

finally
{
iw.Close();
}

}

}

}

}


In the search result, besides the 5 default fields(Scores,title,content,created,image), I would like to add some extra fields in the search result. Say I field called "UserName", and I tried to add this code in my search index
SearchHelper.AddField(doc, "UserName", ValidationHelper.GetString(row["UserName"], "abc"), true, false);



the custom index seems working fine and there is no errors in the event log except I cant just use Eval("UserName") to retrieve the "UserName" field in the search result webpart's transformation.

what is correct way to add fields in search result and how can i access them?


User avatar
Member
Member
kentico_sandroj - 8/10/2013 11:22:03 AM
   
RE:How to add extra Fields in search result
Hello,

Here is the documentation for indexing custom content. Just wondering, is the custom table a Kentico table or is it outside of Kentico? Also, did you rebuild the index?

User avatar
Member
Member
Nel215 - 8/12/2013 5:43:25 AM
   
RE:How to add extra Fields in search result
halo,

I got my problem solved. It is in this document and should use GetSearchValue(string columnName) rather than Eval()

User avatar
Member
Member
kentico_sandroj - 8/13/2013 6:44:32 PM
   
RE:How to add extra Fields in search result
Hello,

Thank you for sharing the solution, I am glad you were able to configure this.