Search code examples
json.netsolrnet

How to get all SolrNet query result info in JSON format?


I am not sure if this is a JSON.Net issue or a SolrNet issue, which is why I am tagging it with both.

I am using SolrNet and currently handling all page interactivity with JavaScript from an MVC call, by returning a JsonNetResult encoding of the object returned by solr.Query(). I am now experimenting with Faceting, but am not seeing the Facet info in my results.

I've got an MVC Controller method like the one below. (JsonNetResult is similar to the stock JsonResult, but encodes JSON using JSON.Net, described here.)

public JsonNetResult Index(string keywords)
{
    JsonNetResult jsonNetResult = new JsonNetResult();

    var documents = solr.Query(new SolrQuery(keywords), new QueryOptions 
    { 
        Rows = 10,
        Facet = new FacetParameters 
        {
            Queries = new[] {new SolrFacetFieldQuery("system")}
        }
    });
    jsonNetResult.Formatting = Formatting.Indented;
    jsonNetResult.Data = documents;
    return jsonNetResult;
}

I was expecting to see the faceting information encoded into JSON in the JsonNetResult, but all it contains is the array of hashes of the documents matching my query. Am I missing something in how SolrNet response objects work, or do I really need to parse through the object myself, and create something that JSON.Net can use to encode all of the information related to my query?

FYI, I have tried using a standard JsonResult in MVC, and the results are the same. Also, the reason I am using SolrNet and not just calling Solr directly and asking for JSON is because we do not want to expose the Solr search engine web interface directly to the user.


Solution

  • Since Solr can respond with JSON, if you want to return JSON directly to the view you'd be incurring some unnecessary overhead by having SolrNet deserialize a XML response, then serialize it to JSON. Instead, use SolrNet's components to skip the response parser. A couple of pointers to do this: