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:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java

License:Apache License

private IndexReader createIndexReader(String indexerPath) throws IOException {
    File indexfile = new File(indexerPath);
    indexDir = FSDirectory.open(indexfile.toPath());

    if (!DirectoryReader.indexExists(indexDir)) {
        LOG.log(Level.SEVERE, "No Lucene Index Dierctory Found, Invoke indexBuild() First !");
        System.exit(1);/* w ww.  j a va  2  s. c  o m*/
    }

    return DirectoryReader.open(indexDir);
}

From source file:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java

License:Apache License

/**
 * Build the gazetteer index line by line
 *
 * @param gazetteerPath path of the gazetteer file
 * @param indexerPath path to the created Lucene index directory.
 * @param reverseGeocodingEnabled/*from w w w . j a v  a  2  s  .c o m*/
 * @throws IOException
 * @throws RuntimeException
 */
public void buildIndex(String gazetteerPath, String indexerPath, boolean reverseGeocodingEnabled)
        throws IOException {
    File indexfile = new File(indexerPath);
    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(gazetteerPath), "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, reverseGeocodingEnabled);

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

From source file:cn.hbu.cs.esearch.index.IndexReaderDispenser.java

License:Apache License

/**
 * constructs a new IndexReader instance
 * <p/>// w  w w  .j av  a2s  .  c o m
 * Where the index is.
 *
 * @return Constructed IndexReader instance.
 *
 * @throws java.io.IOException
 */
private EsearchMultiReader<R> newReader(DirectoryManager dirMgr, IndexReaderDecorator<R> decorator,
        IndexSignature signature) throws IOException {
    if (!dirMgr.exists()) {
        return null;
    }

    Directory dir = dirMgr.getDirectory();

    if (!DirectoryReader.indexExists(dir)) {
        return null;
    }

    int numTries = INDEX_OPEN_NUM_RETRIES;
    EsearchMultiReader<R> reader = null;

    // try max of 5 times, there might be a case where the segment file is being updated
    while (reader == null) {
        if (numTries == 0) {
            LOGGER.error("Problem refreshing disk index, all attempts failed.");
            throw new IOException("problem opening new index");
        }
        numTries--;

        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("opening index reader at: " + dirMgr.getPath());
            }
            DirectoryReader srcReader = DirectoryReader.open(dir);

            try {
                reader = new EsearchMultiReader<R>(srcReader, decorator);
                _currentSignature = signature;
            } catch (IOException ioe) {
                // close the source reader if EsearchMultiReader construction fails
                if (srcReader != null) {
                    srcReader.close();
                }
                throw ioe;
            }
        } catch (IOException ioe) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                LOGGER.warn("thread interrupted.");
                continue;
            }
        }
    }
    return reader;
}

From source file:cn.hbu.cs.esearch.index.RAMSearchIndex.java

License:Apache License

private EsearchMultiReader<R> openIndexReaderInternal() throws IOException {
    if (DirectoryReader.indexExists(_directory)) {
        DirectoryReader srcReader = null;
        EsearchMultiReader<R> finalReader = null;
        try {//from w w w  .j a  va  2  s.  c  om
            // for RAM indexes, just get a new index reader
            srcReader = DirectoryReader.open(_directory);
            finalReader = new EsearchMultiReader<R>(srcReader, _decorator);
            DocIDMapper mapper = _idxMgr._docIDMapperFactory.getDocIDMapper(finalReader);
            finalReader.setDocIDMapper(mapper);
            return finalReader;
        } catch (IOException ioe) {
            // if reader decoration fails, still need to close the source reader
            if (srcReader != null) {
                srcReader.close();
            }
            throw ioe;
        }
    } else {
        return null; // null indicates no index exist, following the contract
    }
}

From source file:com.b2international.index.lucene.NullIndexSearcher.java

License:Apache License

private static DirectoryReader createRamReader() throws IOException {

    final RAMDirectory directory = new RAMDirectory();
    if (!DirectoryReader.indexExists(directory)) {

        final IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
        final IndexWriter writer = new IndexWriter(directory, conf);
        writer.commit();//from  w  w w.  j  a  v a 2  s.  com
        writer.close();
    }

    return DirectoryReader.open(directory);

}

From source file:com.bluedragon.search.collection.Collection.java

