Once you have created your document class, you have to initialize the library in order to operate against the Solr instance. This is usually done once at application startup:
Startup.Init<Product>("http://localhost:8983/solr");
Then you ask the service locator for the SolrNet service instance which allows you to issue any supported operation:
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
await solr.DeleteAsync(SolrQuery.All);
await solr.AddAsync(p);
await solr.CommitAsync();
var products = await solr.QueryAsync(new SolrQueryByRange<decimal>("price", 10m, 100m));
Install-Package SolrNet.Microsoft.DependencyInjection
To use, with an IServiceCollection
instance and one or more assemblies:
services.AddSolrNet("http://localhost:8983/solr");
services.AddSolrNet<Person>("http://localhost:8983/solr/person");
Install-Package SolrNet.Windsor
Alternatively, if your app uses Castle Windsor you can set up SolrNet using the included facility:
container.AddFacility("solr", new SolrNetFacility("http://localhost:8983/solr"));
Or using Windsor's xml config:
<castle>
<facilities>
<facility id="solr" type="Castle.Facilities.SolrNetIntegration.SolrNetFacility, SolrNet">
<solrURL>http://localhost:8983/solr</solrURL>
</facility>
</facilities>
</castle>
If you're using Ninject, you can use the Ninject module:
Install-Package SolrNet.Ninject
kernel.Load(new SolrNetModule("http://localhost:8983/solr"));
Install-Package SolrNet.StructureMap
If you are using StructureMap, you can use the StructureMap module (StructureMap.SolrNetIntegration
):
IEnumerable<SolrServer> solrServers = new[]
{
new SolrServer(id: "test", url: "http://localhost:8893", documentType: "testDocumentType")
};
var container = new StructureMap.Container(SolrNetRegistry.Create(solrServers));
Install-Package SolrNet.Autofac
var builder = new ContainerBuilder();
builder.RegisterModule(new SolrNetModule("http://localhost:8983/solr"));
var container = builder.Build();
Install-Package SolrNet.Autofac
var solrServers = new SolrServers {
new SolrServerElement {
Id = "test",
Url = "http://localhost:8893",
DocumentType = typeof (Entity).AssemblyQualifiedName,
}
};
container = new UnityContainer();
new SolrNetContainerConfiguration().ConfigureContainer(solrServers, container);
Install-Package SolrNet.SimpleInjector
Create or use an existing SimpleInjector Container and add Solr by passing its URL:
var container = new SimpleInjector.Container();
container.AddSolrNet("http://localhost:8983/solr");
If you need to map multiple Solr cores/instances, see this page.