Example usage for org.apache.lucene.index DirectoryReader indexExists

List of usage examples for org.apache.lucene.index DirectoryReader indexExists

Introduction

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

Prototype

public static boolean indexExists(Directory directory) throws IOException 

Source Link

Document

Returns true if an index likely exists at the specified directory.

Usage

From source file:org.apache.tika.parser.geo.topic.GeoNameResolver.java

License:Apache License

/**
 * Build the gazetteer index line by line
 * /* w w w .ja va  2s  .c  om*/
 * @param GAZETTEER_PATH
 *            path of the gazetter file
 * @throws IOException
 * @throws RuntimeException
 */
public void buildIndex(String GAZETTEER_PATH) throws IOException {
    File indexfile = new File(INDEXDIR_PATH);
    indexDir = FSDirectory.open(indexfile.toPath());
    if (!DirectoryReader.indexExists(indexDir)) {
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        indexWriter = new IndexWriter(indexDir, config);
        Logger logger = Logger.getLogger(this.getClass().getName());
        logger.log(Level.WARNING, "Start Building Index for Gazatteer");
        BufferedReader filereader = new BufferedReader(
                new InputStreamReader(new FileInputStream(GAZETTEER_PATH), "UTF-8"));
        String line;
        int count = 0;
        while ((line = filereader.readLine()) != null) {
            try {
                count += 1;
                if (count % 100000 == 0) {
                    logger.log(Level.INFO, "Indexed Row Count: " + count);
                }
                addDoc(indexWriter, line);

            } catch (RuntimeException re) {
                logger.log(Level.WARNING, "Skipping... Error on line: {}", line);
            }
        }
        logger.log(Level.WARNING, "Building Finished");
        filereader.close();
        indexWriter.close();
    }
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

public static final boolean indexExists(final Directory directory) throws IOException {
    return DirectoryReader.indexExists(directory);
}

From source file:org.codice.ddf.spatial.geocoding.index.GeoNamesLuceneIndexer.java

License:Open Source License

IndexWriter createIndexWriter(final boolean create, final Directory directory) throws IOException {

    final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(ANALYZER);

    // Set to CREATE mode if the index does not exist.
    if (!DirectoryReader.indexExists(directory)) {
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
    } else {/*w w  w.  j  a  va  2s .co  m*/
        indexWriterConfig.setOpenMode(create ? OpenMode.CREATE : OpenMode.APPEND);
    }
    indexWriterConfig.setSimilarity(SIMILARITY);
    return new IndexWriter(directory, indexWriterConfig);
}

From source file:org.codice.ddf.spatial.geocoding.query.GeoNamesQueryLuceneIndex.java

License:Open Source License

protected boolean indexExists(final Directory directory) throws IOException {
    return DirectoryReader.indexExists(directory);
}

From source file:org.dspace.search.LuceneIndex.java

License:BSD License

@Override
public void init(String config) {
    indexDirectory = config;//from  ww  w. ja  va  2 s. c om
    File indexDir = new File(indexDirectory);
    try {
        if (!DirectoryReader.indexExists(FSDirectory.open(indexDir))) {
            indexDir.mkdirs();
            openIndex(true).close();
        }
    } catch (IOException e) {
        throw new IllegalStateException("Could not create search index: " + e.getMessage(), e);
    }
    // set maxfieldlength
    maxFieldLength = ConfigurationManager.getIntProperty("search", "maxfieldlength", -1);
}

From source file:org.eclipse.rdf4j.sail.lucene.LuceneIndex.java

License:Open Source License

private void postInit() throws IOException {
    this.queryAnalyzer = new StandardAnalyzer();

    // do some initialization for new indices
    if (!DirectoryReader.indexExists(directory)) {
        logger.debug("creating new Lucene index in directory {}", directory);
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
        writer.close();/*from  www  . j  a  v a2 s  . c  o m*/
    }
}

From source file:org.eu.bitzone.Leia.java

License:Apache License

/**
 * Attempt to load the index with parameters specified in the dialog.
 * <p>/*from   w w  w .  j  a v  a2  s.c  o m*/
 * NOTE: this method is invoked from the UI. If you need to open an index programmatically, you should use
 * {@link #openIndex(String, boolean, boolean, boolean)} instead.
 * </p>
 *
 * @param dialog UI dialog with parameters
 */
public void openOk(final Object dialog) {
    final Object path = find(dialog, "path");
    pName = getString(path, "text").trim();

    final boolean force = getBoolean(find(dialog, "force"), "selected");
    final boolean noReader = getBoolean(find(dialog, "cbNoReader"), "selected");
    tiiDiv = 1;
    try {
        tiiDiv = Integer.parseInt(getString(find(dialog, "tiiDiv"), "text"));
    } catch (final Exception e) {
        e.printStackTrace();
    }
    final Object dirImpl = getSelectedItem(find(dialog, "dirImpl"));
    String dirClass = null;
    if (dirImpl == null) {
        dirClass = FSDirectory.class.getName();
    } else {
        final String name = getString(dirImpl, "name");
        if (name == null) {
            dirClass = getString(dirImpl, "text");
        } else {
            if (name.equals("fs")) {
                dirClass = FSDirectory.class.getName();
            } else if (name.equals("mmap")) {
                dirClass = MMapDirectory.class.getName();
            } else if (name.equals("niofs")) {
                dirClass = NIOFSDirectory.class.getName();
            }
        }
    }
    if (pName == null || pName.trim().equals("")) {
        errorMsg("Invalid path.");
        return;
    }
    readOnly = getBoolean(find(dialog, "ro"), "selected");
    ram = getBoolean(find(dialog, "ram"), "selected");
    keepCommits = getBoolean(find(dialog, "cbKeepCommits"), "selected");
    slowAccess = getBoolean(find(dialog, "cbSlowIO"), "selected");
    decoders.clear();
    currentCommit = null;
    Prefs.addToMruList(pName);
    syncMRU(path);
    remove(dialog);
    if (noReader) {
        removeAll();
        addComponent(this, "/xml/luke.xml", null, null);
        try {
            final Directory d = openDirectory(dirClass, pName, false);
            if (DirectoryReader.indexExists(d)) {
                throw new Exception("there is no valid Lucene index in this directory.");
            }
            dir = d;
            initOverview();
            infoMsg("There is no IndexReader - most actions are disabled. "
                    + "You can open IndexReader from current Directory using 'Re-Open'");
        } catch (final Exception e) {
            errorMsg("ERROR: " + e.toString());
        }
    } else {
        openIndex(pName, force, dirClass, readOnly, ram, keepCommits, null, tiiDiv);
    }
}

From source file:org.getopt.luke.Luke.java

License:Apache License

/**
 * Attempt to load the index with parameters specified in the dialog.
 * <p>NOTE: this method is invoked from the UI. If you need to open an index
 * programmatically, you should use {@link: openIndex(String, boolean, String, boolean,
  boolean, boolean , IndexCommit, int)} instead.</p>
 * @param dialog UI dialog with parameters
 *//*  w  w w  .j  a  va2s  . com*/
public void openOk(Object dialog) {
    Object path = find(dialog, "path");
    pName = getString(path, "text").trim();

    boolean force = getBoolean(find(dialog, "force"), "selected");
    boolean noReader = getBoolean(find(dialog, "cbNoReader"), "selected");
    tiiDiv = 1;
    try {
        tiiDiv = Integer.parseInt(getString(find(dialog, "tiiDiv"), "text"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Object dirImpl = getSelectedItem(find(dialog, "dirImpl"));
    String dirClass = null;
    if (dirImpl == null) {
        dirClass = FSDirectory.class.getName();
    } else {
        String name = getString(dirImpl, "name");
        if (name == null) {
            dirClass = getString(dirImpl, "text");
        } else {
            if (name.equals("fs")) {
                dirClass = FSDirectory.class.getName();
            } else if (name.equals("mmap")) {
                dirClass = MMapDirectory.class.getName();
            } else if (name.equals("niofs")) {
                dirClass = NIOFSDirectory.class.getName();
            }
        }
    }
    if (pName == null || pName.trim().equals("")) {
        errorMsg("Invalid path.");
        return;
    }
    readOnly = getBoolean(find(dialog, "ro"), "selected");
    ram = getBoolean(find(dialog, "ram"), "selected");
    keepCommits = getBoolean(find(dialog, "cbKeepCommits"), "selected");
    slowAccess = getBoolean(find(dialog, "cbSlowIO"), "selected");
    decoders.clear();
    currentCommit = null;
    Prefs.addToMruList(pName);
    syncMRU(path);
    remove(dialog);
    if (noReader) {
        removeAll();
        addComponent(this, "/xml/luke.xml", null, null);
        try {
            Directory d = openDirectory(dirClass, pName, false);
            if (DirectoryReader.indexExists(d)) {
                throw new Exception("there is no valid Lucene index in this directory.");
            }
            dir = d;
            initOverview();
            infoMsg("There is no IndexReader - most actions are disabled. "
                    + "You can open IndexReader from current Directory using 'Re-Open'");
        } catch (Exception e) {
            errorMsg("ERROR: " + e.toString());
        }
    } else {
        openIndex(pName, force, dirClass, readOnly, ram, keepCommits, null, tiiDiv);
    }
}

From source file:org.hibernate.search.store.spi.DirectoryHelper.java

License:LGPL

/**
 * Initialize the Lucene Directory if it isn't already.
 *
 * @param directory the Directory to initialize
 * @throws SearchException in case of lock acquisition timeouts, IOException, or if a corrupt index is found
 *///w ww  .  ja  v  a2 s .  c o m
public static void initializeIndexIfNeeded(Directory directory) {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    try {
        if (!DirectoryReader.indexExists(directory)) {
            try {
                IndexWriterConfig iwriterConfig = new IndexWriterConfig(analyzer)
                        .setOpenMode(OpenMode.CREATE_OR_APPEND);
                //Needs to have a timeout higher than zero to prevent race conditions over (network) RPCs
                //for distributed indexes (Infinispan but probably also NFS and similar)
                iwriterConfig.setWriteLockTimeout(2000);
                IndexWriter iw = new IndexWriter(directory, iwriterConfig);
                iw.close();
            } catch (LockObtainFailedException lofe) {
                log.lockingFailureDuringInitialization(directory.toString());
            }
        }
    } catch (IOException e) {
        throw new SearchException("Could not initialize index", e);
    } finally {
        analyzer.close();
    }
}

From source file:org.modeshape.jcr.index.lucene.LuceneConfig.java

License:Apache License

protected IndexWriter newWriter(String workspaceName, String indexName) {
    CheckArg.isNotNull(indexName, "indexName");
    CheckArg.isNotNull(workspaceName, "workspaceName");
    try {//from ww w  .  j av  a 2 s  . com
        Directory directory = directory(directoryClass, workspaceName, indexName);
        IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig());
        if (DirectoryReader.indexExists(directory)) {
            readLatestCommitTime(indexWriter);
        }
        return indexWriter;
    } catch (IOException e) {
        throw new LuceneIndexException("Cannot create index writer", e);
    }
}