@Test public void searchAfterDocumentAddedIndexReaderReopen() throws CorruptIndexException, IOException { // IndexReader를 얻어온다. IndexReader indexReader = IndexReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Assert.assertTrue(indexReader.isCurrent()); Term t = new Term("ids", "4"); Query q = new TermQuery(t); TopDocs docs = indexSearcher.search(q, 10); Assert.assertEquals(0, docs.totalHits); // 삭제 후 다시 검색 해 본다. addDocument(); Assert.assertFalse(indexReader.isCurrent()); IndexReader newReader = indexReader.reopen(); if (newReader != indexReader) { indexSearcher = new IndexSearcher(newReader); indexReader.close(); } docs = indexSearcher.search(q, 10); Assert.assertEquals(1, docs.totalHits); }
public static SimpleOrderedMap<Object> getIndexInfo(IndexReader reader, boolean countTerms) throws IOException { Directory dir = reader.directory(); SimpleOrderedMap<Object> indexInfo = new SimpleOrderedMap<Object>(); indexInfo.add("numDocs", reader.numDocs()); indexInfo.add("maxDoc", reader.maxDoc()); if (countTerms) { TermEnum te = null; try { te = reader.terms(); int numTerms = 0; while (te.next()) { numTerms++; } indexInfo.add("numTerms", numTerms); } finally { if (te != null) te.close(); } } indexInfo.add( "version", reader.getVersion()); // TODO? Is this different then: IndexReader.getCurrentVersion( dir )? indexInfo.add("optimized", reader.isOptimized()); indexInfo.add("current", reader.isCurrent()); indexInfo.add("hasDeletions", reader.hasDeletions()); indexInfo.add("directory", dir); indexInfo.add("lastModified", new Date(IndexReader.lastModified(dir))); return indexInfo; }
private List getHitIds(Query query) throws IOException { ArrayList matchIds = new ArrayList(); LinkedHashSet matchIdsSet = new LinkedHashSet(); if (query != null) { if (r != null) { if (!r.isCurrent()) { while (r.isLocked(dir)) ; r = IndexReader.open(dir); searcher = new IndexSearcher(r); } } else { r = IndexReader.open(dir); searcher = new IndexSearcher(r); } DocumentCollector s = new DocumentCollector(searcher); searcher.search(query, s); // searcher.search(query); searcher.close(); List hits = s.getStudies(); for (int i = 0; i < hits.size(); i++) { Document d = (Document) hits.get(i); Field authority = d.getField("authority"); String authorityStr = authority.stringValue(); Field studyId = d.getField("studyId"); String studyIdStr = studyId.stringValue(); /* if (studyIdStr.length() < 5) { StringBuffer pad = new StringBuffer(); for (int j = studyIdStr.length(); j < 5; j++) { pad.append('0'); } studyIdStr = pad.toString() + studyIdStr; } */ // Long studyIdLong = Long.valueOf(studyIdStr); String fileName = authorityStr + File.separator + studyIdStr; // System.out.println(fileName); matchIdsSet.add(fileName); } searcher.close(); } matchIds.addAll(matchIdsSet); return matchIds; }
/** @see LuceneIndexReader#isCurrent() */ public boolean isCurrent() throws IOException { return indexReader.isCurrent(); }