License:Open Source License

/**
 * Creates an empty collection to get it up and running
 *//*from  w  ww  .  j a v a  2  s  . c o m*/
public synchronized void create(boolean _errorOnExists) throws IOException {
    setDirectory();

    if (directory.listAll().length > 2) {
        if (_errorOnExists) {
            throw new IOException("directory not empty; possible collection already present");
        } else {
            if (DirectoryReader.indexExists(directory)) {
                return;
            } // otherwise an index doesn't exist so allow the creation code to execute
        }
    }

    IndexWriterConfig iwc = new IndexWriterConfig(AnalyzerFactory.get(language));
    iwc.setOpenMode(OpenMode.CREATE);

    indexwriter = new IndexWriter(directory, iwc);
    indexwriter.commit();
    indexwriter.close();
    indexwriter = null;

    // throw an openbd.create file in there so we know when it was created
    created = System.currentTimeMillis();
    File touchFile = new File(collectionpath, "openbd.created");
    Writer fw = new FileWriter(touchFile);
    fw.close();
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

@Override
public synchronized void delete(T object) {
    int docNum = getDocNum(object);
    if (docNum != -1) {
        try {//from w ww. j  a v  a  2s .  co  m
            String uniqueIndexFieldName = getUniqueIndexFieldName();
            String uniqueIndexFieldValue = getUniqueIndexFieldValue(object);
            Directory directory = FSDirectory.open(indexDir);
            if (DirectoryReader.indexExists(directory)) {
                Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
                IndexWriter indexWriter = new IndexWriter(directory,
                        new IndexWriterConfig(Version.LUCENE_44, analyzer));
                Term term = new Term(uniqueIndexFieldName, uniqueIndexFieldValue);
                indexWriter.deleteDocuments(term);
                indexWriter.close();
            }
            directory.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

private void createIndexIfNecessary() {
    try {//from w  w  w.j  a v  a  2s.c o m
        Directory directory = FSDirectory.open(indexDir);
        if (!DirectoryReader.indexExists(directory)) {
            Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
            IndexWriter indexWriter = new IndexWriter(directory,
                    new IndexWriterConfig(Version.LUCENE_44, analyzer));
            indexWriter.addDocument(new Document());
            indexWriter.close();
        }
        directory.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.doculibre.constellio.lucene.impl.SkosIndexHelperImpl.java

License:Open Source License

@Override
public void delete(Thesaurus thesaurus) {
    try {//from   w  w  w .  j a v a2s .  c  o  m
        Directory directory = FSDirectory.open(getIndexDir());
        if (DirectoryReader.indexExists(directory)) {
            Analyzer analyzer = getAnalyzerProvider().getAnalyzer(Locale.FRENCH);
            IndexWriter indexWriter = new IndexWriter(directory,
                    new IndexWriterConfig(Version.LUCENE_44, analyzer));
            Term term = new Term(THESAURUS_ID, thesaurus.getId().toString());
            indexWriter.deleteDocuments(term);
            indexWriter.close();
        }
        directory.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.alvanson.xltsearch.SelectTask.java

License:Apache License

private Map<String, String> getHashSums() {
    Map<String, String> hashSums = new HashMap<>();
    DirectoryReader ireader = null;/*from  ww  w .  ja  va 2s .co  m*/
    try {
        if (DirectoryReader.indexExists(config.getDirectory())) {
            // read hashsums from `directory`
            ireader = DirectoryReader.open(config.getDirectory());
            IndexSearcher isearcher = new IndexSearcher(ireader);
            Query query = new MatchAllDocsQuery();
            ScoreDoc[] hits = isearcher.search(query, ireader.numDocs() + 1).scoreDocs;
            // collect results
            for (ScoreDoc hit : hits) {
                Document document = isearcher.doc(hit.doc);
                String relPath = document.get(config.pathField);
                String hashSum = document.get(config.hashSumField);
                if (relPath != null && hashSum != null) {
                    hashSums.put(relPath, hashSum);
                }
            }
        } // else: return empty map
    } catch (IOException ex) {
        logger.error("I/O exception while reading index", ex);
    }
    if (ireader != null) {
        try {
            ireader.close();
        } catch (IOException ex) {
            logger.warn("I/O exception while closing index reader", ex);
        }
    }
    return hashSums;
}