In order to connect to the index in Solr that contains documents stored from Content Hub, SolrNet.Microsoft.DependencyInjection
needs to installed to connect and read the documents from the index as below.
Install the SolrNet.Microsoft.DependencyInjection
in your project that will be connecting to Solr
and reading the documents from it.
Create the model containing the properties stored in your Solr
index as below.
using SolrNet.Attributes;
public class ContentHubDocument
{
[SolrField("uniqueKey")]
[SolrUniqueKey]
public string UniqueKey { get; set; }
[SolrField("title")]
public string Title { get; set; }
[SolrField("description")]
public string Description { get; set; }
}
- Register
Solr Url
into Service collection as below, that contains the indexname along with the model defined above.
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using SitecoreCustomExtensions.Models;
using SolrNet;
public class RegisterDependencies : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection.AddSolrNet<ContentHubDocument>("{{SolrUrl}}/solr/{{SolrIndexName}}");
}
}
- Register the above service collection by adding config patch as below.
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<services>
<configurator type="SitecoreCustomExtensions.Pipelines.RegisterDependencies,SitecoreCustomExtensions" />
</services>
</sitecore>
</configuration>
- Then finally you can read the documents stored in Solr through injecting the dependency for
ISolrOperations<ContentHubDocument>
in your repository as below.
using SitecoreCustomExtensions.Models;
using SolrNet;
public class ContentHubRepository : IContentHubRepository
{
private readonly ISolrOperations<ContentHubDocument> _solr;
public ContentHubRepository(ISolrOperations<ContentHubDocument> solr)
{
_solr = solr;
}
public void GetIndexData()
{
string query = "*:*";
var contentHubDocuments = _solr.Query(query);
if (contentHubDocuments != null)
{
// Iterate through solr documents.
foreach(var contentHubDocument in contentHubDocuments)
{
}
}
}
}
Using above way, you will be able to successfully connect to Solr and read the documents stored in it.
You can find more details for initializing the SolrNet on below document.
https://github.com/SolrNet/SolrNet/blob/master/Documentation/Initialization.md
Hope that helps!!! Let me know in case you have any queries.