/** Open an IndexWriter, executing error handling as needed. */ private IndexWriter openIndexWriter(File searchIndexPath, boolean create) throws IOException { // NFS doesn't work with Lucene default locking as of Lucene 3.3, so use // SimpleFSLockFactory instead. LockFactory lockFactory = new SimpleFSLockFactory(); FSDirectory fsDirectory = FSDirectory.open(searchIndexPath, lockFactory); IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(fsDirectory, this.retrieveIndexWriterConfig(create)); } catch (LockObtainFailedException e) { logger.warn( "Unable to obtain lock for " + searchIndexPath.getAbsolutePath() + ". Attempting to forcibly unlock the index."); if (IndexWriter.isLocked(fsDirectory)) { try { IndexWriter.unlock(fsDirectory); logger.info( "Successfully unlocked search directory " + searchIndexPath.getAbsolutePath()); } catch (IOException ex) { logger.warn( "Unable to unlock search directory " + searchIndexPath.getAbsolutePath() + " " + ex.toString()); } } } if (indexWriter == null) { // try again, there could have been a stale lock indexWriter = new IndexWriter(fsDirectory, this.retrieveIndexWriterConfig(create)); } return indexWriter; }
private IndexWriter createWriter() throws IOException { IndexWriter indexWriter = null; try { // release locks when started if (IndexWriter.isLocked(store.directory())) { logger.warn("shard is locked, releasing lock"); IndexWriter.unlock(store.directory()); } boolean create = !IndexReader.indexExists(store.directory()); indexWriter = new IndexWriter( store.directory(), analysisService.defaultIndexAnalyzer(), create, deletionPolicy, IndexWriter.MaxFieldLength.UNLIMITED); indexWriter.setMergeScheduler(mergeScheduler.newMergeScheduler()); indexWriter.setMergePolicy(mergePolicyProvider.newMergePolicy(indexWriter)); indexWriter.setSimilarity(similarityService.defaultIndexSimilarity()); indexWriter.setRAMBufferSizeMB(indexingBufferSize.mbFrac()); indexWriter.setTermIndexInterval(termIndexInterval); } catch (IOException e) { safeClose(indexWriter); throw e; } return indexWriter; }
/** @param indexWriter */ protected void closeIndexWrite(IndexWriter indexWriter) { if (indexWriter != null) { try { indexWriter.close(); if (IndexWriter.isLocked(indexWriter.getDirectory())) { IndexWriter.unlock(indexWriter.getDirectory()); } } catch (IOException e) { e.printStackTrace(); } } }
/** * 打开索引目录 * * @param luceneDir * @return * @throws IOException */ public static FSDirectory openFSDirectory(String luceneDir) { FSDirectory directory = null; try { directory = FSDirectory.open(Paths.get(luceneDir)); /** * 注意:isLocked方法内部会试图去获取Lock,如果获取到Lock,会关闭它,否则return false表示索引目录没有被锁, * 这也就是为什么unlock方法被从IndexWriter类中移除的原因 */ IndexWriter.isLocked(directory); } catch (IOException e) { e.printStackTrace(); } return directory; }
/** 关闭索引 */ public void close() { try { try { writer.close(); } finally { if (IndexWriter.isLocked(dir)) { IndexWriter.unlock(dir); } } dir.close(); } catch (IOException e) { logger.error("IOException: " + e.getMessage()); } }
private void _checkLuceneDir() { if (SearchEngineUtil.isIndexReadOnly()) { return; } try { Directory directory = getLuceneDir(); if (IndexWriter.isLocked(directory)) { IndexWriter.unlock(directory); } } catch (Exception e) { _log.error("Check Lucene directory failed for " + _companyId, e); } }
private void removeLocks() { for (IndexType indexType : IndexType.values()) { Directory dir = null; try { dir = FSDirectory.open(getIndexDirectory(indexType)); if (IndexWriter.isLocked(dir)) { IndexWriter.unlock(dir); LOG.info("Removed Lucene lock file in " + dir); } } catch (Exception x) { LOG.warn("Failed to remove Lucene lock file in " + dir, x); } finally { FileUtil.closeQuietly(dir); } } }
public static void checkIndex(ESLogger logger, Store store, ShardId shardId) { if (store.tryIncRef()) { logger.info("start check index"); try { Directory dir = store.directory(); if (!Lucene.indexExists(dir)) { return; } if (IndexWriter.isLocked(dir)) { ESTestCase.checkIndexFailed = true; throw new IllegalStateException("IndexWriter is still open on shard " + shardId); } try (CheckIndex checkIndex = new CheckIndex(dir)) { BytesStreamOutput os = new BytesStreamOutput(); PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name()); checkIndex.setInfoStream(out); out.flush(); CheckIndex.Status status = checkIndex.checkIndex(); if (!status.clean) { ESTestCase.checkIndexFailed = true; logger.warn( "check index [failure] index files={}\n{}", Arrays.toString(dir.listAll()), new String(os.bytes().toBytes(), StandardCharsets.UTF_8)); throw new IOException("index check failure"); } else { if (logger.isDebugEnabled()) { logger.debug( "check index [success]\n{}", new String(os.bytes().toBytes(), StandardCharsets.UTF_8)); } } } } catch (Exception e) { logger.warn("failed to check index", e); } finally { logger.info("end check index"); store.decRef(); } } }
/** * call this method once you are done with the index writer. Once all consumers have returned * their instance, the writer will be closed and disposed * * @param writer * @throws CorruptIndexException * @throws IOException */ public void returnIndexWriter(IndexWriter writer) throws CorruptIndexException, IOException { if (!lock.isHeldByCurrentThread()) throw new IllegalStateException( "The calling thread isn't the one that obtained the writer initially"); if (writer != indexWriter) throw new IllegalStateException("The given index writer is not the current index writer"); if (indexWriter != null) { try { indexWriter.close(); } finally { indexWriter = null; try { if (IndexWriter.isLocked(directory)) { IndexWriter.unlock(directory); } } finally { lock.unlock(); } } } }
/** @param args */ public static void main(String[] args) { // 指定索引分词技术,这里使用的是标准分词 Analyzer anlyzer = new StandardAnalyzer(Version.LUCENE_43); // indexwriter 配置信息 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_43, anlyzer); // 索引的打开方式,没有索引文件就新建,有就打开 indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); Directory directory = null; IndexWriter indexWrite = null; try { // 指定索引硬盘存储路径 directory = FSDirectory.open(new File("/study/index/testindex")); // 如果索引处于锁定状态,则解锁 if (IndexWriter.isLocked(directory)) { IndexWriter.unlock(directory); } // 指定所以操作对象indexWrite indexWrite = new IndexWriter(directory, indexWriterConfig); } catch (Exception e) { e.printStackTrace(); } // 创建文档一 Document doc1 = new Document(); // 对name域赋值“测试标题”,存储域值信息 doc1.add(new TextField("name", "测试标题", Store.YES)); // 对content域赋值“测试标题”,存储域值信息 doc1.add(new TextField("content", "测试内容", Store.YES)); try { // 将文档写入到索引中 indexWrite.addDocument(doc1); } catch (Exception e) { e.printStackTrace(); } // 创建文档二 Document doc2 = new Document(); doc2.add(new TextField("name", "基于lucene的案例开发:索引数学模型", Store.YES)); doc2.add( new TextField( "content", "lucene将一篇文档分成若干个域,每个域又分成若干个词元,通过词元在文档中的重要程度,将文档转化为N维的空间向量,通过计算两个向量之间的夹角余弦值来计算两个文档的相似程度", Store.YES)); try { // 将文档写入到索引中 indexWrite.addDocument(doc2); } catch (Exception e) { e.printStackTrace(); } // 将indexWrite操作提交,如果不提交,之前的操作将不会保存到硬盘 try { // 这一步很消耗系统资源,所以commit操作需要有一定的策略 indexWrite.commit(); // 关闭资源 indexWrite.close(); directory.close(); } catch (Exception e) { e.printStackTrace(); } }