Mike Nitchie Mike Nitchie - 4 months ago 60x
Java Question

Solr/Solrj pagination

I am using solr and solrj for index and search functionality in a web app I am creating. My request handler is configured in solrconfig.xml as follows:

<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<str name="start">0</str>
<int name="rows">10</int>
<str name="defType">edismax</str>
<str name="qf">
title^10.0 subtitle^7.0 abstract^5.0 content^1.0 text^1.0
<str name="pf">
title^10.0 subtitle^7.0 abstract^5.0 content^1.0 text^1.0
<str name="df">text</str>


As it stands, the indexing and searching works well. However, I want to implement pagination. The config file contains "start" and "row" data. However, in solrj, when I run:

SolrQuery query = new SolrQuery(searchTerm);

The three print statements each show null. I understand each of those 'gets' has a correspond 'set', but I would have imagined that they would be already set via the response handler in the solrconfig.xml. Can someone clue me in?


Before executing the query on the server, the client would not know about what you have set on the server side, right? So it is not a surprise that they are all null.

To implement pagination you need two parameters from the client side - the page number and the number of items per page. Once you got these two, you can construct your SolrQuery on the client side as follows:

SolrQuery query = new SolrQuery(searchTerm);
query.setStart((pageNum - 1) * numItemsPerPage);
// execute the query on the server and get results
QueryResponse res = solrServer.query(solrQuery);