I recently accepted the challenge of creating a new search component (also known as a plug-in) for Solr – a powerful open source search engine. This was both very exciting and extremely daunting as I had never used Solr before. This meant A LOT of reading and A LOT of trial and error!
This blog post is intended to help anyone else who has encountered the same difficulties as me and demonstrate that creating a Solr plug-in is actually quite simple! The following content will focus on the 5 steps required to setup a simple search component using Solr 5 – I have included some code snippets to help make things easy.
Step 1. Using Eclipse – or your preferred Java IDE – set up a new project and make sure you have downloaded and included the relevant Solr JARs. These are the JARs that I used:
Step 2. In order to start working on your component you need to create a concrete class that extends ‘SearchComponent’; this requires the following methods to be overridden: getDescription(), prepare(ResponseBuilder arg0), and processResponseBuilder(arg0).
N.B. One of the mistakes I made was that I started adding my code to the process method without fully understanding when it was being called. So to help you understand I will briefly explain: each component’s prepare method happens before anything else. The process methods happen after ALL components prepare methods are finished, i.e. editing the query in the process method is pointless! As I needed to manipulate the actual query I had to do this in the prepare method to ensure it was happening before anything else – it seems obvious now!
Step 3. Now that I’ve covered the setup of your plug-in, the next step is to test that everything works together before writing a lot of complex code (it’s tempting to dive right in…). Simply add a ‘Hello World’ print statement into the prepare method, then compile your class into a jar file for adding into the Solr directory. I added mine to a plugins folder I created in [solr install directory]/contrib.
Step 4. In order to make your plugin available in Solr, you will need to make some changes in your solrconfig.xml file. This is what I added to mine:
- The classpath to your plugin folder
- Register the custom component, you can name it anything you want but make sure you reference your class’s package correctly
- In your request handler you will need to add the component as a first component (explained below)
You can place components either at the beginning or the end of the default components. The beginning components are called first-components, they occur before the default components which is where this plug-in will go as it’s affecting the query before the search.
Step 5. To test, I went to the Solr Admin page, checked the logs to ensure it built correctly and then ran a query to see if my “Hello world” statement printed.
That’s all there is to writing a Solr plug-in; piece of cake right?! Hopefully this blog has helped those of you who are looking to create your first Solr plug-in and filled in any gaps that other online sources haven’t provided. I personally found that the hardest part was just getting started. Once the plug-in was setup the majority of the coding was a breeze.
Here are some resources I personally found helpful when wanting to learn more about Solr:
- Solr in Action book by Manning Publications: https://www.manning.com/books/solr-in-action?a_bid=39472865&a_aid=1
- Solr Wiki: https://wiki.apache.org/solr/
- And of course Google/Stack Overflow!!