On Sep 7, 2008, at 2:04 PM, Brian Whitman wrote:

Hm... I seem to be having trouble getting either the Factory or the Processor to do an init() for me.

The end result I'd like to see is a function that gets called only once, either on solr init or the first time the handler is called. I can't seem to do that.


Here's my code, and a solution I think works -- is there a better way to do this:


public class KeepIndexedDateFactory extends UpdateRequestProcessorFactory implements SolrCoreAware
{

  DataClassIWantToInstantiateOnce data;
  KeepIndexedDateProcessor p;

  public void inform(SolrCore core) {
    data = new DataClassIWantToInstantiateOnce(null);

  }

public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next)
  {
    KeepIndexedDateProcessor p = new KeepIndexedDateProcessor(next);
    p.associateData(data);
    return p;
  }
}


class KeepIndexedDateProcessor extends UpdateRequestProcessor
{
  DataClassIWantToInstantiateOnce data;
  public KeepIndexedDateProcessor( UpdateRequestProcessor next) {
    super( next );
  }

  public void associateData(Data d) { data = d; }

  @Override
  public void processAdd(AddUpdateCommand cmd) throws IOException {
    SolrInputDocument doc = cmd.getSolrInputDocument();
    String id = doc.getFieldValue( "id" ).toString();
    if( id != null ) {
      SolrQuery getIndexedDatesOfId = new SolrQuery();
      getIndexedDatesOfId.setQuery("id:"+id);
      getIndexedDatesOfId.setFields("indexed");
      getIndexedDatesOfId.setRows(1);
      QueryResponse qr = data.query(getIndexedDatesOfId);
      if(qr != null) {
        if(qr.getResults() != null) {
          if(qr.getResults().size()>0) {
Date thisIndexed = (Date)qr.getResults().get(0).getFieldValue("indexed");
            doc.setField("indexed", thisIndexed);
          }
        }
      }
    }
    // pass it up the chain
    super.processAdd(cmd);
  }

}

Reply via email to