Hi,
In SharePoint,queries are logged by calling the RecordClick method of the search web service. The Microsoft.Office.Server.Search.WebControls.SearchResultsBaseWebPart webpart, which is used to render search results, makes the magic happen.
When the page rendered by Core Search Results Web Part is loaded a function is registered for the onlick event of all HTML link elements whos ID matches a RegExp filter. The page's unload event is also registered to send a soap request containing a
serialized Microsoft.Office.Server.Search.Query.QueryInfo object to the search web service RecordClick method. The properties of the QueryInfo object is then stored in the Share service provider database, in the MSSQLogUnprocessed and MSSQLogNonClickedUnprocessed
tables of Query_SSA_DB.
For your issue, please test the code as below which shows how to use RecordClick and QueryInfo OM for query logging and auto query suggestion preparation.
using (SPSite site = new SPSite("http://mysite"))
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
SearchServiceApplicationProxy searchAppProxy = ((SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(serviceContext));
for (int i = 0; i < 6; i++) // You need at least 6 here to make a query popular
{
string queryStr = "testme";
string queryId = Guid.NewGuid().ToString();
string sessionId = Guid.NewGuid().ToString();
string clickedUrl = "http://mysite/Shared%20Documents/testme.txt";
QueryInfo info = new QueryInfo();
info.QueryGuid = queryId;
info.SiteGuid = site.ID.ToString();
info.SessionId = sessionId;
info.UserName = System.Threading.Thread.CurrentPrincipal.Identity.Name;
info.QueryString = queryStr;
info.StartItem = 1;
info.ClickTime = DateTime.Now;
info.ClickedUrl = clickedUrl;
info.ResultsUrl = "http://mysite/fast/Pages/results.aspx?k=testme";
info.ClientType = QueryLogClientType.ObjectModel;
info.SearchTime = DateTime.Now;
// Send a Click QueryInfo
info.LogType = QueryLogType.Click;
searchAppProxy.RecordClick(info);
// Send a Query QueryInfo
info.LogType = QueryLogType.Query;
searchAppProxy.RecordClick(info);
}
}
CheckPoints after running the demo code:
1. Every 5 minutes or so there's a watch thread copying RecordClick QueryInfo into the "MSSQLogUnprocessed" table of your Query_SSA_DB. Make sure you see records with both 0 (Click) and 1 (Query) in "logType" column.
2. Run the "Query Logging" timer job manually and wait a bit, you'll see the "MSSQLogUnprocessed" table is cleaned up, while "MSSQLogQueryString", "MSSQLogClicks", and "MSSQLog" (other updated tables are
omitted) tables are updated with latest Click and Query records.
3. Run the following SQL to verify if the query string is qualified as Query Suggestion Candidate.
=======================
SELECT TopQueries.queryId, QueryCount, queryString FROM ( SELECT L.queryId, COUNT(*) AS QueryCount FROM (SELECT q.queryId, c.clickedUrlId FROM MSSQLog as q INNER JOIN MSSQLogClicks as c ON q.queryGuid = c.queryGuid) as L WHERE L.clickedUrlId IS NOT NULL GROUP BY queryId HAVING COUNT(*) > 5) as TopQueries JOIN MSSQLogQueryString ON TopQueries.queryId = MSSQLogQueryString.queryId
=======================
If the query string is returned from SQL above, you can safely run the "Prepare Query Suggestions" timer job. You'll find the "MSSQLogSessionSearchCounts" and "MSSQLogSearchCounts" tables will be added with that new popular
query. Turn your "SharePoint Server Search" into Verbose trace logging mode and grep for "GetPopularQueries" in ULS Viewer, you'll also see the new query suggestion candidates in ULS log.
4. Run "Get-SPEnterpriseSearchQuerySuggestionCandidates" against your Query SSA and you'll see the new query suggestion candidate has been added.
Reference:
http://blogs.technet.com/b/bestleon/archive/2011/10/26/use-recordclick-to-add-queryinfo-to-auto-query-suggestion-in-sharepoint-2010.aspx
For more information, you can have a look at the article:
Search Query Shared Services Protocol
Search Service Database Query Protocol Specification
https://msdn.microsoft.com/en-us/library/jj633109.aspx
Best Regards,
Eric