List of usage examples for org.apache.lucene.index DirectoryReader indexExists
public static boolean indexExists(Directory directory) throws IOException
true
if an index likely exists at the specified directory. From source file:com.github.rnewson.couchdb.lucene.DatabaseIndexer.java
License:Apache License
private UpdateSequence getUpdateSequence(final Directory dir) throws IOException { if (!DirectoryReader.indexExists(dir)) { return UpdateSequence.START; }//from w w w.j a v a2 s. c o m final List<IndexCommit> commits = DirectoryReader.listCommits(dir); final IndexCommit latest = commits.get(commits.size() - 1); return getUpdateSequence(latest.getUserData()); }
From source file:com.globalsight.ling.lucene.Index.java
License:Apache License
public boolean exists() throws IOException { return DirectoryReader.indexExists(m_fsDir); }
From source file:com.globalsight.ling.lucene.Index.java
License:Apache License
/** Opens or creates this index. */ public void open() throws IOException { synchronized (m_state) { if (m_state != STATE_CLOSED) { throw new IOException("index is open"); }/* w w w . j a v a2 s. co m*/ m_state = STATE_OPENING; } try { if (!DirectoryReader.indexExists(m_fsDir)) { // create empty index, close it. IndexWriter tempWriter = getIndexWriter(true); tempWriter.close(); tempWriter = null; } } finally { m_state = STATE_OPENED; } }
From source file:com.globalsight.ling.tm2.lucene.LuceneCache.java
License:Apache License
/** * For one dir searcher/* w ww. java2 s. c om*/ * * @param path */ public static LuceneCache getLuceneCache(File path) throws IOException { if (path == null) { return null; } String p = path.getPath(); if (cache.containsKey(p)) { LuceneCache lc = cache.get(p); return lc; } NIOFSDirectory dir = new NIOFSDirectory(path); if (dir != null && DirectoryReader.indexExists(dir)) { // if (IndexWriter.isLocked(dir)) // { // IndexWriter.unlock(dir); // } // clean lock // dir.clearLock(name); IndexReader iR = DirectoryReader.open(dir); IndexSearcher iS = new IndexSearcher(iR); LuceneCache lc = new LuceneCache(p, iR, iS); iR.addReaderClosedListener(lc); cache.put(p, lc); return lc; } else { return null; } }
From source file:com.globalsight.ling.tm2.lucene.LuceneIndexWriter.java
License:Apache License
/** * The constructor gets a lock on the index directory. If the * directory doesn't exist yet, it is created. When an operation * is done, close() method must be called to remove the lock. * //from w ww . ja v a 2 s . c om * @param p_tmId TM id * @param p_locale locale of the index */ public LuceneIndexWriter(long p_tmId, GlobalSightLocale p_locale, boolean p_isFirst) throws Exception { m_tmId = p_tmId; m_analyzer = new GsPerFieldAnalyzer(p_locale); m_isFirst = p_isFirst; m_indexDir = LuceneUtil.getGoldTmIndexDirectory(p_tmId, p_locale, true); // get the directory. Note that the directory cannot be // created before getting a lock. Note2: // FSDirectory.getDirectory(dir, true) doesn't really create // index files. It just clear out the old index files and lock // file. m_directory = FSDirectory.open(m_indexDir); // get a lock on the directory m_lock = m_directory.makeLock(LOCK_NAME); if (!m_lock.obtain(180000L)) { m_lock = null; throw new IOException("Index locked for write: " + m_lock); } // only after gettting a lock, create the initial index files // if it doesn't exist. if (!DirectoryReader.indexExists(m_directory)) { IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer); conf.setOpenMode(OpenMode.CREATE_OR_APPEND); boolean initSuccess = false; IndexWriter writer = null; try { writer = new IndexWriter(m_directory, conf); initSuccess = true; } catch (IndexFormatTooOldException ie) { // delete too old index File[] files = m_indexDir.listFiles(); if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { File oneFile = files[i]; if (!LuceneIndexWriter.LOCK_NAME.equals(oneFile.getName())) { oneFile.delete(); } } } writer = new IndexWriter(m_directory, conf); initSuccess = true; } finally { if (!initSuccess) { m_lock.release(); } IOUtils.closeWhileHandlingException(writer); } } }
From source file:com.mathworks.xzheng.tools.SpellCheckerExample.java
License:Apache License
public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell"); System.exit(1);//w w w . j a v a 2s .c om } String spellCheckDir = args[0]; String wordToRespell = args[1]; Directory dir = FSDirectory.open(new File(spellCheckDir)); if (!DirectoryReader.indexExists(dir)) { System.out.println("\nERROR: No spellchecker index at path \"" + spellCheckDir + "\"; please run CreateSpellCheckerIndex first\n"); System.exit(1); } SpellChecker spell = new SpellChecker(dir); //#A spell.setStringDistance(new LevensteinDistance()); //#B //spell.setStringDistance(new JaroWinklerDistance()); String[] suggestions = spell.suggestSimilar(wordToRespell, 5); //#C System.out.println(suggestions.length + " suggestions for '" + wordToRespell + "':"); for (String suggestion : suggestions) System.out.println(" " + suggestion); }
From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java
License:Open Source License
public IndexWriter createWriter(File directory, boolean doUpgrade) throws Exception { Directory dir = MMapDirectory.open(directory.toPath()); Analyzer analyzer = new SimpleAnalyzer(); // Upgrade the index in place if necessary. if (doUpgrade && DirectoryReader.indexExists(dir)) { upgradeIndex(dir);//from w ww .j a v a 2s. co m } IndexWriterConfig iwc = new IndexWriterConfig(analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); iwc.setIndexDeletionPolicy(new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())); Long totalMBs = getHost().getServiceMemoryLimitMB(getSelfLink(), MemoryLimitType.EXACT); if (totalMBs != null) { // give half to the index, the other half we keep for service caching context totalMBs = Math.max(1, totalMBs / 2); iwc.setRAMBufferSizeMB(totalMBs); } this.writer = new IndexWriter(dir, iwc); this.writer.commit(); this.indexUpdateTimeMicros = Utils.getNowMicrosUtc(); this.indexWriterCreationTimeMicros = this.indexUpdateTimeMicros; return this.writer; }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java
License:Open Source License
public IndexWriter createWriter(File directory, boolean doUpgrade) throws Exception { Analyzer analyzer = new SimpleAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); Long totalMBs = getHost().getServiceMemoryLimitMB(getSelfLink(), MemoryLimitType.EXACT); if (totalMBs != null) { long cacheSizeMB = (totalMBs * 3) / 4; cacheSizeMB = Math.max(1, cacheSizeMB); iwc.setRAMBufferSizeMB(cacheSizeMB); this.linkAccessMemoryLimitMB = totalMBs / 4; }// ww w .ja v a 2s. c om Directory dir = MMapDirectory.open(directory.toPath()); // Upgrade the index in place if necessary. if (doUpgrade && DirectoryReader.indexExists(dir)) { upgradeIndex(dir); } iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); iwc.setIndexDeletionPolicy(new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())); IndexWriter w = new IndexWriter(dir, iwc); w.commit(); synchronized (this.searchSync) { this.writer = w; this.linkAccessTimes.clear(); this.indexUpdateTimeMicros = Utils.getNowMicrosUtc(); this.indexWriterCreationTimeMicros = this.indexUpdateTimeMicros; } return this.writer; }
From source file:de.csw.linkgenerator.plugin.lucene.IndexUpdater.java
License:Open Source License
public synchronized void init(Properties config, LucenePlugin plugin, XWikiContext context) throws IOException { this.xwiki = context.getWiki(); this.context = (XWikiContext) context.clone(); this.context.setDatabase(this.context.getMainXWiki()); this.plugin = plugin; // take the first configured index dir as the one for writing // String[] indexDirs = // StringUtils.split(config.getProperty(LucenePlugin.PROP_INDEX_DIR), " // ,");//from ww w.j a v a 2 s . c om String[] dirPaths = StringUtils.split(plugin.getIndexDirs(), ","); if (dirPaths != null && dirPaths.length > 0) { try { this.indexDir = new NIOFSDirectory(new File(dirPaths[0])); } catch (IOException e) { IOException up = new IOException("Cannot create index in " + dirPaths[0], e); throw up; // he he } File f = new File(dirPaths[0]); if (!f.isDirectory()) { f.mkdirs(); this.needInitialBuild = true; } if (!DirectoryReader.indexExists(indexDir)) { this.needInitialBuild = true; } } this.indexingInterval = 1000 * Integer.parseInt(config.getProperty(LucenePlugin.PROP_INDEXING_INTERVAL, "30")); this.maxQueueSize = Integer.parseInt(config.getProperty(LucenePlugin.PROP_MAX_QUEUE_SIZE, "1000")); // Note: There's no need to open the Searcher here (with a call to // openSearcher()) as each task needing it will open it itself. }
From source file:de.csw.linkgenerator.plugin.lucene.LucenePlugin.java
License:Open Source License
/** * Creates an array of Searchers for a number of lucene indexes. * /*from www . java 2 s.c o m*/ * @param indexDirs Comma separated list of Lucene index directories to create searchers for. * @return Array of searchers */ public IndexSearcher[] createSearchers(String indexDirs) throws Exception { String[] dirPaths = StringUtils.split(indexDirs, ","); NIOFSDirectory[] dirs = new NIOFSDirectory[dirPaths.length]; for (int i = 0; i < dirPaths.length; i++) { dirs[i] = new NIOFSDirectory(new File(dirPaths[i])); } List<IndexSearcher> searchersList = new ArrayList<IndexSearcher>(); for (int i = 0; i < dirs.length; i++) { try { if (!DirectoryReader.indexExists(dirs[i])) { // If there's no index there, create an empty one; otherwise the reader // constructor will throw an exception and fail to initialize IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_40, analyzer); new IndexWriter(dirs[i], conf).close(); } DirectoryReader reader = DirectoryReader.open(dirs[i]); searchersList.add(new IndexSearcher(reader)); } catch (IOException e) { LOG.error("cannot open index " + dirs[i], e); } } return searchersList.toArray(new IndexSearcher[searchersList.size()]); }