Package com.gentics.cr.lucene.didyoumean

Source Code of com.gentics.cr.lucene.didyoumean.DidyoumeanIndexJob

package com.gentics.cr.lucene.didyoumean;

import java.io.IOException;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.spell.CustomSpellChecker;
import org.apache.lucene.search.spell.LuceneDictionary;

import com.gentics.cr.CRConfig;
import com.gentics.cr.CRConfigUtil;
import com.gentics.cr.exceptions.CRException;
import com.gentics.cr.lucene.indexaccessor.IndexAccessor;
import com.gentics.cr.monitoring.MonitorFactory;
import com.gentics.cr.monitoring.UseCase;
import com.gentics.cr.util.indexing.AbstractUpdateCheckerJob;
import com.gentics.cr.util.indexing.IndexLocation;

/**
* This job is used to re-index (or newly index) the didyoumean-index.
*/
public class DidyoumeanIndexJob extends AbstractUpdateCheckerJob {

  private DidyoumeanIndexExtension didyoumean;

  public DidyoumeanIndexJob(CRConfig updateCheckerConfig, IndexLocation indexLoc, DidyoumeanIndexExtension didyoumean) {
    super(updateCheckerConfig, indexLoc, null);

    this.identifyer = identifyer.concat(":reIndex");
    log = Logger.getLogger(DidyoumeanIndexJob.class);
    this.didyoumean = didyoumean;
  }

  /**
   * starts the job - is called by the IndexJobQueue.
   */
  @Override
  protected void indexCR(IndexLocation indexLocation, CRConfigUtil config) throws CRException {
    try {
      reIndex();
    } catch (IOException e) {
      throw new CRException("Could not access the DidYouMean- index! " + e.getMessage());
    }

  }

  private synchronized void reIndex() throws IOException {
    UseCase ucReIndex = MonitorFactory.startUseCase("reIndex()");
    // build a dictionary (from the spell package)
    log.debug("Starting to reindex didyoumean index.");
    IndexAccessor sourceAccessor = didyoumean.getSourceLocation().getAccessor();
    IndexReader sourceReader = sourceAccessor.getReader(false);
    CustomSpellChecker spellchecker = didyoumean.getSpellchecker();
    Collection<String> fields = null;

    if (didyoumean.isAll()) {
      fields = sourceReader.getFieldNames(IndexReader.FieldOption.ALL);
    } else {
      fields = didyoumean.getDym_fields();
    }
    try {
      for (String fieldname : fields) {
        LuceneDictionary dict = new LuceneDictionary(sourceReader, fieldname);
        spellchecker.indexDictionary(dict);
      }
    } finally {
      if (sourceAccessor != null && sourceReader != null) {
        sourceAccessor.release(sourceReader, false);
      }
    }
    log.debug("Finished reindexing didyoumean index.");
    ucReIndex.stop();
  }

}
TOP

Related Classes of com.gentics.cr.lucene.didyoumean.DidyoumeanIndexJob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.