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?