While switching between one example and another I just realized that each (first) time I had to load and index the sample data manually. That was good for the very first time, but after that, it was just a repetitive work. So I started looking for some helpful built-in thing...and I found it (at least I think): the SolrEventListeners
SolrEventListener is an interface that defines a set of callbacks on several Solr lifecycle events:
- void postCommit()
- void postSoftCommit()
- void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher)
The interesting method is instead newSearcher(...) which allows me to register a custom event listener associated with two events:
- firstSearcher
- newSearcher
When another (i.e new) searcher is opened, it is prepared (i.e. auto-warmed) while the current one still serves the incoming requests. When the new searcher is ready, it will become the current searcher, it will handle any new search requests, and the old searcher will be closed (as soon as all requests it was servicing finish). This scenario is where the "newSearcher" callback is invoked.
As you can see, the callback method for those two events is the same, there's no a "firstSearcher" and a "newSearcher" method. The difference resides in the input arguments: for "firstSearcher" events there's no a currentSearcher so the second argument is null; this is obviously not true for "newSearcher" callbacks where both first and second arguments contain a valid searcher reference.
Returning to my scenario, all what I need is
- to declare that listener in solrconfig.xml
- a concrete implementation of SolrEventListener
<listener event="firstSearcher" class="a.b.c.SolrStartupListener">
<str name="datafile">${solr.solr.home}/sample/data.xml</str>
</listener>
The listener will be initialized with just one parameter, the file that contains the sample data. Using the "event" attribute I can inform Solr about the kind of event I'm interested on (i.e firstSearcher).
The implementation class is quite simple: it extends SolrEventListener:
public class SolrStartupListener implements SolrEventListener
in the init(...) method it retrieves the input argument:
@Override
public void init(final NamedList args) {
this.datafile = (String) args.get("datafile");
}
last, the newSearcher method preloads the data:
LocalSolrQueryRequest request = null;
try {
// 1. Create the arguments map for the update request
final NamedList
args.add(
addEventParms(currentSearcher, args);
// 2. Create a new Solr (update) request
request = new LocalSolrQueryRequest(
// 3. Fill the request with the (datafile) input stream
streams.add(new ContentStreamBase() {
@Override
public InputStream getStream() throws IOException {
return new FileInputStream(datafile);
}
});
request.setContentStreams(streams);
// 4. Creates a new Solr response
// 5. And finally call invoke the update handler
SolrRequestInfo.setRequestInfo(
} finally {
request.close();
}
No comments:
Post a Comment