In this blog post I will show you how to implement a custom SOLR Core and use it with a Sitecore API. First, let’s talk a little about the reasons for using SOLR. As we know SOLR is a search engine used for searching in content and operational databases that Sitecore uses. It’s fast, configurable and robust, especially when you work with a large number of Sitecore items.
Now we need to make our custom SOLR Core indexed with items from Sitecore that we want to have in it, and at the end use that information via Sitecore API calls.
Creating SOLR Core
Here we can see my SOLR admin page with all cores listed. Now we can navigate to the Add Core button, define all parameters and create a new core, but I have in mind something way simpler.
What we are going to do is call a specific URL, which will then create our new Core and define the configset.
Simple as that. Let’s take a look what are the parts of this URL:
- Address of our SOLR Instance
- Command (CREATE, DELETE)
- Name of our SOLR Core
- Instance Dir of our SOLR Core
- Configset that our SOLR Core will use
It’s all strait forward, but I will talk about configsets later in this article, as they are very interesting to look at.
Now, our SOLR Core is created and we should receive a message like this:
Again, if we look at the SOLR admin page we should see our core under the Core Admin page:
Now that we have our SOLR Core online, we can see numDocs value is 0, which means it doesn’t have any indexes in itself, it’s empty. We should do a rebuilding of an index, but we will first check our configuration files to see if we need to change anything there.
Where can we found SOLR configuration files?
Configsets are located in our SOLR installation folder under …/server/solr/ alongside with dir’s for all SOLR Cores.
We can see here that we have a dir for our MY_NEW_CUSTOM_CORE called MY_NEW_CUSTOM_CORE_DIR.
If we look at the top we can see two folders, both folders for configsets. The first one, basic_configs, is basic SOLR config files. We could use that config, but we would need to change a lot of settings inside the config files. Inside the configsets folder we have multiple configurations.
Again, we can see here multiple configurations for various SOLR Core types that we have. We are interested in Sitecore so we would choose sitecore_configs. You can see that we also have two custom configsets, in which we have custom settings for some of our Cores, but we will get to that later on.
Now, let’s go to our SOLR Core dir and let’s take a look what we have there.
- Data folder – by default, Solr stores its index data in this directory
- core.properties – this is a properties file where each line is just a key=value pair
This is a somewhat minimal properties file. We can see the name of your SOLR Core and configset that this Core is using. We are using sitecore_configs config, but as I mentioned before, we can have a custom config in configsets. Then we would change this property here to point to our custom config.
Now, let’s get a little bit more in details to our Core config files.
The files that we are interested in are:
This file contains all of the details about which fields your item can contain and how those fields should be dealt with when adding item to the index.
This is the file that contains most of the parameters for configuring SOLR itself. For example if we want to add a custom search to SOLR, we would add a searchcomponent in this file with custom properties.
We now have pretty much everything set-up from the point of SOLR. We got our empty custom SOLR Core, we have our configurations file pointed correctly and in SOLR Admin Panel we shouldn’t see any errors (both on the top label and in the SOLR log).
In our Sitecore project we should first add a .config file for our custom SOLR Core. Navigate to /App_Config/Include/ folder and add a new config file. I called mine:
Inside I have these settings:
I have marked some interesting parts in this config file:
- Index name – name must be the same as it is in SOLR
- Configuration name
- Database we want to crawl and root path we want to take
This configuration will connect our Sitecore Item and SOLR during rebuilding of indexes. All settings from this configuration file will be taken in during that process.
Next we need an additional configuration file inside our project for this core also. I’ve called mine:
We can define which templates we want to include in our index, we can add a custom computed field into our index and connect it to a class inside our project. There are a lot of settings in this file and you should customize them to your needs.
The last step is to rebuild our index in Sitecore’s Index Manager. Go to Control Panel > Indexing Manager
After the rebuilding is done we should have more than 0 doc’s in our core.
How to call SOLR Index from Sitecore API?
We have made a controller inside our project which we can call from frontend of our application. We are going to do a simple search of our index based on some search term.
- Our method for calling to SOLR using Sitecore API. We are using SearchResultItem list, as this is a Sitecore default model.
- We need to get our SOLR index by using ContentSearchManager and a method named GetIndex
- Then we need to create a Search Context
- Here we are building our query and we are using LINQ for that. We will search for our search term in item’s content.
- With GetResults() method we will fill up the list with all the results from defined SOLR index based on our query.
- Here we are just checking if our result list has any hits (Hits is number which shows us how many, if any, results we have retrieved from SOLR Index.
The next step would be to process our retrieved results and use them in a way we need to in our application.