I have written a custom search control that passes a user's search query to an ashx handler, which then performs the search and returns the results back to my control serialized as JSON. The handler executes multiple queries, so I'm using the SearchExecutor.ExecuteQueries method to perform the search. It appears that this method is losing the HttpContext information when there are multiple KeywordQueries being executed - in which case, I'm getting results returned for users that they do not have permission to access. If there is only one KeywordQuery in the dictionary that is passed to ExecuteQueries, then the results are returned correctly. So I'm assuming it's a threading issue.
Here is the code for my handler (with query information simplified):
public class SPSearchTest : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
string masterQuery = "";
if (context.Request.QueryString["Query"] != null)
masterQuery = context.Request.QueryString["Query"];
SPSite searchSite = SPContext.Current.Site;
string jsonObj = "";
Dictionary<string, Query> allQueries = new Dictionary<string, Query>();
KeywordQuery docQuery = new KeywordQuery(searchSite);
docQuery.QueryText = masterQuery + " AND SPContentType=Document";
allQueries.Add("Documents", docQuery);
KeywordQuery eventQuery = new KeywordQuery(searchSite);
eventQuery.QueryText = masterQuery + " AND SPContentType=Event";
allQueries.Add("Events", eventQuery);
SearchExecutor searchExecutor = new SearchExecutor();
Dictionary<string, ResultTableCollection> resultTableCollection = searchExecutor.ExecuteQueries(allQueries);
IEnumerable<ResultTable> resultTables = resultTableCollection["Documents"].Filter("TableType", KnownTableTypes.RelevantResults);
ResultTable resultTable = resultTables.FirstOrDefault();
DataTable results = resultTable.Table;
jsonObj = JsonConvert.SerializeObject(results, Common.serializerSettings);
context.Response.ContentType = "text/plain";
context.Response.Write(jsonObj);
}
}
If I remove the "Events" query, the results from the "Documents" query consists of 1 document, which is correct. But when the "Events" query is included, the "Documents" query returns 2 results, one of which the user does not have permission to access.
I can't find much out there on this ExecuteQueries method and how it handles the search threads. I was hoping to use it to avoid threading issues of my own, but it doesn't appear to be working.


