Fixing lucene index startup so that it does not build a new index if not needed NIHVIVO-1701
This commit is contained in:
parent
f45eafa3f4
commit
78d96c3afa
2 changed files with 19 additions and 22 deletions
|
@ -20,10 +20,9 @@ import org.apache.lucene.index.CorruptIndexException;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.TermDocs;
|
|
||||||
import org.apache.lucene.index.TermEnum;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.FSDirectory;
|
import org.apache.lucene.store.FSDirectory;
|
||||||
|
import org.apache.lucene.store.LockObtainFailedException;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -133,7 +132,7 @@ public class LuceneIndexer implements IndexerIface {
|
||||||
this.currentOffLineDir = offLineDir;
|
this.currentOffLineDir = offLineDir;
|
||||||
writer = new IndexWriter(offLineDir, analyzer, true, MAX_FIELD_LENGTH);
|
writer = new IndexWriter(offLineDir, analyzer, true, MAX_FIELD_LENGTH);
|
||||||
}else{
|
}else{
|
||||||
writer = new IndexWriter(this.liveIndexDir, analyzer, false, MAX_FIELD_LENGTH);
|
writer = getLiveIndexWriter(false);
|
||||||
}
|
}
|
||||||
indexing = true;
|
indexing = true;
|
||||||
urisIndexed = new HashSet<String>();
|
urisIndexed = new HashSet<String>();
|
||||||
|
@ -340,13 +339,16 @@ public class LuceneIndexer implements IndexerIface {
|
||||||
log.error("indexing should not be set to true just yet");
|
log.error("indexing should not be set to true just yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IndexWriter getLiveIndexWriter(boolean createNew) throws CorruptIndexException, LockObtainFailedException, IOException{
|
||||||
|
return new IndexWriter(this.liveIndexDir, analyzer, createNew, MAX_FIELD_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized void makeEmptyIndexIfNone() throws IOException {
|
private synchronized void makeEmptyIndexIfNone() throws IOException {
|
||||||
if( !liveIndexExists() ){
|
if( !liveIndexExists() ){
|
||||||
log.debug("Making new index dir and initially empty lucene index at " + liveIndexDir);
|
log.debug("Making new index dir and initially empty lucene index at " + liveIndexDir);
|
||||||
closeWriter();
|
closeWriter();
|
||||||
makeIndexDirs();
|
makeIndexDirs();
|
||||||
writer = new IndexWriter(liveIndexDir,analyzer,true,MAX_FIELD_LENGTH);
|
writer = getLiveIndexWriter(true);
|
||||||
closeWriter();
|
closeWriter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -406,16 +408,12 @@ public class LuceneIndexer implements IndexerIface {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isIndexEmpty() throws CorruptIndexException, IOException{
|
public boolean isIndexEmpty() throws CorruptIndexException, IOException{
|
||||||
TermDocs td = null;
|
IndexWriter writer = null;
|
||||||
try{
|
try{
|
||||||
IndexReader reader = IndexReader.open(new File( this.liveIndexDir ));
|
writer = getLiveIndexWriter(false);
|
||||||
td = reader.termDocs(new Term( Entity2LuceneDoc.VitroLuceneTermNames.DOCID) );
|
return writer.numDocs() == 0;
|
||||||
if( td.next() )
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return true;
|
|
||||||
}finally{
|
}finally{
|
||||||
if (td != null) td.close();
|
if (writer != null) writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,10 +70,9 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
|
||||||
public void contextInitialized(ServletContextEvent sce) {
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
try {
|
try {
|
||||||
ServletContext context = sce.getServletContext();
|
ServletContext context = sce.getServletContext();
|
||||||
log.debug("**** Running " + this.getClass().getName() + ".contextInitialized()");
|
|
||||||
|
|
||||||
String baseIndexDir = getBaseIndexDirName();
|
String baseIndexDir = getBaseIndexDirName();
|
||||||
log.info("Base directory of lucene full text index: " + baseIndexDir);
|
log.info("Seting up Lucene index. Base directory of lucene index: " + baseIndexDir);
|
||||||
|
|
||||||
setBoolMax();
|
setBoolMax();
|
||||||
|
|
||||||
|
@ -99,10 +98,10 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
|
||||||
indexer.setLuceneIndexFactory(lif);
|
indexer.setLuceneIndexFactory(lif);
|
||||||
|
|
||||||
if( indexer.isIndexCorroupt() ){
|
if( indexer.isIndexCorroupt() ){
|
||||||
log.info("index is corrupt, requesting rebuild");
|
log.info("lucene index is corrupt, requesting rebuild");
|
||||||
}
|
}
|
||||||
if( indexer.isIndexEmpty() ){
|
if( indexer.isIndexEmpty() ){
|
||||||
log.info("index is empty, requesting rebuild");
|
log.info("lucene index is empty, requesting rebuild");
|
||||||
sce.getServletContext().setAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP, Boolean.TRUE);
|
sce.getServletContext().setAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP, Boolean.TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,21 +137,21 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
|
||||||
|
|
||||||
if( (Boolean)sce.getServletContext().getAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP) instanceof Boolean &&
|
if( (Boolean)sce.getServletContext().getAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP) instanceof Boolean &&
|
||||||
(Boolean)sce.getServletContext().getAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP) ){
|
(Boolean)sce.getServletContext().getAttribute(INDEX_REBUILD_REQUESTED_AT_STARTUP) ){
|
||||||
log.info("Rebuild of search index required before startup.");
|
log.info("Rebuild of lucene index required before startup.");
|
||||||
builder.doIndexRebuild();
|
builder.doIndexRebuild();
|
||||||
Thread.currentThread().sleep(500);
|
Thread.currentThread().sleep(500);
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while( builder.isReindexRequested() || builder.isIndexing() ){
|
while( builder.isReindexRequested() || builder.isIndexing() ){
|
||||||
n++;
|
n++;
|
||||||
if( n % 20 == 0 ) //output message every 10 sec.
|
if( n % 20 == 0 ) //output message every 10 sec.
|
||||||
log.info("Still rebulding search index");
|
log.info("Still rebulding lucene index");
|
||||||
Thread.currentThread().sleep(500);
|
Thread.currentThread().sleep(500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("**** End of " + this.getClass().getName() + ".contextInitialized()");
|
log.info("Setup of Lucene index completed.");
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error("***** Error setting up Lucene search *****", t);
|
log.error("***** Error setting up Lucene index *****", t);
|
||||||
throw new RuntimeException("Startup of vitro application was prevented by errors in the lucene configuration");
|
throw new RuntimeException("Startup of vitro application was prevented by errors in the lucene configuration");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue