

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.
Table of Contents
Steps 1 : Create java project
Create java project in any editor, here we are using intellij idea.Add following java library in your classpath.
- solr-core-*.jar
- slf4j-api-*.jar
- 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.
Example:
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; import java.io.IOException; /** * Created by JavaDeveloperZone on 5/7/2017. */ public class CustomQueryComponent extends SearchComponent{ private static final Logger LOG = LoggerFactory.getLogger(CustomQueryComponent.class); @Override public void prepare(ResponseBuilder responseBuilder) throws IOException { LOG.info("prepare method of CustomQueryComponent"); } @Override public void process(ResponseBuilder responseBuilder) throws IOException { LOG.info("process method of CustomQueryComponent"); } @Override 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"> </searchComponent>
<requestHandler name="/select" class="solr.SearchHandler"> <lst name="defaults"> <str name="echoParams">explicit</str> <int name="rows">10</int> <bool name="preferLocalShards">false</bool> </lst> <arr name="last-components"> <str>customQueryComponentDemo</str> </arr> </requestHandler>
Step 6 : Testing
Now reload that core and execute following query to check whether our component is called or not.
http://localhost:8984/solr/CustomQueryComponent/select?q=*%3A*&wt=json&indent=true
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