在 SolrNet 中记录 debugQuery 响应的简洁方法

Clean way to log response from debugQuery in SolrNet

我们添加了一个 属性 让我们在从我们的应用程序查询 Solr 时添加 debugQuery=true 作为选项。有时查询可能很复杂,因此我们需要查看查询的哪些部分需要时间。但是,如果不使用 ISolrConnection.Get() 并手动构建查询(并获取原始 XML),我无法找到任何从 Solr 获取响应的好方法。今天我们这样做:

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);
}

因为我们使用了ISolrOperations<T>和return SolrQueryResults<T>额外的参数在这个过程中得到了"lost"。有什么方法可以在不重写太多的情况下读出额外的参数?

快速浏览 SolrQueryExecuter class GitHub

给了我答案。我基本上在我的代码中实现了 Execute 方法:

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;
    }