Search in sources :

Example 1 with SearcherTaxonomyManager

use of org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager in project nrtsearch by Yelp.

the class ShardState method start.

/**
 * Start this shard as standalone (not primary nor replica)
 */
public synchronized void start() throws Exception {
    if (isStarted()) {
        throw new IllegalStateException("index \"" + name + "\" was already started");
    }
    try {
        if (indexState.saveLoadState == null) {
            indexState.initSaveLoadState();
        }
        Path indexDirFile;
        if (rootDir == null) {
            indexDirFile = null;
        } else {
            indexDirFile = rootDir.resolve("index");
        }
        origIndexDir = indexState.df.open(indexDirFile, indexState.globalState.getConfiguration().getPreloadConfig());
        // nocommit remove NRTCachingDir too?
        if ((origIndexDir instanceof MMapDirectory) == false) {
            double maxMergeSizeMB = indexState.getDoubleSetting("nrtCachingDirectoryMaxMergeSizeMB", 5.0);
            double maxSizeMB = indexState.getDoubleSetting("nrtCachingDirectoryMaxSizeMB", 60.0);
            if (maxMergeSizeMB > 0 && maxSizeMB > 0) {
                indexDir = new NRTCachingDirectory(origIndexDir, maxMergeSizeMB, maxSizeMB);
            } else {
                indexDir = origIndexDir;
            }
        } else {
            indexDir = origIndexDir;
        }
        // Rather than rely on IndexWriter/TaxonomyWriter to
        // figure out if an index is new or not by passing
        // CREATE_OR_APPEND (which can be dangerous), we
        // already know the intention from the app (whether
        // it called createIndex vs openIndex), so we make it
        // explicit here:
        IndexWriterConfig.OpenMode openMode;
        if (doCreate) {
            // nocommit shouldn't we set doCreate=false after we've done the create?  make test!
            openMode = IndexWriterConfig.OpenMode.CREATE;
        } else {
            openMode = IndexWriterConfig.OpenMode.APPEND;
        }
        Path taxoDirFile;
        if (rootDir == null) {
            taxoDirFile = null;
        } else {
            taxoDirFile = rootDir.resolve("taxonomy");
        }
        taxoDir = indexState.df.open(taxoDirFile, indexState.globalState.getConfiguration().getPreloadConfig());
        taxoSnapshots = new PersistentSnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy(), taxoDir, IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        taxoWriter = new DirectoryTaxonomyWriter(taxoDir, openMode) {

            @Override
            protected IndexWriterConfig createIndexWriterConfig(IndexWriterConfig.OpenMode openMode) {
                IndexWriterConfig iwc = super.createIndexWriterConfig(openMode);
                iwc.setIndexDeletionPolicy(taxoSnapshots);
                return iwc;
            }

            @Override
            protected IndexWriter openIndexWriter(Directory dir, IndexWriterConfig iwc) throws IOException {
                IndexWriter w = super.openIndexWriter(dir, iwc);
                taxoInternalWriter = w;
                return w;
            }
        };
        writer = new IndexWriter(indexDir, indexState.getIndexWriterConfig(openMode, origIndexDir, shardOrd));
        snapshots = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
        // loads its commits after writer calls .onInit:
        for (IndexCommit c : snapshots.getSnapshots()) {
            long gen = c.getGeneration();
            SegmentInfos sis = SegmentInfos.readCommit(origIndexDir, IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
            snapshotGenToVersion.put(c.getGeneration(), sis.getVersion());
        }
        // nocommit must also pull snapshots for taxoReader?
        manager = new SearcherTaxonomyManager(writer, true, new ShardSearcherFactory(true, true), taxoWriter);
        restartReopenThread();
        startSearcherPruningThread(indexState.globalState.getShutdownLatch());
        started = true;
    } finally {
        if (!started) {
            IOUtils.closeWhileHandlingException(reopenThread, manager, writer, taxoWriter, searcherPruningThread, slm, indexDir, taxoDir);
            writer = null;
        }
    }
}
Also used : Path(java.nio.file.Path) SegmentInfos(org.apache.lucene.index.SegmentInfos) KeepOnlyLastCommitDeletionPolicy(org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy) PersistentSnapshotDeletionPolicy(org.apache.lucene.index.PersistentSnapshotDeletionPolicy) IOException(java.io.IOException) MMapDirectory(org.apache.lucene.store.MMapDirectory) IndexCommit(org.apache.lucene.index.IndexCommit) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) IndexWriter(org.apache.lucene.index.IndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) LiveIndexWriterConfig(org.apache.lucene.index.LiveIndexWriterConfig) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 2 with SearcherTaxonomyManager

use of org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager in project exist by eXist-db.

the class LuceneIndex method open.

@Override
public void open() throws DatabaseConfigurationException {
    Path dir = getDataDir().resolve(getDirName());
    Path taxoDir = dir.resolve(TAXONOMY_DIR_NAME);
    if (LOG.isDebugEnabled())
        LOG.debug("Opening Lucene index directory: {}", dir.toAbsolutePath().toString());
    IndexWriter writer = null;
    try {
        if (Files.exists(dir)) {
            if (!Files.isDirectory(dir))
                throw new DatabaseConfigurationException("Lucene index location is not a directory: " + dir.toAbsolutePath().toString());
        } else {
            Files.createDirectories(taxoDir);
        }
        directory = FSDirectory.open(dir.toFile());
        taxoDirectory = FSDirectory.open(taxoDir.toFile());
        final IndexWriterConfig idxWriterConfig = new IndexWriterConfig(LUCENE_VERSION_IN_USE, defaultAnalyzer);
        idxWriterConfig.setRAMBufferSizeMB(bufferSize);
        cachedWriter = new IndexWriter(directory, idxWriterConfig);
        cachedTaxonomyWriter = new DirectoryTaxonomyWriter(taxoDirectory);
        searcherManager = new SearcherTaxonomyManager(cachedWriter, true, null, cachedTaxonomyWriter);
        readerManager = new ReaderManager(cachedWriter, true);
    } catch (IOException e) {
        throw new DatabaseConfigurationException("Exception while reading lucene index directory: " + e.getMessage(), e);
    } finally {
        releaseWriter(writer);
    }
}
Also used : Path(java.nio.file.Path) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) IOException(java.io.IOException) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 SearcherTaxonomyManager (org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager)2 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)2 IndexCommit (org.apache.lucene.index.IndexCommit)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 KeepOnlyLastCommitDeletionPolicy (org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy)1 LiveIndexWriterConfig (org.apache.lucene.index.LiveIndexWriterConfig)1 PersistentSnapshotDeletionPolicy (org.apache.lucene.index.PersistentSnapshotDeletionPolicy)1 SegmentInfos (org.apache.lucene.index.SegmentInfos)1 Directory (org.apache.lucene.store.Directory)1 MMapDirectory (org.apache.lucene.store.MMapDirectory)1 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)1 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)1