Example usage for org.apache.lucene.search SearcherManager SearcherManager

List of usage examples for org.apache.lucene.search SearcherManager SearcherManager

Introduction

In this page you can find the example usage for org.apache.lucene.search SearcherManager SearcherManager.

Prototype

public SearcherManager(IndexWriter writer, boolean applyAllDeletes, boolean writeAllDeletes,
        SearcherFactory searcherFactory) throws IOException 

Source Link

Document

Expert: creates and returns a new SearcherManager from the given IndexWriter , controlling whether past deletions should be applied.

Usage

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLuceneWithRegionDirectory() throws Exception {
    doTest("RegionDirectory", new TestCallbacks() {

        public Cache cache;
        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//w w w  .j a v  a  2 s. co  m
        public void init() throws Exception {
            cache = new CacheFactory().set(MCAST_PORT, "0").set(LOG_LEVEL, "warning").create();
            final FileSystemStats stats = new FileSystemStats(cache.getDistributedSystem(), "stats");
            RegionDirectory dir = new RegionDirectory(new ConcurrentHashMap<String, File>(),
                    new ConcurrentHashMap<ChunkKey, byte[]>(), stats);
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
            cache.close();
            ;
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLucene() throws Exception {
    doTest("Lucene", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//w ww.j  a  va 2  s  .co  m
        public void init() throws Exception {
            RAMDirectory dir = new RAMDirectory();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl.java

License:Apache License

public IndexRepositoryImpl(Region<?, ?> region, IndexWriter writer, LuceneSerializer serializer,
        LuceneIndexStats stats, Region<?, ?> userRegion) throws IOException {
    this.region = region;
    this.userRegion = userRegion;
    this.writer = writer;
    searcherManager = new SearcherManager(writer, APPLY_ALL_DELETES, true, null);
    this.serializer = serializer;
    this.stats = stats;
    documentCountSupplier = new DocumentCountSupplier();
    stats.addDocumentsSupplier(documentCountSupplier);
}

From source file:org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLuceneWithRegionDirectory() throws Exception {
    doTest("RegionDirectory", new TestCallbacks() {

        public Cache cache;
        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override/*w w  w . jav  a2 s .c o m*/
        public void init() throws Exception {
            cache = new CacheFactory().set(MCAST_PORT, "0").set(LOG_LEVEL, "warning").create();
            final FileSystemStats stats = new FileSystemStats(cache.getDistributedSystem(), "stats");
            RegionDirectory dir = new RegionDirectory(new ConcurrentHashMap<String, File>(),
                    new ConcurrentHashMap<ChunkKey, byte[]>(), stats);
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
            cache.close();
            ;
        }

        @Override
        public void waitForAsync() throws Exception {
            // do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLucene() throws Exception {
    doTest("Lucene", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override/*w  w  w .ja  v a 2s.c o  m*/
        public void init() throws Exception {
            RAMDirectory dir = new RAMDirectory();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            // do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:org.apache.gora.lucene.store.LuceneStore.java

License:Apache License

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties)
        throws GoraException {
    try {//from   w  w  w . j ava 2s.c om
        super.initialize(keyClass, persistentClass, properties);
    } catch (GoraException ge) {
        LOG.error(ge.getMessage(), ge);
        throw new GoraException(ge);
    }

    String mappingFile = null;
    try {
        mappingFile = DataStoreFactory.getMappingFile(properties, (DataStore<?, ?>) this, DEFAULT_MAPPING_FILE);
    } catch (IOException ioe) {
        LOG.error(ioe.getMessage(), ioe);
        throw new GoraException(ioe);
    }
    String luceneVersion = properties.getProperty(LUCENE_VERSION_KEY, DEFAULT_LUCENE_VERSION);
    String ramBuffer = properties.getProperty(LUCENE_RAM_BUFFER_KEY, DEFAULT_LUCENE_RAMBUFFER);

    LOG.debug("Lucene index version: {}", luceneVersion);
    LOG.debug("Lucene index writer RAM buffer size: {}", ramBuffer);

    try {
        mapping = readMapping(mappingFile);
    } catch (IOException ioe) {
        LOG.error(ioe.getMessage(), ioe);
        throw new GoraException(ioe);
    }
    String persistentClassObject = persistentClass.getCanonicalName();
    String dataStoreOutputPath = outputPath + "_" + persistentClassObject
            .substring(persistentClassObject.lastIndexOf('.') + 1).toLowerCase(Locale.getDefault());
    try {
        dir = FSDirectory.open(FileSystems.getDefault().getPath(dataStoreOutputPath));

        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwc.setRAMBufferSizeMB(Double.parseDouble(ramBuffer));

        writer = new IndexWriter(dir, iwc);
        //TODO do we definately want all past deletions to be applied.
        searcherManager = new SearcherManager(writer, true, true, new SearcherFactory());
    } catch (IOException e) {
        LOG.error("Error opening {} with Lucene FSDirectory.", outputPath, e);
    }
}

From source file:org.eclipse.che.api.search.server.impl.LuceneSearcher.java

License:Open Source License

@Inject
public LuceneSearcher(@Named("vfs.index_filter_matcher") Set<PathMatcher> excludePatterns,
        @Named("vfs.local.fs_index_root_dir") File indexDirectory,
        @Named("che.user.workspaces.storage") File root, PathTransformer pathTransformer) throws IOException {

    if (indexDirectory.exists()) {
        if (indexDirectory.isFile()) {
            throw new IOException("Wrong configuration `vfs.local.fs_index_root_dir` is a file");
        }/*  w ww. ja v a 2s.c o m*/
    } else {
        Files.createDirectories(indexDirectory.toPath());
    }

    this.indexDirectory = indexDirectory;
    this.root = root;
    this.excludePatterns = excludePatterns;
    this.pathTransformer = pathTransformer;
    this.analyzer = CustomAnalyzer.builder().withTokenizer(WhitespaceTokenizerFactory.class)
            .addTokenFilter(LowerCaseFilterFactory.class).build();
    this.luceneIndexWriter = new IndexWriter(
            FSDirectory.open(indexDirectory.toPath(), new SingleInstanceLockFactory()),
            new IndexWriterConfig(analyzer));
    this.searcherManager = new SearcherManager(luceneIndexWriter, true, true, new SearcherFactory());
    this.sort = new Sort(SortField.FIELD_SCORE, new SortField(PATH_FIELD, SortField.Type.STRING));
}

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

License:Apache License

protected SearcherManager searchManager(IndexWriter writer) {
    try {/*from   ww w  .  j  a va2 s  .  c  om*/
        return new SearcherManager(writer, true, true, null);
    } catch (IOException e) {
        throw new LuceneIndexException("Cannot create index writer", e);
    }
}

From source file:sh.isaac.provider.query.lucene.LuceneIndexer.java

License:Apache License

/**
 * Instantiates a new lucene indexer.//  w w  w. j a  v a2s  .  c om
 *
 * @param indexName the index name
 * @throws IOException Signals that an I/O exception has occurred.
 */
protected LuceneIndexer(String indexName) throws IOException {
    try {
        this.indexName = indexName;
        this.luceneWriterService = LookupService.getService(WorkExecutors.class).getIOExecutor();
        this.luceneWriterFutureCheckerService = Executors.newFixedThreadPool(1,
                new NamedThreadFactory(indexName + " Lucene future checker", false));

        final Path searchFolder = LookupService.getService(ConfigurationService.class).getSearchFolderPath();
        final File luceneRootFolder = new File(searchFolder.toFile(), DEFAULT_LUCENE_FOLDER);

        luceneRootFolder.mkdirs();
        this.indexFolder = new File(luceneRootFolder, indexName);

        if (!this.indexFolder.exists()) {
            this.databaseValidity = DatabaseValidity.MISSING_DIRECTORY;
            LOG.info("Index folder missing: " + this.indexFolder.getAbsolutePath());
        } else if (this.indexFolder.list().length > 0) {
            this.databaseValidity = DatabaseValidity.POPULATED_DIRECTORY;
        }

        this.indexFolder.mkdirs();
        LOG.info("Index: " + this.indexFolder.getAbsolutePath());

        final MMapDirectory indexDirectory = new MMapDirectory(this.indexFolder.toPath()); // switch over to MMapDirectory - in theory - this gives us back some
        // room on the JDK stack, letting the OS directly manage the caching of the index files - and more importantly, gives us a huge
        // performance boost during any operation that tries to do multi-threaded reads of the index (like the SOLOR rules processing) because
        // the default value of SimpleFSDirectory is a huge bottleneck.

        final IndexWriterConfig config = new IndexWriterConfig(new PerFieldAnalyzer());

        config.setRAMBufferSizeMB(256);

        final MergePolicy mergePolicy = new LogByteSizeMergePolicy();

        config.setMergePolicy(mergePolicy);
        config.setSimilarity(new ShortTextSimilarity());

        this.indexWriter = new IndexWriter(indexDirectory, config);

        final boolean applyAllDeletes = false;
        final boolean writeAllDeletes = false;

        this.searcherManager = new SearcherManager(indexWriter, applyAllDeletes, writeAllDeletes, null);

        // [3]: Create the ControlledRealTimeReopenThread that reopens the index periodically taking into
        // account the changes made to the index and tracked by the TrackingIndexWriter instance
        // The index is refreshed every 60sc when nobody is waiting
        // and every 100 millis whenever is someone waiting (see search method)
        // (see http://lucene.apache.org/core/4_3_0/core/org/apache/lucene/search/NRTManagerReopenThread.html)
        this.reopenThread = new ControlledRealTimeReopenThread<>(this.indexWriter, this.searcherManager, 60.00,
                0.1);
        this.startThread();

        // Register for commits:
        LOG.info("Registering indexer " + indexName + " for commits");
        this.changeListenerRef = new ChronologyChangeListener() {
            @Override
            public void handleCommit(CommitRecord commitRecord) {
                if (LuceneIndexer.this.dbBuildMode == null) {
                    LuceneIndexer.this.dbBuildMode = Get.configurationService().inDBBuildMode();
                }

                if (LuceneIndexer.this.dbBuildMode) {
                    LOG.debug("Ignore commit due to db build mode");
                    return;
                }

                final int size = commitRecord.getSemanticNidsInCommit().size();

                if (size < 100) {
                    LOG.info("submitting semantic elements " + commitRecord.getSemanticNidsInCommit().toString()
                            + " to indexer " + getIndexerName() + " due to commit");
                } else {
                    LOG.info("submitting " + size + " semantic elements to indexer " + getIndexerName()
                            + " due to commit");
                }

                commitRecord.getSemanticNidsInCommit().stream().forEach(sememeId -> {
                    final SemanticChronology sc = Get.assemblageService().getSemanticChronology(sememeId);

                    index(sc);
                });
                LOG.info("Completed index of " + size + " semantics for " + getIndexerName());
            }

            @Override
            public void handleChange(SemanticChronology sc) {
                // noop
            }

            @Override
            public void handleChange(ConceptChronology cc) {
                // noop
            }

            @Override
            public UUID getListenerUuid() {
                return UuidT5Generator.get(getIndexerName());
            }
        };
        Get.commitService().addChangeListener(this.changeListenerRef);
    } catch (final IOException e) {
        LookupService.getService(SystemStatusService.class).notifyServiceConfigurationFailure(indexName, e);
        throw e;
    }
}

From source file:spimedb.SpimeDB.java

License:Apache License

private SpimeDB(File file, Directory dir) {

    this.file = file;
    this.dir = dir;
    this.analyzer = new StandardAnalyzer();

    this.facetsConfig.setHierarchical(NObject.ID, true);
    this.facetsConfig.setMultiValued(NObject.ID, false);

    this.facetsConfig.setHierarchical(NObject.TAG, false);
    this.facetsConfig.setMultiValued(NObject.TAG, true);

    final String[] defaultFindFields = new String[] { NObject.NAME, NObject.DESC, NObject.TAG, NObject.ID };

    this.defaultFindQueryParser = ThreadLocal.withInitial(() -> new MultiFieldQueryParser(defaultFindFields,
            analyzer,/*from   w w w  . j  a v  a  2 s.co m*/
            Maps.mutable.with(NObject.NAME, 1f, NObject.ID, 1f, NObject.DESC, 0.25f, NObject.TAG, 0.5f)));

    writerConf = new IndexWriterConfig(analyzer);
    writerConf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    writerConf.setCommitOnClose(true);
    try {
        writer = new IndexWriter(dir, writerConf);
        readerMgr = new ReaderManager(writer, true, true);
        searcherMgr = new SearcherManager(writer, true, true, new SearcherFactory());
    } catch (IOException e) {
        e.printStackTrace();
    }

}