SharePoint: How can I improve the search relevance of a list item? (How do I use RecordPageClick?)
In order to improve the relevance of a list item, I learnt about RecordClick.
Thankfully, I found the following code on Technet to resolve the problem. I have duplicated it here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;
using System.IO;
namespace SPOnlinePageCallback2
{
class Program
{
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext("https://xxxxxxx.sharepoint.com/"))
{
SecureString passWord = new SecureString();
foreach (char c in "XXXXXXXX".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("xxxxxx@xxxxxx.onmicrosoft.com", passWord);
StringBuilder sbOut = new StringBuilder();
string resultTitleToClick = "tt";
string resultUrlToClick = "https://xxxxxx.sharepoint.com/Lists/clist1/DispForm.aspx?ID=4";
string immediacyQueryString = "tt";
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.RowLimit = 50;
keywordQuery.QueryText = immediacyQueryString;
keywordQuery.TrimDuplicates = true;
keywordQuery.EnableStemming = true;
keywordQuery.StartRow = 0;
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> resultTables = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
//pageImpressionId_partitionBucket_LCID (1131_1820_1033)
//NOTE: pageImpressionId is the search result page Id that it will change every time
string pageInfo = resultTables.Value.Properties["piPageImpression"].ToString();
string clickType = "Result";
string immediacySourceId = resultTables.Value.Properties["SourceId"].ToString();
//Microsoft.SharePoint.Client.Search.Query.
ResultTable relevantResults = resultTables.Value.Where(
result => result.TableType == KnownTableTypes.RelevantResults).FirstOrDefault(result => result.QueryRuleId == "00000000-0000-0000-0000-000000000000");
List<IDictionary<string, object>> resultRows = (List<IDictionary<string, object>>)relevantResults.ResultRows;
//int clickResultBlockId = (int)relevantResults.Properties["piPageImpressionBlockType"];
int blockType = (int)relevantResults.Properties["piPageImpressionBlockType"];
int subResultIndex = 0;
//var clickThroughResult = resultRows[5];
//string clickResultId = (string)relevantResults.Properties["piSearchResultId"];
Dictionary<string, object> clickThroughResult = null;
foreach(Dictionary<string, object> resultRow in resultRows)
{
string resultTitle = (string)resultRow["Title"];
string resultUrl = (string)resultRow["Path"];
if ((resultTitle.ToLower() == resultTitleToClick.ToLower()) && (resultUrl.ToLower() == resultUrlToClick.ToLower()))
{
clickThroughResult = resultRow;
break;
}
}
//var clickThroughResult = resultRows[1];
if (clickThroughResult != null)
{
string clickedResultId = (string)clickThroughResult["piSearchResultId"];
string immediacyTitle = (string)clickThroughResult["Title"];
string immediacyUrl = (string)clickThroughResult["Path"];
searchExecutor.RecordPageClick(pageInfo, clickType, blockType, clickedResultId, subResultIndex, immediacySourceId, immediacyQueryString, immediacyTitle, immediacyUrl);
clientContext.ExecuteQuery();
sbOut.Append(clickedResultId + "\r\n");
sbOut.Append(immediacyTitle + "\r\n");
sbOut.Append(immediacyUrl + "\r\n");
sbOut.Append("Clicked");
}
int resultCount = resultRows.Count;
Console.Write(sbOut.ToString());
Console.ReadLine();
}
}
}
}
Thankfully, I found the following code on Technet to resolve the problem. I have duplicated it here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;
using System.IO;
namespace SPOnlinePageCallback2
{
class Program
{
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext("https://xxxxxxx.sharepoint.com/"))
{
SecureString passWord = new SecureString();
foreach (char c in "XXXXXXXX".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("xxxxxx@xxxxxx.onmicrosoft.com", passWord);
StringBuilder sbOut = new StringBuilder();
string resultTitleToClick = "tt";
string resultUrlToClick = "https://xxxxxx.sharepoint.com/Lists/clist1/DispForm.aspx?ID=4";
string immediacyQueryString = "tt";
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.RowLimit = 50;
keywordQuery.QueryText = immediacyQueryString;
keywordQuery.TrimDuplicates = true;
keywordQuery.EnableStemming = true;
keywordQuery.StartRow = 0;
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> resultTables = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
//pageImpressionId_partitionBucket_LCID (1131_1820_1033)
//NOTE: pageImpressionId is the search result page Id that it will change every time
string pageInfo = resultTables.Value.Properties["piPageImpression"].ToString();
string clickType = "Result";
string immediacySourceId = resultTables.Value.Properties["SourceId"].ToString();
//Microsoft.SharePoint.Client.Search.Query.
ResultTable relevantResults = resultTables.Value.Where(
result => result.TableType == KnownTableTypes.RelevantResults).FirstOrDefault(result => result.QueryRuleId == "00000000-0000-0000-0000-000000000000");
List<IDictionary<string, object>> resultRows = (List<IDictionary<string, object>>)relevantResults.ResultRows;
//int clickResultBlockId = (int)relevantResults.Properties["piPageImpressionBlockType"];
int blockType = (int)relevantResults.Properties["piPageImpressionBlockType"];
int subResultIndex = 0;
//var clickThroughResult = resultRows[5];
//string clickResultId = (string)relevantResults.Properties["piSearchResultId"];
Dictionary<string, object> clickThroughResult = null;
foreach(Dictionary<string, object> resultRow in resultRows)
{
string resultTitle = (string)resultRow["Title"];
string resultUrl = (string)resultRow["Path"];
if ((resultTitle.ToLower() == resultTitleToClick.ToLower()) && (resultUrl.ToLower() == resultUrlToClick.ToLower()))
{
clickThroughResult = resultRow;
break;
}
}
//var clickThroughResult = resultRows[1];
if (clickThroughResult != null)
{
string clickedResultId = (string)clickThroughResult["piSearchResultId"];
string immediacyTitle = (string)clickThroughResult["Title"];
string immediacyUrl = (string)clickThroughResult["Path"];
searchExecutor.RecordPageClick(pageInfo, clickType, blockType, clickedResultId, subResultIndex, immediacySourceId, immediacyQueryString, immediacyTitle, immediacyUrl);
clientContext.ExecuteQuery();
sbOut.Append(clickedResultId + "\r\n");
sbOut.Append(immediacyTitle + "\r\n");
sbOut.Append(immediacyUrl + "\r\n");
sbOut.Append("Clicked");
}
int resultCount = resultRows.Count;
Console.Write(sbOut.ToString());
Console.ReadLine();
}
}
}
}
Comments
Post a Comment