Solrnet Example



Example: val:'recip(rord(myfield),1,2,3)' Nested query support for any type of query parser (via QParserPlugin). Quotes will often be necessary to encapsulate the nested query if. 2.Compile the project and get the dlls from SOLRNET project (there will be SolrNet.dll, Microsoft.Practices.ServiceLocation.dll, HttpWebAdapters.dll and the pdb and xml files) 3.Create an empty ASP.NET project.

I got a very relevant question on one of my prior Blog posts:

Examples

“I saw you mentioning using SolrNet but i thought ContentSearch was using

SolrNet underneath.what do i gain using one vs another?”

This post shares some of my experiences and thought’s on the above question.

First of all, “Yes”, Sitecore ContentSearch uses SolrNet when you choose to use Solr instead of Lucene. So if you are familiar with the Content Search API, you don’t have to worry about it really; you just continue to do what you normally do.

If you dive in a little deeper there are some differences in flexibility and potentially performance. That’s really no surprise since – I see the ContentSearch to be more abstract as it hides some of the specifics of Solr. But say you have a very high performance website, and you have to tune and configure Solr – the actual Solr queries will impact performance.

Let’s look at an example. Say that we have some “Article” items in Sitecore with Title, Body and Category fields. The Category is a Multilist. Below examples on how to do such a query using ContentSearch as well as SolrNet directly to find all Articles in a specific category.

ContentSearch API

The following Content Search query:

Results in a Solr Query like this:

SolrNet

Example

The following SolrNet Query:

Results in a Solr Query like this:

Solrnet Cloud Example

SolrNet

So the difference is around the use of Solr “FilterQueries” – the fq parameter. Sitecores ContentSearch API converts it into the use of logical AND, where the use of SolrNet directly converts it into separate filter queries.

So the question is what difference does this make?

Solrnet facet query example

If you have millions of items in Solr that you query – there will be a difference. The major difference is that with Filter Queries Solr can utilize a FilterCache. If you have a large index, uses a lot of facets and queries it can make a big difference in performance. Also – the filter cache can be configured with more dedicated memory if needed.

Example

Solrnet Example Meaning

What to use?

Depends on the specific solution – as always. It’s easy to use the ContentSearch API; so stick to that unless you know that you are working with a lot of data. In some cases, we do both – e.g. if we need a specific Solr function such as “MoreLikeThis”. SolrNet implements this directly and is easy to use for such a special functionality.

An obvious question would be if one can extend the existing ContentSearch API – and force it to utilize fq instead? That could be a challenge worth for another Blog post.

Relevant links

UPDATE 2/19/2009: by now most of this is obsolete, please check out more recent releases

Last month I've been working my a** off integrating Solr to our main site. The first step was to find out how to communicate with the Solr server. Naturally, I came to SolrSharp. But I found it to be really IoC-unfriendly: lots of inheritance, no interfaces, no unit-tests, so it would have been a real PITA to integrate it to Castle. So, instead of wrapping it, I built SolrNet.

Solrnet Example Sample

Before explaining how it works, a disclaimer: I'm a complete newbie to Solr, Lucene and full-text searching in general. The code works on my machine and does what I need it to do for the task that I have at hand. This project is not, and might never be, feature complete like SolrSharp. Currently it doesn't support facets (UPDATE 8/20/08: I added facet support) or highlights, and maybe some other stuff. If you absolutely need those features right now, either use SolrSharp or write a patch for SolrNet. However, the next step in the integration is implementing faceted search, so I will definitely implement facets sooner or later.

Solrnet Indexing Example

Usage

Solrnet Example Paragraph

First we have to map the Solr document to a class (Solr supports only one document type per instance at the moment). Let's use a subset of the default schema that comes with the Solr distribution:

It's just a POCO with a marker interface (ISolrDocument)[1] and some attributes: SolrField maps the attribute to a Solr field and SolrUniqueKey (optional) maps an attribute to a Solr unique key field. Let's add a document (make sure you have a running Solr instance first):

Solrnet Facet Query Example

Let's see if the document is there:

For more examples, see the tests.

DSL

Since DSLs are such a hot topic nowadays, I decided to give it a try to see what happened. I just defined the syntax I wanted in a test, then wrote the interfaces to comply to the syntax and chain the methods, then built the implementations for those interfaces. The result is pretty much self-explanatory:

Solrnet Example C#

Run() is the explicit kicker method[1]. The DSL is defined in a separate DLL, in case you don't want/need it. There are some more examples in the tests.

I TDDd most of the project, so the code coverage is near 75%. I'll add the remaining tests if/when I have the time. Of course, as usual, patches/bugfixes are more than welcome :-)

[1] I might drop this requirement in the future.