trong
trong

Reputation: 11

How to create a custom search component in solr

I'm trying to create a custom search component for solr to handle a special request. In that I want to search and check if the result number is empty or not. If it's empty I will try to update search condition and try to search again.

I need to do this because currently I have to query multiple times and check the result number and query again if the result is empty. And it costs a lot request to get the final result.

So I want to do it in solr server, then at client just send 1 request and have the final result.

I have follow this tutorial. Then I check solr log but I didn't see "Hello world" was printed.

Here is my custom class

public class CustomQueryComponent extends SearchComponent {
    private static final Logger LOG = Logger.getLogger(CustomQueryComponent.class);

    @Override
    public void prepare(ResponseBuilder responseBuilder) throws IOException {
        LOG.info("Hello world");
    }

    @Override
    public void process(ResponseBuilder responseBuilder) throws IOException {
        LOG.info("Hello world");
    }

    @Override
    public String getDescription() {
        return "CustomQueryComponent";
    }

    @Override
    public String getSource() {
        return null;
    }
}

And my solrconfig.xml:

//...
    <searchComponent name="customQueryComponentDemo" class="com.company.CustomQueryComponent">
    </searchComponent>

    <requestHandler name="/search" class="solr.SearchHandler">
        <lst name="defaults">
            <str name="echoParams">explicit</str>
            <int name="rows">10</int>
            <bool name="preferLocalShards">false</bool>
        </lst>
        <arr name="first-components">
            <str>customQueryComponentDemo</str>
        </arr>
    </requestHandler>
//...

Solr logs when I access url http://localhost:6280/solr//select?q=%3A&wt=json&indent=true

INFO  - 2018-11-27 14:27:25.206; org.apache.solr.core.SolrCore; [collection] webapp=/solr path=/select params={q=*:*&indent=true&wt=json} hits=9997 status=0 QTime=10
INFO  - 2018-11-27 14:27:28.204; org.apache.solr.core.SolrCore; [collection] webapp=/solr path=/select params={q=*:*&indent=true&wt=json} hits=9997 status=0 QTime=6
INFO  - 2018-11-27 14:27:30.722; org.apache.solr.core.SolrCore; [collection] webapp=/solr path=/select params={q=*:*&indent=true&wt=json} hits=9997 status=0 QTime=5
INFO  - 2018-11-27 14:27:35.080; org.apache.solr.core.SolrCore; [collection] webapp=/solr path=/select params={q=*:*&indent=true&wt=json} hits=9997 status=0 QTime=3

Does anybody know why? I'm looking forward your answers. Thanks a lot! And sorry for not good English.

Upvotes: 0

Views: 1145

Answers (1)

trong
trong

Reputation: 11

I finnaly found out the problerm. In solrconfig.xml, I defined /search request hander using my custom search component. But when I test it, I used /select.


For more information:

You can check in console output if your jar file was loaded successfully or not via grep later lines after the line like this:

INFO  org.apache.solr.core.SolrResourceLoader  û Adding 'file:/C:/Users/../custom-search-component.jar' to classloader

Then goto Solr Admin page > collection_name > Plugins / Stats > QUERYHANDLER > your_custom_handler > description > Search using components. If you see your custom component in this list then it's done. Solr Admin page


Thank you @matslindh for your advice.

Upvotes: 1

Related Questions