Package index

Source Code of index.IndexBuilder

package index;

import java.io.IOException;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.springframework.stereotype.Component;

import parse.LuceneConfiguration;
import source.SourceReader;
import value.Row;

/**
* Class responsible for creating an index for the given configuration
*
* @author rahul
*
*/
@Component
public class IndexBuilder {
    private static Log log = LogFactory.getLog(IndexBuilder.class);
    @Resource
    private LuceneConfiguration luceneConfiguration;
    @Resource
    private SourceReader<Row> sourceReader;

    /**
     * Method responsible for creating the index.
     */
    public void buildIndex() {
        try {
            IndexWriter indexWriter = getWriter();
            addDocuments(indexWriter);
            indexWriter.close();
        } catch (Exception e) {
            log.error("error in buiding index", e);
        }
    }

    private void addDocuments(IndexWriter indexWriter) throws Exception {
        sourceReader.setSeparator(luceneConfiguration.getSeparator());
        while (sourceReader.hasNext()) {
            Row row = sourceReader.next();
            indexWriter.addDocument(createDocument(row));
        }
    }

    private Document createDocument(Row row) {
        Document document = new Document();
        Set<String> fields = luceneConfiguration.getPerFieldInfo().keySet();
        for (String fieldName : fields) {
            boolean isStore = luceneConfiguration.getPerFieldInfo().get(fieldName).isStore();
            Store store = getStore(isStore);
            Field field = new Field(fieldName, row.get(fieldName), store, Index.ANALYZED, TermVector.NO);
            document.add(field);
        }
        return document;
    }

    private Store getStore(boolean isStore) {
        Store store = Store.NO;
        if (isStore) {
            store = Store.YES;
        }
        return store;
    }

    private IndexWriter getWriter() throws CorruptIndexException, LockObtainFailedException, IOException {
        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(luceneConfiguration.getIndexLocation()), new WhitespaceAnalyzer(),
                luceneConfiguration.isOverWrite(), IndexWriter.MaxFieldLength.UNLIMITED);
        return indexWriter;
    }
}
TOP

Related Classes of index.IndexBuilder

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.