下面列出了怎么用org.apache.lucene.index.IndexNotFoundException的API类实例代码及写法,或者点击链接到github查看源代码。
public void testExceptionDuringRefresh() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
IndexWriter w = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
w.commit();
tw.commit();
SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(indexDir, taxoDir, null);
tw.addCategory(new FacetLabel("a", "b"));
w.addDocument(new Document());
tw.commit();
w.commit();
// intentionally corrupt the taxo index:
SegmentInfos infos = SegmentInfos.readLatestCommit(taxoDir);
taxoDir.deleteFile(infos.getSegmentsFileName());
expectThrows(IndexNotFoundException.class, mgr::maybeRefreshBlocking);
IOUtils.close(w, tw, mgr, indexDir, taxoDir);
}
/**
* Returns the last {@link IndexCommit} found in the {@link Directory}, or
* {@code null} if there are no commits.
*/
public static IndexCommit getLastCommit(Directory dir) throws IOException {
try {
if (DirectoryReader.indexExists(dir)) {
List<IndexCommit> commits = DirectoryReader.listCommits(dir);
// listCommits guarantees that we get at least one commit back, or
// IndexNotFoundException which we handle below
return commits.get(commits.size() - 1);
}
} catch (IndexNotFoundException e) {
// ignore the exception and return null
}
return null;
}
public void testNoDir() throws Throwable {
Path tempDir = createTempDir("doesnotexist");
IOUtils.rm(tempDir);
try (Directory dir = getDirectory(tempDir)) {
expectThrowsAnyOf(Arrays.asList(NoSuchFileException.class, IndexNotFoundException.class), () -> {
DirectoryReader.open(dir);
});
}
}
public void testNoDir() throws Throwable {
Path primDir = createTempDir("foo");
Path secondDir = createTempDir("bar");
Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet());
expectThrows(IndexNotFoundException.class, () -> {
DirectoryReader.open(dir);
});
dir.close();
}
private List<IndexCommit> listCommits(String directory) throws Exception {
Directory dir = new NIOFSDirectory(Paths.get(directory));
try {
return DirectoryReader.listCommits(dir);
} catch (IndexNotFoundException ex) {
// This can happen when the delete snapshot functionality cleans up the index files (when the directory
// storing these files is not the *current* index directory).
return Collections.emptyList();
}
}
protected FacetedContentsResult searchContents(SearchEngineFilter[] filters,
Collection<ITreeNode> categories, Collection<String> allowedGroups, boolean faceted) throws ApsSystemException {
FacetedContentsResult result = new FacetedContentsResult();
List<String> contentsId = new ArrayList<String>();
IndexSearcher searcher = null;
try {
searcher = this.getSearcher();
Query query = null;
if ((null == filters || filters.length == 0)
&& (null == categories || categories.isEmpty())
&& (allowedGroups != null && allowedGroups.contains(Group.ADMINS_GROUP_NAME))) {
query = new MatchAllDocsQuery();
} else {
query = this.createQuery(filters, categories, allowedGroups);
}
TopDocs topDocs = searcher.search(query, 1000);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
Map<String, Integer> occurrences = new HashMap<String, Integer>();
if (scoreDocs.length > 0) {
for (int index = 0; index < scoreDocs.length; index++) {
Document doc = searcher.doc(scoreDocs[index].doc);
contentsId.add(doc.get(IIndexerDAO.DATAOBJECT_ID_FIELD_NAME));
if (faceted) {
Set<String> codes = new HashSet<String>();
String[] categoryPaths = doc.getValues(IIndexerDAO.DATAOBJECT_CATEGORY_FIELD_NAME);
for (int i = 0; i < categoryPaths.length; i++) {
String categoryPath = categoryPaths[i];
String[] paths = categoryPath.split(IIndexerDAO.DATAOBJECT_CATEGORY_SEPARATOR);
codes.addAll(Arrays.asList(paths));
}
Iterator<String> iter = codes.iterator();
while (iter.hasNext()) {
String code = iter.next();
Integer value = occurrences.get(code);
if (null == value) {
value = 0;
}
occurrences.put(code, (value + 1));
}
}
}
}
result.setOccurrences(occurrences);
result.setContentsId(contentsId);
} catch (IndexNotFoundException inf) {
logger.error("no index was found in the Directory", inf);
} catch (Throwable t) {
logger.error("Error extracting documents", t);
throw new ApsSystemException("Error extracting documents", t);
} finally {
this.releaseResources(searcher);
}
return result;
}