Integrate Solr Instance With .NET Core

In this post, we will learn to integrate Solr instance with .net core application.

We will first introduce Solr based client-server architecture to understand how data should reside for Solr based search support. Then we will setup a Solr instance to run on a Docker container.

Finally, we will write code to connect to this container from a .net core application.

Introduction To Solr

Solr is a popular and highly efficient search engine platform. It was written in Java and is open sourced by the Apache Lucene project.

Solr is a standalone enterprise search server with a REST-like API. You put documents in it (called “indexing”) via JSON, XML, CSV or binary over HTTP. You query it via HTTP GET and receive JSON, XML, CSV or binary results.

Some Benefits Of Using Solr

  • Full-text search
  • High volume search support
  • REST API based query support
  • Advanced Admin interface
Solr Features
Solr Features

Solr Search Based Client-Server Architecture

In a typical client-server based architecture, here’s how things usually work.

A Typical Client-Server Architecture
A Typical Client-Server Architecture

A web app or a web API receives HTTP requests that are then converted into CRUD operations by the server that operate on the underlying database.

Now, with the introduction of Solr search engine, things are going to look a bit different.

A Solr Based Archietecture
A Solr Based Architecture

Data Duplication

The first thing that we need to consider when integrating with Solr is that our data is going to be duplicated. The data resides in the persistent database as well as in the Solr core.

Of course we will not duplicate the entire data from the database. Only the data that should queried by your application’s search engine should be stored on the Solr core.

Consequence Of Data Duplication

Now in order to make sure that the needed application data resides in the Solr core, we need to make sure that the database and Solr core stays in sync all the time.

So, every add, delete and update operations that you perform on the database must be passed over to the Solr as well.

Performing Search Operation With Solr

In the absence of Solr search engine, we queried data directly on the underlying database. Now, with Solr, we perform all the queries on the Solr instance.

Become A Flutter Expert With This Course

Depending upon your data structure, you might need to query further on the database as well based on the results of Solr query. However, the subsequent queries on the database would only be a basic select queries.

All the advanced and complex queries should be performed on the Solr search engine.

Run Solr Container Via Docker Compose

Create a service in your docker compose for creating a Solr instance and expose it on port 8983.

Here, we have specified that we want to pre-create a solr core “my_core” whenever the docker creates a solr instance. A core is something similar to a database which is used to refer a single index in Solr.

After you run the docker compose up command, you should be able to access Solr admin panel from your machine.

If you are running the app on your local machine, the hostname is going to be localhost. On windows machine, you might have to put in your docker-machine’s ip which you can get via command:

You can do all sorts of things from the admin panel like querying data and analyzing requests. In this post however, we are going to focus on connecting to our Solr instance via a .NET core application.

Setup To Integrate Solr Instance With .NET Core Application

Solr Client Nuget Package For .NET Core

We will be making use of the SolrNet package which is a Solr client for .NET applications. Add reference to this library in your .net core application.

Defining Solr Document Schema

In Solr, the basic unit of data is called a document which is a set of data that describes something. You can think of a document as something like a table row in a RDBMS. Each document has many fields. For example, a Contact document can contain fields like name, number and address.

We will define the document schema via a data model class.

Let’s say we have a database entity called Post which looks like this below:

A Solr document data model for this same class would look like this:

Basically, we create fields for only those items that are going to be queryable later on.

Perform Add, Update And Delete On Solr Index

We have a working Solr document model now. Next we need a way to perform operations like add, update and delete with this model on the Solr instance.

We perform such operations with the help of ISolrOperations which exposes all possible Solr operations.

The SolrIndexService is a generic class which can be re-used for any Solr document model.

Now, every time you create a new object in your database, you should add a document in the Solr instance as well.

You can repeat similar implementations for update and delete operations.

Inject SolrIndexService From Startup In .NET Core

In the example above we had assumed that the SolrIndexService is going to be injected in the service layer. We can configure this from Startup class in a .net core application.

Wrapping Up

This concludes the part of connecting our Solr instance with a .NET core application. We created a Solr instance that runs on a Docker container. Then we created a data model to represent Solr document. Finally, we created methods like add and delete to ensure that the data between database and Solr instance stays in sync.

In the next part, we will see how we can integrate Solr search in our application.

Next Part: Coming Soon!

Check out our other .NET Related Articles as well.

About the author