r 8289 change for index update

This commit is contained in:
anupsawant 2011-06-27 21:44:25 +00:00
parent e41f90a653
commit 5136b7c095

View file

@ -7,14 +7,19 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Iterator;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.*;
import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer;
import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.search.IndexingException; import edu.cornell.mannlib.vitro.webapp.search.IndexingException;
import edu.cornell.mannlib.vitro.webapp.search.docbuilder.Obj2DocIface; import edu.cornell.mannlib.vitro.webapp.search.docbuilder.Obj2DocIface;
@ -36,43 +41,40 @@ public class SolrIndexer implements IndexerIface {
@Override @Override
public void index(Individual ind) throws IndexingException { public void index(Individual ind) throws IndexingException {
if( ! indexing ) if( ! indexing )
throw new IndexingException("SolrIndexer: must call " + throw new IndexingException("SolrIndexer: must call " +
"startIndexing() before index()."); "startIndexing() before index().");
if( ind == null ) if( ind == null )
log.debug("Individual to index was null, ignoring."); log.debug("Individual to index was null, ignoring.");
try{ try{
if( urisIndexed.contains(ind.getURI()) ){ if( urisIndexed.contains(ind.getURI()) ){
log.debug("already indexed " + ind.getURI() ); log.debug("already indexed " + ind.getURI() );
return; return;
}else{ }else{
SolrInputDocument solrDoc = null; SolrInputDocument solrDoc = null;
synchronized(this){ synchronized(this){
urisIndexed.add(ind.getURI()); urisIndexed.add(ind.getURI());
} }
log.debug("indexing " + ind.getURI()); log.debug("indexing " + ind.getURI());
// synchronized(individualToSolrDoc){
solrDoc = individualToSolrDoc.translate(ind); solrDoc = individualToSolrDoc.translate(ind);
// }
if( solrDoc != null){ if( solrDoc != null){
//sending each doc individually is inefficient UpdateResponse res = server.add( solrDoc );
// Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); log.debug("response after adding docs to server: "+ res);
// docs.add( solrDoc ); }else{
UpdateResponse res = server.add( solrDoc ); log.debug("removing from index " + ind.getURI());
log.debug("response after adding docs to server: "+ res); //TODO: how do we delete document?
}else{ //writer.deleteDocuments((Term)obj2doc.getIndexId(ind));
log.debug("removing from index " + ind.getURI()); }
//TODO: how do we delete document? }
//writer.deleteDocuments((Term)obj2doc.getIndexId(ind)); } catch (IOException ex) {
} throw new IndexingException(ex.getMessage());
} } catch (SolrServerException ex) {
} catch (IOException ex) { throw new IndexingException(ex.getMessage());
throw new IndexingException(ex.getMessage()); }
} catch (SolrServerException ex) {
throw new IndexingException(ex.getMessage());
}
} }
@Override @Override
@ -95,7 +97,7 @@ public class SolrIndexer implements IndexerIface {
@Override @Override
public synchronized void startIndexing() throws IndexingException { public synchronized void startIndexing() throws IndexingException {
while( indexing ){ //wait for indexing to end. while( indexing ){ //wait for indexing to end.
log.debug("LuceneIndexer.startIndexing() waiting..."); log.debug("SolrIndexer.startIndexing() waiting...");
try{ wait(); } catch(InterruptedException ex){} try{ wait(); } catch(InterruptedException ex){}
} }
@ -149,8 +151,24 @@ public class SolrIndexer implements IndexerIface {
@Override @Override
public long getModified() { public long getModified() {
// TODO Auto-generated method stub long modified = 0;
return 0;
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.addSortField("indexedTime", SolrQuery.ORDER.desc);
try {
QueryResponse rsp = server.query(query);
SolrDocumentList docs = rsp.getResults();
if(docs!=null){
modified = (Long)docs.get(0).getFieldValue("indexedTime");
}
} catch (SolrServerException e) {
// TODO Auto-generated catch block
log.error(e,e);
}
return modified;
} }
public boolean isIndexEmpty() { public boolean isIndexEmpty() {