Example usage for org.apache.lucene.index IndexWriterConfig setOpenMode

List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode

Introduction

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

Prototype

public IndexWriterConfig setOpenMode(OpenMode openMode) 

Source Link

Document

Specifies OpenMode of the index.

Usage

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  w w  . ja  v  a  2s .c  om*/
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 {/*from  w w  w .j  a  v a 2  s  .co  m*/
        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.
 * //ww w  .  j  av  a2  s  .c  om
 * @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);
}

From source file:com.gmail.mosoft521.luceneDemo.IndexFiles.java

License:Apache License

/**
 * Index all text files under a directory.
 *///from   w w w  .  jav  a2s  .  c  om
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "index";
    String docsPath = null;
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(new File(indexPath));
        // :Post-Release-Update-Version.LUCENE_XY:
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.google.gerrit.server.change.ReviewerSuggestionCache.java

License:Apache License

private IndexSearcher index() throws IOException, OrmException {
    RAMDirectory idx = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer(CharArraySet.EMPTY_SET));
    config.setOpenMode(OpenMode.CREATE);

    try (IndexWriter writer = new IndexWriter(idx, config)) {
        for (Account a : db.get().accounts().all()) {
            if (a.isActive()) {
                addAccount(writer, a);/*from   www . j a va  2s.  c  o m*/
            }
        }
    }

    return new IndexSearcher(DirectoryReader.open(idx));
}

From source file:com.gprasad.searchwithlucene.Indexer.java

private static void createIndex(String indexPath) throws IOException {
    Directory dir = FSDirectory.open(Paths.get(indexPath));
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    indexWriterConfig.setOpenMode(OpenMode.CREATE);
    writer = new IndexWriter(dir, indexWriterConfig);
}

From source file:com.graphhopper.compare.misc.LuceneStorage.java

License:Apache License

public boolean init(boolean forceCreate) {
    try {/* ww  w .  ja  va 2s. c om*/
        File file = new File("osm.lucene.test");
        if (forceCreate)
            Helper.deleteDir(file);

        // germany.osm => 3.6 GB on disc for nodes only, 1.5 GB memory usage at the end of the nodes
        Directory dir = FSDirectory.open(file);
        IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_35, new KeywordAnalyzer());
        LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
        mp.setMaxMergeMB(3000);
        cfg.setRAMBufferSizeMB(128);
        cfg.setTermIndexInterval(512);
        cfg.setMergePolicy(mp);

        // specify different formats for id fields etc
        // -> this breaks 16 of our tests!? Lucene Bug?
        //            cfg.setCodec(new Lucene40Codec() {
        //
        //                @Override public PostingsFormat getPostingsFormatForField(String field) {
        //                    return new Pulsing40PostingsFormat();
        //                }
        //            });

        // cfg.setMaxThreadStates(8);
        boolean create = !IndexReader.indexExists(dir);
        cfg.setOpenMode(create ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND);
        writer = new IndexWriter(dir, cfg);
        return true;
    } catch (Exception ex) {
        logger.error("cannot init lucene storage", ex);
        return false;
    }
}

From source file:com.helger.pd.indexer.lucene.PDLucene.java

License:Apache License

public PDLucene() throws IOException {
    // Where to store the index files
    final Path aPath = getLuceneIndexDir().toPath();
    m_aDir = FSDirectory.open(aPath);//  ww  w  .j av  a2 s.  c o  m

    // Analyzer to use
    m_aAnalyzer = createAnalyzer();

    // Create the index writer
    final IndexWriterConfig aWriterConfig = new IndexWriterConfig(m_aAnalyzer);
    aWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    m_aIndexWriter = new IndexWriter(m_aDir, aWriterConfig);

    // Reader and searcher are opened on demand

    s_aLogger.info("Lucene index operating on " + aPath);
}

From source file:com.icdd.lucence.IndexFiles.java

License:Apache License

public void index(boolean mode) {
    boolean create = mode;
    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        logger.warn("Document directory '" + docDir + "'does not exist or  is not readable, "
                + "please check the path");
        System.exit(1);//from  w w  w . jav  a2 s . c o m
    }
    Date start = new Date();
    try {
        logger.warn("Indexing to directory '" + indexPath + "'...");
        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }
        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);
        writer.close();
        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");
    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.icdd.lucene.CreateIndex.java

License:Apache License

public CreateIndex(boolean mode) {
    boolean create = mode;
    try {/*from  w w  w .  j  a va2  s.c o  m*/
        Directory dir = FSDirectory.open(Paths.get(INDEX_PATH));
        IndexWriterConfig iwc = new IndexWriterConfig(ANALYZER);
        if (create)
            iwc.setOpenMode(OpenMode.CREATE);
        else {
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }
        writer = new IndexWriter(dir, iwc);
    } catch (IOException e) {
        e.printStackTrace();
    }

}