Example usage for org.apache.lucene.index IndexWriter IndexWriter

List of usage examples for org.apache.lucene.index IndexWriter IndexWriter

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter IndexWriter.

Prototype

public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException 

Source Link

Document

Constructs a new IndexWriter per the settings given in conf.

Usage

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void create(IndexObject indexObject) {

    IndexWriter indexWriter = null;/*from   w ww  .j  a va2 s .  c om*/
    try {
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.addDocument(DocumentUtil.IndexObject2Document(indexObject));
        indexWriter.commit();
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void deleteAll() {
    IndexWriter indexWriter = null;// w w w  . j a  v a  2  s.  com
    try {
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        Long result = indexWriter.deleteAll();
        /**/
        indexWriter.forceMergeDeletes();
        log.info("deleted:{}", result);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void update(IndexObject indexObject) {

    IndexWriter indexWriter = null;/*from w w  w .j  a  v  a 2s.c  om*/

    try {

        Term term = new Term("id", indexObject.getId().toString());
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.updateDocument(term, DocumentUtil.IndexObject2Document(indexObject));

    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void delete(IndexObject indexObject) {
    IndexWriter indexWriter = null;/*from  w w  w . j  a v  a  2  s.  c  om*/
    try {
        Term term = new Term("id", indexObject.getId().toString());
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.deleteDocuments(term);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            indexWriter.rollback();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } finally {
        try {
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:com.globalsight.ling.lucene.Index.java

License:Apache License

/**
 * Re-creates the index by batch-loading entries into it.
 * The index must be closed before calling this method.
 * Caller must use//from  ww  w . j  a  va 2 s .  c  o  m
 * <PRE>
 *   close();
 *   try
 *   {
 *     batchOpen();
 *     ...
 *     batchAddDocument();
 *     ...
 *   }
 *   finally
 *   {
 *     batchDone();
 *   }
 */
public void batchOpen() throws IOException {
    synchronized (m_state) {
        if (m_state != STATE_CLOSED) {
            throw new IOException("index is open");
        }

        m_state = STATE_CREATING;
    }

    // setup RAMDirectory and writer
    m_ramdir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    config.setOpenMode(OpenMode.CREATE_OR_APPEND);
    //config.setSimilarity(m_similarity);

    m_ramIndexWriter = new IndexWriter(m_ramdir, config);
    //m_ramIndexWriter.mergeFactor = 10000;
}

From source file:com.globalsight.ling.lucene.Index.java

License:Apache License

/**
 * Allocates a Lucene IndexWriter object. The IndexWriter must be
 * closed by the caller. If p_create is true, the existing index
 * is cleared on disk.//  w  w  w. ja  v a 2  s  .c o  m
 */
private IndexWriter getIndexWriter(boolean p_create) throws IOException {
    IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    OpenMode om = p_create ? OpenMode.CREATE : OpenMode.CREATE_OR_APPEND;
    conf.setOpenMode(om);

    IndexWriter result = null;
    boolean deleteFiles = false;
    try {
        result = new IndexWriter(m_fsDir, conf);
    } catch (EOFException eofe) {
        deleteFiles = true;
    } catch (IndexFormatTooOldException ie) {
        deleteFiles = true;
    }

    if (deleteFiles) {
        File indexDir = new File(m_directory);
        if (!indexDir.exists()) {
            indexDir.mkdirs();
        }
        // delete too old index
        File[] files = indexDir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                File oneFile = files[i];
                try {
                    oneFile.delete();
                } catch (Exception eee) {
                    // ignore
                }
            }
        }

        result = new IndexWriter(m_fsDir, conf);
    }

    return result;
}

From source file:com.globalsight.ling.tm2.lucene.LuceneIndexWriter.java

License:Apache License

/**
 * The constructor gets a lock on the index directory.  If the
 * directory doesn't exist yet, it is created. When an operation
 * is done, close() method must be called to remove the lock.
 * /*  ww  w.  j  av  a2 s .co m*/
 * @param p_tmId TM id
 * @param p_locale locale of the index
 */
public LuceneIndexWriter(long p_tmId, GlobalSightLocale p_locale, boolean p_isFirst) throws Exception {
    m_tmId = p_tmId;
    m_analyzer = new GsPerFieldAnalyzer(p_locale);
    m_isFirst = p_isFirst;

    m_indexDir = LuceneUtil.getGoldTmIndexDirectory(p_tmId, p_locale, true);

    // get the directory. Note that the directory cannot be
    // created before getting a lock. Note2:
    // FSDirectory.getDirectory(dir, true) doesn't really create
    // index files. It just clear out the old index files and lock
    // file.
    m_directory = FSDirectory.open(m_indexDir);

    // get a lock on the directory
    m_lock = m_directory.makeLock(LOCK_NAME);
    if (!m_lock.obtain(180000L)) {
        m_lock = null;
        throw new IOException("Index locked for write: " + m_lock);
    }

    // only after gettting a lock, create the initial index files
    // if it doesn't exist.
    if (!DirectoryReader.indexExists(m_directory)) {
        IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
        conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
        boolean initSuccess = false;
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(m_directory, conf);
            initSuccess = true;
        } catch (IndexFormatTooOldException ie) {
            // delete too old index
            File[] files = m_indexDir.listFiles();
            if (files != null && files.length > 0) {
                for (int i = 0; i < files.length; i++) {
                    File oneFile = files[i];
                    if (!LuceneIndexWriter.LOCK_NAME.equals(oneFile.getName())) {
                        oneFile.delete();
                    }
                }
            }

            writer = new IndexWriter(m_directory, conf);
            initSuccess = true;
        } finally {
            if (!initSuccess) {
                m_lock.release();
            }
            IOUtils.closeWhileHandlingException(writer);
        }
    }
}

From source file:com.globalsight.ling.tm2.lucene.LuceneIndexWriter.java

License:Apache License

/**
 * Indexes segments. To maintain index integrity, indexes are at
 * first created in memory and merged into a file system index.
 *
 * @param p_tuvs List of BaseTmTuv, SegmentsForSave.AddTuv, or TM3Tuv
 * @param p_sourceLocale true if p_tuvs are source locale segments
 * @param p_indexTargetLocales true for TM3, false for TM2
 *///from   w ww  .j  av a 2  s  .c o m
public void index(List p_tuvs, boolean p_sourceLocale, boolean p_indexTargetLocales) throws Exception {
    IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    conf.setOpenMode(m_isFirst ? OpenMode.CREATE : OpenMode.CREATE_OR_APPEND);
    IndexWriter fsIndexWriter = new IndexWriter(m_directory, conf);

    try {
        for (Iterator it = p_tuvs.iterator(); it.hasNext();) {
            Object tuv = it.next();

            Document doc = tuv instanceof BaseTmTuv
                    ? createDocumentFromBaseTmTuv((BaseTmTuv) tuv, p_sourceLocale, p_indexTargetLocales)
                    : tuv instanceof AddTuv
                            ? createDocumentFromAddTuv((AddTuv) tuv, p_sourceLocale, p_indexTargetLocales)
                            : tuv instanceof TM3Tuv
                                    ? createDocumentFromTM3Tuv((TM3Tuv<GSTuvData>) tuv, p_sourceLocale,
                                            p_indexTargetLocales)
                                    : null;

            fsIndexWriter.addDocument(doc);
        }
    } finally {
        fsIndexWriter.close();
    }

    // clean cache if have
    LuceneCache.cleanLuceneCache(m_indexDir);
}

From source file:com.globalsight.ling.tm2.lucene.LuceneIndexWriter.java

License:Apache License

public void remove(Collection p_tuvs) throws Exception {
    IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(m_directory, conf);

    try {/* w w  w  .j  a v a2 s.c  om*/
        for (Iterator it = p_tuvs.iterator(); it.hasNext();) {
            Object tuv = it.next();
            Long id = tuv instanceof BaseTmTuv ? ((BaseTmTuv) tuv).getId()
                    : tuv instanceof TM3Tuv ? ((TM3Tuv) tuv).getId() : null;

            Term term = new Term(TuvDocument.TUV_ID_FIELD, id.toString());
            writer.deleteDocuments(term);
        }
    } catch (Throwable e) {
        c_logger.error(e.getMessage(), e);
        //indexReader.undeleteAll();
        throw (e instanceof Exception ? (Exception) e : new Exception(e));
    } finally {
        writer.commit();
        writer.close();
    }

    // clean cache if have
    LuceneCache.cleanLuceneCache(m_indexDir);
}

From source file:com.globalsight.ling.tm2.lucene.LuceneReindexer.java

License:Apache License

/**
 * The constructor gets a lock on the index directory and discard
 * existing index, if any. When an operation is done, close()
 * method must be called to remove the lock.
 * /*from   w  w  w . j a  v a2  s.co m*/
 * @param p_tmId TM id
 * @param p_locale locale of the index
 */
public LuceneReindexer(long p_tmId, GlobalSightLocale p_locale) throws Exception {
    m_locale = p_locale;
    m_tmId = p_tmId;
    m_analyzer = new GsAnalyzer(p_locale);

    m_indexDir = LuceneUtil.getGoldTmIndexDirectory(p_tmId, p_locale, true);

    // get the directory
    FSDirectory directory = FSDirectory.open(m_indexDir);

    // get a lock on the directory
    m_lock = directory.makeLock(LuceneIndexWriter.LOCK_NAME);
    if (!m_lock.obtain(180000L)) {
        m_lock = null;
        throw new IOException("Index locked for write: " + m_lock);
    }

    // get an IndexWriter on the directory, recreating a new index
    // repository
    IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    conf.setOpenMode(OpenMode.CREATE);
    boolean initSuccess = false;
    try {
        m_indexWriter = new IndexWriter(directory, conf);
        initSuccess = true;
    } catch (IndexFormatTooOldException ie) {
        // delete too old index
        File[] files = m_indexDir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                File oneFile = files[i];
                if (!LuceneIndexWriter.LOCK_NAME.equals(oneFile.getName())) {
                    oneFile.delete();
                }
            }
        }

        m_indexWriter = new IndexWriter(directory, conf);
        initSuccess = true;
    } finally {
        if (!initSuccess) {
            m_lock.release();
        }
    }

    // clean cache if have
    LuceneCache.cleanLuceneCache(m_indexDir);
}