search component is a feature of search, such as highlighting or faceting. The search component is defined in solrconfig.xml separate from the request handlers, and then registered with a request handler as needed.

Search components define the logic that is used by the SearchHandler to perform queries for users.

There are several default components such as query,facet,stats,debug,expand are available which work with all the search handlers without any additional configuration.

Solr allows usto load custom code to perform a variety of tasks within Solr like custom requesthandler , custom responsewriters,custom analyzers and filter and search components.

In this blog we are going to discuss various steps of how to create custom search component and configure it.

Steps 1 : Create java project

Create java project in any editor, here we are using intellij idea.Add following java library in your classpath.

  1. solr-core-*.jar
  2. slf4j-api-*.jar
  3. slf4j-log4j-api-*.jar

Step 2 : Override SearchComponent class

Solr has inbuilt class called SearchComponent.We must extents this class and override it’s three method prepare , process and description to create custom SearchComponent.


package com.javadevzone.solr;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.SearchComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 * Created by JavaDeveloperZone on 5/7/2017.
public class CustomQueryComponent extends SearchComponent{
    private static final Logger LOG = LoggerFactory.getLogger(CustomQueryComponent.class);
    public void prepare(ResponseBuilder responseBuilder) throws IOException {"prepare method of CustomQueryComponent");
    public void process(ResponseBuilder responseBuilder) throws IOException {"process method of CustomQueryComponent");
    public String getDescription() {
        return "CustomQueryComponent";

In above example we have only print standard method name in log to verify our code.

Step 3 : Make jar

Now compile above code and make jar file and copy that jar file to {SOLR_INSTALL_DIR}/contrib/plugins folder.

Step 4 : Add it into classpath

Now configure our code as plugin,Need to do some changes in solrconfig.xml file as below.

<lib dir="${solr.install.dir:../../../..}/plugins/" regex=".*\.jar" />

Step 5 : Configure as a plugin

Register newly created Search Component and add that component as last component in request handler.

<searchComponent name="customQueryComponentDemo" class="com.javadevzone.solr.CustomQueryComponent">
<requestHandler name="/select" class="solr.SearchHandler">
     <lst name="defaults">
       <str name="echoParams">explicit</str>
       <int name="rows">10</int>
       <bool name="preferLocalShards">false</bool>
    <arr name="last-components">

Step 6 : Testing

Now reload that core and execute following query to check whether our component is called or not.


you will find following entry in solr log file if our custom component called successfully.

2017-05-08 17:07:14.800 INFO  (qtp1769597131-18) [   x:CustomQueryComponent] c.j.s.CustomQueryComponent prepare method of CustomQueryComponent
2017-05-08 17:07:14.823 INFO  (qtp1769597131-18) [   x:CustomQueryComponent] c.j.s.CustomQueryComponent process method of CustomQueryComponent



Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *