Content type managed property for search queries programming
1. I was having a requirement that to query a search scope which has all the tasks items from a site collection.
2. Keyword Query is the search object model used here as follows:
var keywordQuery = new KeywordQuery("http://localhost/")
{
QueryText = string.Format("AssignedTo:\"{0}\"", "Employee2"),
KeywordInclusion = KeywordInclusion.AnyKeyword,
HiddenConstraints = "scope:" + "\"Tasks\"",
ResultTypes = ResultType.RelevantResults,
RankingModelId = "D9BFB1A1-9036-4627-83B2-BBD9983AC8A1"
};
keywordQuery.SelectProperties.Add("Title");
keywordQuery.SelectProperties.Add("Path");
keywordQuery.SelectProperties.Add("StartDate");
ResultTableCollection resultTableCol = keywordQuery.Execute();
ResultTable resultTable = resultTableCol[ResultType.RelevantResults];
var resultsDataTable = new DataTable
{
TableName = "Results"
};
resultsDataTable.Load(resultTable, LoadOption.OverwriteChanges);
3. The start date is the default managed property provided by sharepoint and was unable to list in the search results. The exception it raised was
“Property doesn't exist or is used in a manner inconsistent with schema settings.”
While executing the line
ResultTableCollection resultTableCol = keywordQuery.Execute();
4. I digged still in depth and made sure that below settings are done for the meta dat property “StartDate”.

5. And made as retrievable using following code snippet:
SearchServiceApplication searchApplication = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>("Search Service Application");
var searchSchema = new Schema(searchApplication);
ManagedProperty managedProperty = searchSchema.AllManagedProperties["StartDate"];
managedProperty.Retrievable = true;
managedProperty.Update();
6. Still I faced the same exception and this is caused due to the reason that it is default content type managed property which actually cannot be queried J
7. The work around at last was creating a new managed property and adding all the mappings above which are there for start date property as follows:
8. Now queried the search scope as follows:
var keywordQuery = new KeywordQuery("http://localhost/")
{
QueryText = string.Format("AssignedTo:\"{0}\"", "Employee2"),
KeywordInclusion = KeywordInclusion.AnyKeyword,
HiddenConstraints = "scope:" + "\"Tasks\"",
ResultTypes = ResultType.RelevantResults,
RankingModelId = "D9BFB1A1-9036-4627-83B2-BBD9983AC8A1"
};
keywordQuery.SelectProperties.Add("Title");
keywordQuery.SelectProperties.Add("Path");
keywordQuery.SelectProperties.Add("StartDateTest");
ResultTableCollection resultTableCol = keywordQuery.Execute();
ResultTable resultTable = resultTableCol[ResultType.RelevantResults];
var resultsDataTable = new DataTable
{
TableName = "Results"
};
resultsDataTable.Load(resultTable, LoadOption.OverwriteChanges);
and the data table is as follows:
Hope this may help you!!!

No comments:
Post a Comment