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:org.explore3.searchengine.indexCreator.Indexer.java

License:Apache License

public static void main(String[] args) {

    String indexPath = "index";
    String DocsPath = "C:/Users/Pietro/Desktop/dataset/";
    boolean create = true;

    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);/*from  w  ww .ja v  a2 s  . c  o  m*/
    }

    Date start = new Date();

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

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

        if (create) {
            // Creo nuovo indice e rimuovo i precedenti
            indexWriterConf.setOpenMode(OpenMode.CREATE);
        } else {
            // Aggiungo il documento ad un indice esistente
            indexWriterConf.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        IndexWriter writer = new IndexWriter(indexDir, indexWriterConf);
        indexDocument(writer, DocDir);

        writer.close();
        IndexWriterConfig indexWriterConf2 = new IndexWriterConfig(Version.LUCENE_47, analyzer);
        System.out.println("Creating spell index...");
        Spellindex.createSpellIndex("words", indexDir, indexDir, indexWriterConf2);

        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:org.fcrepo.migration.foxml.DirectoryScanningIDResolver.java

License:Apache License

/**
 * directory scanning ID resolver/*from   w  ww  .  j a  v a 2s. co m*/
 * @param cachedIndexDir the index directory.  If it exists, the old cache will be used, if it doesn't a new
 *                 cache will be built at that location.  If it is null, a new cache will be built in
 *                 the temp file space that will be deleted upon application shutdown.
 * @param dsRoot the datastream root
 * @throws IOException IO exception
 */
public DirectoryScanningIDResolver(final File cachedIndexDir, final File dsRoot) throws IOException {
    final File indexDir;
    if (cachedIndexDir == null) {
        final File temp = File.createTempFile("tempfile", "basedir");
        temp.delete();
        temp.mkdir();
        indexDir = new File(temp, "index");
        LOGGER.info("No index directory specified.  Creating temporary index at \"" + indexDir.getAbsolutePath()
                + "\".");
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    LOGGER.info("Deleting index directory at \"" + indexDir.getAbsolutePath() + "\"...");
                    FileUtils.deleteDirectory(indexDir);
                } catch (IOException e) {
                    LOGGER.error("Unable to delete index directory at \"" + indexDir.getAbsolutePath() + "\"!",
                            e);
                    e.printStackTrace();
                }
            }
        }));
    } else {
        indexDir = cachedIndexDir;
    }
    final Directory dir = FSDirectory.open(indexDir);
    if (indexDir.exists()) {
        LOGGER.warn("Index exists at \"" + indexDir.getPath() + "\" and will be used.  "
                + "To clear index, simply delete this directory and re-run the application.");
    } else {
        final Analyzer analyzer = new StandardAnalyzer();
        final IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        final IndexWriter writer = new IndexWriter(dir, iwc);
        LOGGER.info("Builidng an index of all the datastreams in \"" + dsRoot.getPath() + "\"...");
        indexDatastreams(writer, dsRoot);

        writer.commit();
        writer.close();
    }

    final IndexReader reader = DirectoryReader.open(FSDirectory.open(indexDir));
    searcher = new IndexSearcher(reader);
}

From source file:org.fcrepo.migration.idmappers.OpaqueIDMapper.java

License:Apache License

/**
 * A constructor.//from   w  w w. ja  v  a2  s  . c o m
 * @param cachedIDIndexDir the directory (or null) where the index of generated pids should be maintained
 * @param f4Client a Fedora 4 client to mediate interactions with the repository
 * @throws IOException
 */
