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.modeshape.jcr.index.lucene.LuceneIndex.java

License:Apache License

@Override
public boolean requiresReindexing() {
    try {//from w ww.  j av a 2  s . c  o m
        return !DirectoryReader.indexExists(writer.getDirectory());
    } catch (IOException e) {
        logger.debug(e, "cannot determine if lucene index exists...");
        return false;
    }
}

From source file:org.neo4j.kernel.api.impl.index.AbstractLuceneIndex.java

License:Open Source License

private boolean luceneDirectoryExists(File folder) throws IOException {
    try (Directory directory = indexStorage.openDirectory(folder)) {
        return DirectoryReader.indexExists(directory);
    }//from   w w w.  j  a  va 2  s .  com
}

From source file:org.neo4j.kernel.api.impl.index.backup.LuceneIndexSnapshotFileIterator.java

License:Open Source License

private static boolean hasCommits(IndexWriter indexWriter) throws IOException {
    Directory directory = indexWriter.getDirectory();
    return DirectoryReader.indexExists(directory) && SegmentInfos.readLatestCommit(directory) != null;
}

From source file:org.neo4j.kernel.api.impl.index.backup.LuceneIndexSnapshots.java

License:Open Source License

private static boolean hasCommits(Directory directory) throws IOException {
    return DirectoryReader.indexExists(directory) && SegmentInfos.readLatestCommit(directory) != null;
}

From source file:org.ojbc.adapters.analyticaldatastore.personid.IndexedIdentifierGenerationStrategy.java

License:RPL License

private Document searchForExistingAttributes(Map<String, Object> attributes) throws IOException {

    Document ret = null;/* w w w .  j  a va  2  s  .  com*/

    BooleanQuery query = new BooleanQuery();

    String firstNameAttributeValue = getFormattedAttributeValue(attributes.get(FIRST_NAME_FIELD));
    if (resolveEquivalentNames) {
        firstNameAttributeValue = firstNameEquivalentCorpus.getEquivalentName(firstNameAttributeValue);
    }
    query.add(new FuzzyQuery(new Term(FIRST_NAME_FIELD, firstNameAttributeValue), 2, 1), Occur.MUST);
    query.add(new FuzzyQuery(
            new Term(LAST_NAME_FIELD, getFormattedAttributeValue(attributes.get(LAST_NAME_FIELD))), 2, 1),
            Occur.MUST);
    query.add(new FuzzyQuery(
            new Term(MIDDLE_NAME_FIELD, getFormattedAttributeValue(attributes.get(MIDDLE_NAME_FIELD))), 2, 1),
            Occur.SHOULD);
    query.add(new FuzzyQuery(
            new Term(BIRTHDATE_FIELD, getFormattedAttributeValue(attributes.get(BIRTHDATE_FIELD))), 1, 0),
            Occur.MUST);
    query.add(new TermQuery(new Term(SEX_FIELD, getFormattedAttributeValue(attributes.get(SEX_FIELD)))),
            Occur.SHOULD);
    query.add(new FuzzyQuery(new Term(SSN_FIELD, getFormattedAttributeValue(attributes.get(SSN_FIELD))), 1, 0),
            Occur.SHOULD);
    log.debug("Query: " + query.toString());

    Directory directory = indexWriter.getDirectory();
    if (DirectoryReader.indexExists(directory)) {
        DirectoryReader reader = DirectoryReader.open(directory);

        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs hits = searcher.search(query, 2);

        ScoreDoc[] hitDocs = hits.scoreDocs;
        if (hitDocs.length > 1) {
            throw new RuntimeException("Invalid index state:  multiple matches for attributes=" + attributes);
        }
        if (hitDocs.length == 1) {
            int id = hitDocs[0].doc;
            ret = searcher.doc(id);
            log.debug("Found a match, id=" + id);
        }

        reader.close();
    }

    return ret;

}

From source file:org.olat.search.service.indexer.IndexWriterHolder.java

License:Apache License

/**
 * @return true if it has created the index
 *//*w  w w  . ja  v a 2s . co m*/
public synchronized boolean ensureIndexExists() {
    boolean created = false;
    IndexWriter writer = null;
    try {
        if (!DirectoryReader.indexExists(indexPath)) {
            writer = getAndLock();
            created = true;
        }
    } catch (IOException e) {
        log.error("", e);
    } finally {
        release(writer);
    }
    return created;
}

From source file:org.olat.search.service.indexer.IndexWriterHolder.java

License:Apache License

public synchronized IndexWriter getAndLock() throws IOException {
    if (writerRef == null) {
        long start = System.nanoTime();
        IndexWriter indexWriter = new IndexWriter(indexPath, indexer.newIndexWriterConfig());
        if (!DirectoryReader.indexExists(indexPath)) {
            indexWriter.commit();//make sure it exists
        }/*from   w  w w .  j  a v a  2 s. c  o m*/
        log.info("Opening writer takes (ms): " + CodeHelper.nanoToMilliTime(start));
        writerRef = indexWriter;
    }
    counter.incrementAndGet();
    return writerRef;
}

From source file:org.opengrok.indexer.index.IndexDatabase.java

License:Open Source License

/**
 * Get an indexReader for the Index database where a given file
 *
 * @param path the file to get the database for
 * @return The index database where the file should be located or null if it
 * cannot be located.//  w  w  w  .j  av  a2 s .c o m
 */
public static IndexReader getIndexReader(String path) {
    IndexReader ret = null;

    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    File indexDir = new File(env.getDataRootFile(), INDEX_DIR);

    if (env.hasProjects()) {
        Project p = Project.getProject(path);
        if (p == null) {
            return null;
        }
        indexDir = new File(indexDir, p.getPath());
    }
    try {
        FSDirectory fdir = FSDirectory.open(indexDir.toPath(), NoLockFactory.INSTANCE);
        if (indexDir.exists() && DirectoryReader.indexExists(fdir)) {
            ret = DirectoryReader.open(fdir);
        }
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Failed to open index: {0}", indexDir.getAbsolutePath());
        LOGGER.log(Level.FINE, "Stack Trace: ", ex);
    }
    return ret;
}

From source file:org.opengrok.suggest.Suggester.java

License:Open Source License

private boolean indexExists(final Path indexDir) throws IOException {
    try (Directory indexDirectory = FSDirectory.open(indexDir)) {
        return DirectoryReader.indexExists(indexDirectory);
    }/* ww w .  j  av a 2  s .  c  o  m*/
}

From source file:org.openrdf.sail.lucene4.LuceneIndex.java

License:BSD License

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

    // do some initialization for new indices
    if (!DirectoryReader.indexExists(directory)) {
        logger.info("creating new Lucene index in directory {}", directory);
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
        writer.close();/*from  ww  w . j ava 2  s  . c  o  m*/
    }
}