Search code examples
c#.netsolrsolrnet

Clean way to log response from debugQuery in SolrNet


We have added a property that let us add debugQuery=true as option when querying Solr from our application. Sometimes the queries can be complex, so we need to see what parts of the query takes time. However I have not been able to find any good way to get the response from Solr without using ISolrConnection.Get() and build up the query manualy (and getting the raw XML). Today we do like this:

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDocument>>();
var qo = new QueryOptions();
        qo.AddFilterQueries(AuthorizationIncludeQuery(solrQueryParameter.AccessibleDocuments));
        qo.AddFilterQueries(new SolrQuery("person_id:" + solrQueryParameter.PersonId));
        qo.Start = solrQueryParameter.Start;
        qo.Rows = solrQueryParameter.Rows;
        qo.Fields = new[] { "*", "score" };


if (Constants.SolrDebugEnabled)
{
      var debugParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("debugQuery", "true") };
      qo.ExtraParams = debugParams;
}
try
{
   return solr.Query(searchQuery, qo);
}

Since we use ISolrOperations<T> and return SolrQueryResults<T> The extra parameters gets "lost" in the process. Any way to read out the extra parameters without too much rewriting?


Solution

  • A quick look at the SolrQueryExecuter class on GitHub

    gave me the answer. I basicly implemented the Execute method in my code:

    private static SolrQueryResults<SolrConsistencyCheckDocument> FindAllDocumentsWithDebugEnabled(string searchQuery, QueryOptions qo)
    {
            var directConnection = ServiceLocator.Current.GetInstance<ISolrConnection>();
            var queryResult = new SolrQueryResults<SolrConsistencyCheckDocument>();
            var debugParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("debugQuery", "true") };
            qo.ExtraParams = debugParams;
            var parser = ServiceLocator.Current.GetInstance<ISolrAbstractResponseParser<SolrConsistencyCheckDocument>>();
            var solrQueryTest = new SolrQueryExecuter<SolrConsistencyCheckDocument>(
                parser,
                directConnection,
                ServiceLocator.Current.GetInstance<ISolrQuerySerializer>(),
                ServiceLocator.Current.GetInstance<ISolrFacetQuerySerializer>(),
                ServiceLocator.Current.GetInstance<ISolrMoreLikeThisHandlerQueryResultsParser<SolrConsistencyCheckDocument>>());
            var parameters = solrQueryTest.GetAllParameters(new SolrQuery(searchQuery), qo);
            var resultq = directConnection.Get(solrQueryTest.Handler, parameters);
            var responseXML = XDocument.Parse(resultq);
            LogDebugElement(responseXML);
            parser.Parse(responseXML, queryResult);
            return queryResult;
        }