public OpaqueIDMapper(final File cachedIDIndexDir, final Fedora4Client f4Client) throws IOException {
    this.f4Client = f4Client;
    final File indexDir;
    if (cachedIDIndexDir == null) {
        final File temp = File.createTempFile("tempfile", "basedir");
        temp.delete();
        temp.mkdir();
        indexDir = new File(temp, "index");
        LOGGER.info("No generated ID index directory specified.  Creating temporary index at \""
                + indexDir.getAbsolutePath() + "\".");
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    LOGGER.info("Deleting generated ID index directory at \"" + indexDir.getAbsolutePath()
                            + "\"...");
                    FileUtils.deleteDirectory(indexDir);
                } catch (IOException e) {
                    LOGGER.error("Unable to delete generated ID index directory at \""
                            + indexDir.getAbsolutePath() + "\"!", e);
                    e.printStackTrace();
                }
            }
        }));
    } else {
        indexDir = cachedIDIndexDir;
    }

    final Directory dir = FSDirectory.open(indexDir);
    if (indexDir.exists()) {
        LOGGER.warn("Index exists at \"" + indexDir.getPath() + "\" and will be used.  "
                + "To clear index, simply delete this directory and re-run the application.");
    }
    final Analyzer analyzer = new StandardAnalyzer();
    final IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    writer = new IndexWriter(dir, iwc);
    writer.commit();

    searcherManager = new SearcherManager(writer, false, null);
}

From source file:org.fnlp.app.lucene.demo.BuildIndex.java

License:Open Source License

/**
 * @param args/*from  w w  w  .  j a v a2  s.  c om*/
 * @throws IOException 
 * @throws LoadModelException 
 */
public static void main(String[] args) throws IOException, LoadModelException {
    String indexPath = "../tmp/lucene";
    System.out.println("Indexing to directory '" + indexPath + "'...");
    Date start = new Date();
    Directory dir = FSDirectory.open(new File(indexPath));//Dirctory dir-->FSDirectory
    //?? CNFactory
    CNFactory factory = CNFactory.getInstance("../models", Models.SEG_TAG);
    Analyzer analyzer = new FNLPAnalyzer(Version.LUCENE_47);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(dir, iwc);

    String[] strs = new String[] { "?",
            "?????????",
            "????", "" };
    //Date start = new Date();
    for (int i = 0; i < strs.length; i++) {

        Document doc = new Document();

        Field field = new TextField("content", strs[i], Field.Store.YES);
        doc.add(field);
        if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
            writer.addDocument(doc);
        } else {
            writer.updateDocument(new Term("content", strs[i]), doc);
        }
    }
    writer.close();

    //??????
    //dir.close();
    //??????

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

}

From source file:org.frontcache.cache.impl.LuceneIndexManager.java

License:Apache License

/**
 * Returns instance of IndexManager/*from www  .  j  a  v  a2 s  . c o m*/
 * @param create
 * @return
 * @throws IOException
 */
private IndexWriter getIndexWriter() throws IOException {
    if (indexWriter == null || !indexWriter.isOpen()) {
        synchronized (this) {
            if (indexWriter == null || !indexWriter.isOpen()) {
                indexWriter = null;
                logger.info("Trying to get indexWriter...");
                Directory dir = FSDirectory.open(Paths.get(INDEX_PATH));
                Analyzer analyzer = new StandardAnalyzer();
                IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

                iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
                iwc.setRAMBufferSizeMB(250.0);
                indexWriter = new IndexWriter(dir, iwc);
                logger.info("IndexWriter initialized");
            }
        }
    }

    return indexWriter;
}

From source file:org.h2.fulltext.FullTextLucene.java

License:Mozilla Public License

/**
 * Get the index writer/searcher wrapper for the given connection.
 *
 * @param conn the connection/*from w ww  . j  a  v a  2 s.co  m*/
 * @return the index access wrapper
 */
protected static IndexAccess getIndexAccess(Connection conn) throws SQLException {
    String path = getIndexPath(conn);
    synchronized (INDEX_ACCESS) {
        IndexAccess access = INDEX_ACCESS.get(path);
        if (access == null) {
            try {
                Directory indexDir = path.startsWith(IN_MEMORY_PREFIX) ? new RAMDirectory()
                        : FSDirectory.open(new File(path));
                Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
                IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, analyzer);
                conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
                IndexWriter writer = new IndexWriter(indexDir, conf);
                //see http://wiki.apache.org/lucene-java/NearRealtimeSearch
                IndexReader reader = IndexReader.open(writer, true);
                access = new IndexAccess();
                access.writer = writer;
                access.reader = reader;
                access.searcher = new IndexSearcher(reader);
            } catch (IOException e) {
                throw convertException(e);
            }
            INDEX_ACCESS.put(path, access);
        }
        return access;
    }
}

