Search code examples
.netjsonsolrsolrnet

Is it possible to convert SolrNet QueryOptions to Json string?


I am sending solr request from my web application to the Solr using SolrNet using below code:

SolrQueryResults<MyClass> results= solrCustomWorker.Query(new SolrQuery(query), options);

where options value is like:

options = new QueryOptions
                        {
                            Rows = pageSize,
                            Start = (pageIndex - 1) * pageSize,
                            FilterQueries = _solrQuery.ToArray(),
                            OrderBy = new[] { new SolrNet.SortOrder("NameCopy", SolrNet.Order.ASC) },
                            Facet = new FacetParameters
                            {
                                Queries = _solr.ToArray(),
                                MinCount = minCount,
                            },
                            Stats = statsParams
                        };

Now I want to add object caching for this request so that if same request is being processed then it will fetch value from Caching instead of again sending the request to the Solr.

Can anyone please suggest How can I include options value in my cachekey? As options have facets, filterqueries, Extraperms Fields etc. and these values are in array format.

Can I convert options value in json string? Then add it to my key value.


Solution

  • I have convert is using below code:

     using System.Web.Script.Serialization;  
    
      //code
     string json = new JavaScriptSerializer().Serialize(options);
     string solrRequestCacheKey = "solrRequestCacheKey-" + query + "-" + json;
     var policy = new CacheItemPolicy();
     SolrQueryResults<MyClass> temp;
     if (CustomCache[solrRequestCacheKey] == null)
      {
         temp = solrCustomWorker.Query(new SolrQuery(query), options);
         CustomCache.Add(solrRequestCacheKey, temp, policy);
      }
      results = (SolrQueryResults<MyClass>)CustomCache[solrRequestCacheKey]; 
    

    Using Serialization I can successfully added options value to my cachekey.