LinqToSolr – use LINQ to obtain data from Solr

Due to the fact that in my company, Solr was chosen as a full-text search platform, there appeared a strong desire to simplify the work with Solr queries using LINQ expressions.

Having surfed the Internet for alternatives, I came to the conclusion that at the moment I do not have the required library to be publicly available. The maximum I managed to find is a very partial implementation of Solr.NET queries (and the skeptic comment of the author himself).

The result was a small LinqToSolr library (a GitHub project), which contains an implementation of the IQueriable <> interface with the ability to convert queries into an understandable Solr API and back.

Implemented enumerable methods

At the moment, the following methods are available:

  • Where
  • First
  • FirstOrDefault
  • Select
  • GroupBy
  • GroupByFacets – an additional method for working with Facets
  • Take
  • Skip
  • OrderBy
  • ThenBy
  • OrderByDescending
  • ThenByDescending

Configuration

It is required to configure the connection and match our data model to the Solr indexes.

Let’s start with the configuration, but first, we will define the representation of the Solr document in the form of a conventional class:

Above is the class describing the Solr index, which contains typed fields Id, Name, Group, Price, and IsDeleted. If there is a strong desire, the JsonProperty property can be used to redefine field names (for example, the Id field).

Create the configuration:

What do we see here? First, we provide the address to Solr.

Next, we specify the projection of our classes to the Solr indexes. Obviously, we can specify as many classes as we like for different indices. The only condition is that one class should be associated with a single index. The minimal configuration is ready.

Initialize the service itself:

That’s all. We met all the conditions to start using Linq to Solr.

Examples of usage

FirstOrDefault method

Where method

Selecting all the documents for the group:

An implementation example of using functions inside LINQ queries:

An example of an array search:

An example of selection with “more-less”:

Using several Where:

Sorting documents

Selecting a certain number

Select method

Working with Facets

In the example above, we request Solr to return 2 groups – Name and Group.

You can also use the GroupBy method. It will return a similar result, but also will add the grouped documents. It is up to you what is better to use. Facets is faster, but it is necessary to make the second request to a server to get a list of documents. GroupBy works slower, because it returns, in addition to groups, the documents already sorted into groups.

Debugging and Validation

In any case, we need to check the request and the response. This can be done using the built-in LastResponse object. In fact, this is the view of the Solr server response. There is also the Url of the request (LastRequestUrl), which we can use in the browser to verify what Solr actually returns.

Service

Of course, using LinqToSolrService is not very convenient. We create our own service inherited from LinqToSolrService.

Summary

I hope someone who uses Solr in .NET projects will find the library useful. At the moment, the most evident queries are implemented.

Daniel Lavrushin

Daniel lives in Copenhagen, Denmark, and works in a major Danish company as a C# developer with more than 10 years of experience in the field.

Latest posts by Daniel Lavrushin (see all)

Daniel Lavrushin

Daniel lives in Copenhagen, Denmark, and works in a major Danish company as a C# developer with more than 10 years of experience in the field.