@Override Directory getDirectory( File baseStorePath, IndexIdentifier identifier ) throws IOException { return FSDirectory.open( getFileDirectory( baseStorePath, identifier ).toPath() ); }
void setIndexWriter() {try{ if(indexWriter == null){ dir = FSDirectory.open(indexDir.toPath()); Redwood.log(Redwood.DBG, "Updating lucene index at " + indexDir); indexWriter = new IndexWriter(dir, iwc); }}catch(IOException e){ throw new RuntimeException(e); } }
static synchronized void setIndexWriter() { try{ if(!openIndexWriter.get()){ dir = FSDirectory.open(indexDir.toPath()); Redwood.log(Redwood.DBG, "Updating lucene index at " + indexDir); indexWriter = new IndexWriter(dir, iwc); openIndexWriter.set(true); } }catch(IOException e){ throw new RuntimeException(e); } }
static Directory getDirectory( File storeDir, IndexIdentifier identifier ) throws IOException { return FSDirectory.open( getFileDirectory( storeDir, identifier ).toPath() ); }
private LuceneSearcher(File indexDir) throws IOException { Path path = indexDir.toPath(); // symlinks are not supported here, see https://issues.apache.org/jira/browse/LUCENE-6700, // so we resolve the link ourselves: if (Files.isSymbolicLink(path)) { path = indexDir.getCanonicalFile().toPath(); } this.directory = FSDirectory.open(path); this.reader = DirectoryReader.open(directory); this.searcher = new IndexSearcher(reader); } public IndexReader getReader() {
@SuppressWarnings( "ResultOfMethodCallIgnored" ) @Override public Directory open( File dir ) throws IOException { dir.mkdirs(); FSDirectory directory = USE_DEFAULT_DIRECTORY_FACTORY ? FSDirectory.open( dir.toPath() ) : new NIOFSDirectory( dir.toPath() ); return new NRTCachingDirectory( directory, MAX_MERGE_SIZE_MB, MAX_CACHED_MB ); }
private boolean indexExists(final Path indexDir) throws IOException { try (Directory indexDirectory = FSDirectory.open(indexDir)) { return DirectoryReader.indexExists(indexDirectory); } }
SuggesterProjectData( final Directory indexDir, final Path suggesterDir, final boolean allowMostPopular, final Set<String> fields ) throws IOException { this.indexDir = indexDir; this.suggesterDir = suggesterDir; this.allowMostPopular = allowMostPopular; tempDir = FSDirectory.open(Paths.get(System.getProperty(TMP_DIR_PROPERTY))); initFields(fields); }
void setIndexReaderSearcher() throws IOException { FSDirectory index = FSDirectory.open(indexDir.toPath()); if(reader == null){ reader = DirectoryReader.open(index); searcher = new IndexSearcher(reader); }else{ DirectoryReader newreader = DirectoryReader.openIfChanged(reader); if(newreader != null) { reader.close(); reader = newreader; searcher = new IndexSearcher(reader); } } }
public void checkClean(){ try { dir = FSDirectory.open(indexDir.toPath()); CheckIndex checkIndex = new CheckIndex(dir); CheckIndex.Status status = checkIndex.checkIndex(); assert (status.clean) : "index is not clean"; dir.close(); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public List<Document> call() { final List<Document> localScoreDocs = new ArrayList<>(); try (final DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(indexDirectory))) { final IndexSearcher searcher = new IndexSearcher(directoryReader); final TopDocs topDocs = searcher.search(luceneQuery, 10000000); logger.info("For {}, Top Docs has {} hits; reading Lucene results", indexDirectory, topDocs.scoreDocs.length); if (topDocs.totalHits > 0) { for (final ScoreDoc scoreDoc : topDocs.scoreDocs) { final int docId = scoreDoc.doc; final Document d = directoryReader.document(docId); localScoreDocs.add(d); } } hits.addAndGet(localScoreDocs.size()); } catch (final IndexNotFoundException e) { } catch (final IOException ioe) { throw new RuntimeException(ioe); } return localScoreDocs; } };
private Runnable getInitRunnable(final NamedIndexDir indexDir) { return () -> { try { Instant start = Instant.now(); logger.log(Level.FINE, "Initializing {0}", indexDir); SuggesterProjectData wfst = new SuggesterProjectData(FSDirectory.open(indexDir.path), getSuggesterDir(indexDir.name), allowMostPopular, allowedFields); wfst.init(); if (projectsEnabled) { projectData.put(indexDir.name, wfst); } else { projectData.put(PROJECTS_DISABLED_KEY, wfst); } Duration d = Duration.between(start, Instant.now()); logger.log(Level.FINE, "Finished initialization of {0}, took {1}", new Object[] {indexDir, d}); } catch (Exception e) { logger.log(Level.SEVERE, "Could not initialize suggester data for " + indexDir, e); } }; }
@Override public Directory open( File dir ) throws IOException { dir.mkdirs(); FSDirectory fsDir = FSDirectory.open( dir.toPath() ); return new SyncNotifierDirectory( fsDir, signal ); }
/** * Search one index. This is used if no projects are set up. * @param paging whether to use paging (if yes, first X pages will load * faster) * @param root which db to search * @throws IOException */ private void searchSingleDatabase(File root, boolean paging) throws IOException { IndexReader ireader = DirectoryReader.open(FSDirectory.open(root.toPath())); searcher = new IndexSearcher(ireader); collector = TopScoreDocCollector.create(hitsPerPage * cachePages); searcher.search(query, collector); totalHits = collector.getTotalHits(); if (!paging && totalHits > 0) { collector = TopScoreDocCollector.create(totalHits); searcher.search(query, collector); } hits = collector.topDocs().scoreDocs; for (ScoreDoc hit : hits) { int docId = hit.doc; Document d = searcher.doc(docId); docs.add(d); } }
/** * Get the index writer/searcher wrapper for the given connection. * * @param conn the connection * @return the index access wrapper */ protected static IndexAccess getIndexAccess(Connection conn) throws SQLException { String path = getIndexPath(conn); synchronized (INDEX_ACCESS) { IndexAccess access = INDEX_ACCESS.get(path); if (access == null) { try { Directory indexDir = path.startsWith(IN_MEMORY_PREFIX) ? new RAMDirectory() : FSDirectory.open(new File(path)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, analyzer); conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(indexDir, conf); //see http://wiki.apache.org/lucene-java/NearRealtimeSearch access = new IndexAccess(writer); } catch (IOException e) { throw convertException(e); } INDEX_ACCESS.put(path, access); } return access; } }
/** * Check index version in given directory. It assumes that that all commits * in the Lucene segment file were done with the same version. * * @param dir directory with index * @thows IOException if the directory cannot be opened */ private static void checkDir(File dir) throws Exception { LockFactory lockfact = NativeFSLockFactory.INSTANCE; int segVersion; try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockfact)) { SegmentInfos segInfos = null; try { segInfos = SegmentInfos.readLatestCommit(indexDirectory); segVersion = segInfos.getIndexCreatedVersionMajor(); } catch (IndexNotFoundException e) { return; } } if (segVersion != Version.LATEST.major) { throw new IndexVersionException( String.format("Directory %s has index of version %d and Lucene has %d", dir.toString(), segVersion, Version.LATEST.major)); } } }
@SuppressWarnings("PMD.CollapsibleIfStatements") private void initialize() throws IOException { synchronized (INSTANCE_LOCK) { RuntimeEnvironment env = RuntimeEnvironment.getInstance(); File indexDir = new File(env.getDataRootFile(), INDEX_DIR); if (project != null) { indexDir = new File(indexDir, project.getPath()); } if (!indexDir.exists() && !indexDir.mkdirs()) { // to avoid race conditions, just recheck.. if (!indexDir.exists()) { throw new FileNotFoundException("Failed to create root directory [" + indexDir.getAbsolutePath() + "]"); } } lockfact = pickLockFactory(env); indexDirectory = FSDirectory.open(indexDir.toPath(), lockfact); ignoredNames = env.getIgnoredNames(); includedNames = env.getIncludedNames(); analyzerGuru = new AnalyzerGuru(); xrefDir = new File(env.getDataRootFile(), XREF_DIR); listeners = new CopyOnWriteArrayList<>(); dirtyFile = new File(indexDir, "dirty"); dirty = dirtyFile.exists(); directories = new ArrayList<>(); } }
/** * 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. */ 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; }
@Override public EventIndexSearcher borrowIndexSearcher(final File indexDir) throws IOException { final File absoluteFile = indexDir.getAbsoluteFile(); final IndexWriterCount writerCount; synchronized (writerCounts) { writerCount = writerCounts.remove(absoluteFile); if (writerCount != null) { // Increment writer count and create an Index Searcher based on the writer writerCounts.put(absoluteFile, new IndexWriterCount(writerCount.getWriter(), writerCount.getAnalyzer(), writerCount.getDirectory(), writerCount.getCount() + 1, writerCount.isCloseableWhenUnused())); } } final DirectoryReader directoryReader; if (writerCount == null) { logger.trace("Creating index searcher for {}", indexDir); final Directory directory = FSDirectory.open(indexDir); directoryReader = DirectoryReader.open(directory); } else { final EventIndexWriter eventIndexWriter = writerCount.getWriter(); directoryReader = DirectoryReader.open(eventIndexWriter.getIndexWriter(), false); } final IndexSearcher searcher = new IndexSearcher(directoryReader, this.searchExecutor); logger.trace("Created index searcher {} for {}", searcher, indexDir); return new LuceneEventIndexSearcher(searcher, indexDir, null, directoryReader); }
Directory dir = FSDirectory.open(new File(indexDir, proj).toPath()); mgr = new SearcherManager(dir, new ThreadpoolSearcherFactory()); searcherManagerMap.put(proj, mgr);