From source file:org.hibernate.search.test.query.engine.FieldNameCollectorTest.java

License:LGPL

private void indexTestDocuments(Directory directory) throws IOException {
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    Document document = new Document();
    document.add(new StringField("stringField", "test", Field.Store.NO));
    document.add(new IntField("intField", 0, Field.Store.NO));
    indexWriter.addDocument(document);// ww  w . j  a v a 2 s . c o  m
    indexWriter.commit();
    indexWriter.close();
}

From source file:org.hip.vif.core.search.VIFIndexing.java

License:Open Source License

protected IndexWriterConfig createConfiguration(final boolean inCreateNew) {
    final IndexWriterConfig out = new IndexWriterConfig(AbstractSearching.LUCENE_VERSION,
            AbstractSearching.getAnalyzer());
    out.setOpenMode(inCreateNew ? OpenMode.CREATE : OpenMode.CREATE_OR_APPEND);
    return out;//from   w w w  .j  a v  a 2s.  c o  m
}

From source file:org.janusgraph.diskstorage.lucene.LuceneExample.java

License:Apache License

@Test
public void example1() throws Exception {
    Directory dir = FSDirectory.open(path.toPath());
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(dir, iwc);

    indexDocs(writer, "doc1", ImmutableMap.of("name", "The laborious work of John Doe as we know it", "city",
            "Blumenkamp", "location", Geoshape.point(51.687882, 6.612053), "time", 1000342034));

    indexDocs(writer, "doc2", ImmutableMap.of("name", "Life as we know it or not", "city", "Essen", "location",
            Geoshape.point(51.787882, 6.712053), "time", 1000342034 - 500));

    indexDocs(writer, "doc3",
            ImmutableMap.of("name", "Berlin - poor but sexy and a display of the extraordinary", "city",
                    "Berlin", "location", Geoshape.circle(52.509535, 13.425293, 50), "time",
                    1000342034 + 2000));
    writer.close();/* www  .j a va 2  s .  c  o  m*/

    //Search
    IndexReader reader = DirectoryReader.open(FSDirectory.open(path.toPath()));
    IndexSearcher searcher = new IndexSearcher(reader);

    //Auesee
    BooleanQuery.Builder filter = new BooleanQuery.Builder();

    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects,
            Geoshape.circle(51.666167, 6.58905, 450).getShape());

    filter.add(LongPoint.newRangeQuery("time", (long) 1000342034, (long) 1000342034), BooleanClause.Occur.MUST);

    filter.add(new PrefixQuery(new Term("city_str", "B")), BooleanClause.Occur.MUST);

    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    qb.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    qb.add(filter.build(), BooleanClause.Occur.FILTER);
    TopDocs docs = searcher.search(qb.build(), MAX_RESULT);
    if (docs.totalHits >= MAX_RESULT)
        throw new RuntimeException("Max results exceeded: " + MAX_RESULT);

    Set<String> result = getResults(searcher, docs);
    System.out.println(result);

}

From source file:org.janusgraph.diskstorage.lucene.LuceneIndex.java

License:Apache License

private IndexWriter getWriter(String store, KeyInformation.IndexRetriever informations)
        throws BackendException {
    Preconditions.checkArgument(writerLock.isHeldByCurrentThread());
    IndexWriter writer = writers.get(store);
    if (writer == null) {
        final LuceneCustomAnalyzer analyzer = delegatingAnalyzerFor(store, informations);
        final IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        try {/* ww w.j  av  a2s .c  om*/
            writer = new IndexWriter(getStoreDirectory(store), iwc);
            writers.put(store, writer);
        } catch (final IOException e) {
            throw new PermanentBackendException("Could not create writer", e);
        }
    }
    return writer;
}