[lucene] 为什么这样删除lucene索引文件会出错?

mniz 2010-12-25
请教下各位,为什么我这样删除lucene 的索引,导致所有的数据都查询不到,
我这里只想删除前两天新建的索引文件

如果知道的还请告诉下小弟呀

IndexSearcher searcher =  RemoveIndex.getInstance(Tools.INDEX_PIC_DIR);
		Analyzer analyzer = new PaodingAnalyzer();
		IndexReader reader = null;
		IndexWriter indexwriter = null;
		Tools.createDirectory();
		Directory directory = FSDirectory.getDirectory(Tools.INDEX_DIR_PIC);
		indexwriter = new IndexWriter(Tools.INDEX_DIR_PIC, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
		boolean isLocked = IndexWriter.isLocked(directory);
		if (isLocked) {
			IndexWriter.unlock(directory);
		}
			
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Sort sort = new Sort();
			SortField sortfield = new SortField("add_time", SortField.INT, true);
			sort.setSort(sortfield);
			reader = searcher.getIndexReader();
			Term term1 = new Term("add_time", strbegin);
			Term term2 = new Term("add_time", strend);
			RangeQuery rq = new RangeQuery(term2, term1, false);
			 
			TopDocs topc = searcher.search(rq,1000000);
			ScoreDoc[] docs = topc.scoreDocs;
			log.debug(topc.totalHits+">>>>>>>>>>>>>>>>");
			if (docs != null && topc.totalHits > 0) {
				for (int i = 0; i < docs.length; i++) {
					if (docs[i] != null) {
						int docId = docs[i].doc;
						reader.deleteDocument(docId);
					}
					log.debug(i+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				}
			}
			int total = Integer.parseInt(Tools.readContent(Tools.LAST_NUMBER_PIC));
			int num = topc.totalHits ;
			Tools.writeContent(String.valueOf(total-num),Tools.LAST_NUMBER_PIC);
			reader.close();
			indexwriter.optimize();
			indexwriter.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}



luckaway 2010-12-29
IndexReader和IndexWriter不能同时更新索引的

#         boolean isLocked = IndexWriter.isLocked(directory); 
#         if (isLocked) { 
#             IndexWriter.unlock(directory); 
#         } 

IndexWriter.unlock(directory); 应该只是删除lock文件,不会释放资源。
删除索引最好统一用IndexWriter去操作
Global site tag (gtag.js) - Google Analytics