Two Clever Ways to Get Data Directly from Solr
Solr is used to power both operational and custom search indexes in Sitecore, but there are times in which you may want to access this data outside the normal Sitecore usage. A few examples are as follows:
- Consuming Web item data in a mobile application without having an Experience Edge or Sitecore Headless environment
- Reporting tools for tracking specific types of items such as products
- Viewing a total count of items
You may have other needs, but the bottom line is that there are a few methods to directly query Solr data without going through your Sitecore endpoints or using headless… bearing in mind that if you decide to make substantial requests to Solr you could impact your Sitecore environment so make sure to properly scale and load test any solution. You should also have adequate security in place (ex. do not expose Solr publicly and ensure secure communication between your application and Solr while treating Solr like a backend application).
SolrNet
SolrNet is a Solr client for use with .NET applications and available as a NuGet Package. There are several options with the most common being SolrNet.Core. Note that Sitecore uses SolrNet for its communication with Solr (check your bin directory).
I have had mixed results with their SolrCloud packages. The link to the SolrNet GitHub repository is as follows: GitHub – SolrNet/SolrNet: Solr client for .Net
If you leverage Docker for local development, they offer a sample MVC app for full text search and faceting: GitHub – SolrNet/SolrNetSampleApps: Sample application using SolrNet
Directly Call Solr Rest Endpoints
If the constraints of a library like SolrNet are not to your liking, you can directly call Solr Rest endpoints. In the following URL, I am getting all the products in the Web index that match the Product Template in the English language.
https://mysolr.local:8984/solr/sitecore_web_index/select?q=((_templatename%3A(“Product Template”) AND _language%3A(“en”)))
Of note, I obtained this URL by:
- Opening the Solr GUI and browsing to the desired index
- Running a query with the q field defined for ((_templatename:(“Product Template”) AND _language:(“en”)))
- Copying the URL that is displayed above the returned data
With this endpoint, I can now run a fetch operation in my programming language of choice to query the returned data as JSON and select what information I want to display. It is also recommended to create a model for the data you are querying as Solr will send a lot of data back.
The advantage of the approach above is that you can send filtered Solr queries to output the desired data set without having dependencies from a library like SolrNet.