List of usage examples for org.apache.lucene.index IndexReader close
@Override public final synchronized void close() throws IOException
From source file:drakkar.mast.retrieval.SVNContext.java
/** * {@inheritDoc}/*from w w w. ja v a 2s. com*/ */ public boolean loadIndex(File indexPath) throws IOException, IndexException { IndexReader reader = null; boolean flag = false; if (!indexPath.isDirectory() || !indexPath.exists() || indexPath == null || IndexReader.indexExists(FSDirectory.open(indexPath)) == false) { message = "Not found index in default index path"; OutputMonitor.printLine(message, OutputMonitor.ERROR_MESSAGE); throw new IndexException(message); } else { reader = IndexReader.open(FSDirectory.open(indexPath)); loadedDocs = reader.numDocs(); reader.close(); message = "Loading SVN index..."; OutputMonitor.printLine(message, OutputMonitor.INFORMATION_MESSAGE); this.notifyTaskProgress(INFORMATION_MESSAGE, message); try { Thread.sleep(2000); } catch (InterruptedException ex) { message = "Error loading index: " + ex.toString(); OutputMonitor.printLine(message, OutputMonitor.ERROR_MESSAGE); this.notifyTaskProgress(ERROR_MESSAGE, message); } message = "Total of documents of the index: " + loadedDocs; OutputMonitor.printLine(message, OutputMonitor.INFORMATION_MESSAGE); this.notifyTaskProgress(INFORMATION_MESSAGE, message); flag = true; this.notifyLoadedDocument(loadedDocs); } return flag; }
From source file:drakkar.mast.retrieval.SVNContext.java
/** * {@inheritDoc}//from w w w .j a v a 2 s . co m */ public boolean loadIndex() throws IndexException, IOException { IndexReader reader = null; File defaultFile = new File(this.defaultIndexPath); boolean flag = false; if (!defaultFile.isDirectory() || !defaultFile.exists() || defaultFile == null || IndexReader.indexExists(FSDirectory.open(defaultFile)) == false) { message = "Not found index in default index path"; OutputMonitor.printLine(message, OutputMonitor.ERROR_MESSAGE); throw new IndexException(message); } else { reader = IndexReader.open(FSDirectory.open(defaultFile)); loadedDocs = reader.numDocs(); reader.close(); message = "Loading SVN index..."; OutputMonitor.printLine(message, OutputMonitor.INFORMATION_MESSAGE); this.notifyTaskProgress(INFORMATION_MESSAGE, message); try { Thread.sleep(2000); } catch (InterruptedException ex) { message = "Error loading index: " + ex.toString(); OutputMonitor.printLine(message, OutputMonitor.ERROR_MESSAGE); this.notifyTaskProgress(ERROR_MESSAGE, message); } message = "Total of documents of the index: " + loadedDocs; OutputMonitor.printLine(message, OutputMonitor.INFORMATION_MESSAGE); this.notifyTaskProgress(INFORMATION_MESSAGE, message); flag = true; this.notifyLoadedDocument(loadedDocs); } return flag; }
From source file:edu.cmu.cs.in.search.HoopLuceneSearch.java
License:Apache License
/** * /*from w w w .j av a 2 s. c om*/ */ public void runSearch(String[] args) throws Exception { debug("runSearch ()"); String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/java/4_0/demo.html for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { debug(usage); System.exit(0); } String index = "index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } } IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user debug("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); debug("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); debug("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
public static Map<String, Double> getAllFilesFrequency(final CaseFacade caseFacade) throws IOException { Map<String, Double> map = new HashMap<String, Double>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); TermEnum te = indexReader.terms(new Term(IndexingConstant.FILE_PATH, "")); while (te.next()) { Term currentTerm = te.term();/*w w w . j av a 2s .c o m*/ if (!currentTerm.field().equals(IndexingConstant.FILE_PATH)) continue; String file = currentTerm.text(); String fullPath = caseFacade.getFullPath(file); String ext = FileUtil.getExtension(fullPath); if (ext == null || ext.length() > 6) // no more extension than 5 character! continue; ext = ext.toLowerCase(); if (map.get(ext) == null) { map.put(ext, 1.0); } else map.put(ext, map.get(ext) + 1); } te.close(); indexReader.close(); return map; }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
public static List<String> getAllFilePaths(final CaseFacade caseFacade) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); if (!relativePath.isEmpty()) { String fullpath = caseFacade.getFullPath(relativePath); files.add(fullpath); }/*from w w w . j av a 2 s . c o m*/ } } } } indexReader.close(); return files; }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
private static List<String> getAllFilePathsHaveAuther(final CaseFacade caseFacade, final List<String> authers) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); String auther = document.get("Author"); if (!relativePath.isEmpty() && auther != null && !auther.trim().isEmpty() && Utilities.isFound(authers, auther)) { String fullpath = caseFacade.getFullPath(relativePath); files.add(fullpath); }/* ww w . j av a 2 s.c o m*/ } } } } indexReader.close(); return files; }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
public static List<String> getAllAuthers(final CaseFacade caseFacade) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); String auther = document.get("Author"); if (!relativePath.isEmpty() && auther != null && !auther.trim().isEmpty()) { files.add(auther); }//from w ww .ja v a2s. com } } } } indexReader.close(); return files; }
From source file:edu.coeia.tasks.CaseDuplicationTask.java
License:Open Source License
private void fillCaseDuplicationMap() throws Exception { String indexDir = this.aCase.getCaseLocation() + File.separator + ApplicationConstants.CASE_INDEX_FOLDER; Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); try {//from w w w . ja v a 2s. co m for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_HASH); if (field != null && field.stringValue() != null) { String documentHash = field.stringValue(); this.panel.getCaseDuplicationMap().put(documentHash, document.get(IndexingConstant.DOCUMENT_ID)); } } } } finally { indexReader.close(); } }
From source file:edu.coeia.tasks.CommonKeywordsTask.java
License:Open Source License
public Map<String, Integer> getAllTermFreqFromItems() throws IOException { Map<String, Integer> map = new HashMap<String, Integer>(); String indexDir = this.aCase.getCaseLocation() + File.separator + ApplicationConstants.CASE_INDEX_FOLDER; Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); TermEnum terms = indexReader.terms(); int factor = indexReader.maxDoc() / 100; while (terms.next()) { if (isCancelledTask()) break; Term term = terms.term();/*from ww w. j a v a 2 s . co m*/ if (this.isAllowedFeild(term.field().trim())) { String termText = term.text(); int frequency = indexReader.docFreq(term); if (frequency >= factor) map.put(termText, frequency); } } System.out.println("map size: " + map.size()); indexReader.close(); return map; }
From source file:edu.coeia.tasks.EmailProcessingTask.java
License:Open Source License
private void getAllMessageInEmailSource(final String value, final String constant) { IndexReader indexReader = null; try {//from w w w . java2 s.co m List<Integer> ids = new ArrayList<Integer>(); Directory dir = FSDirectory.open(new File(this.panel.getCaseFacade().getCaseIndexFolderLocation())); indexReader = IndexReader.open(dir); Map<Entry, Integer> messageCounter = new HashMap<Entry, Integer>(); for (int i = 0; i < indexReader.maxDoc(); i++) { if (this.isCancelledTask()) { break; } Document document = indexReader.document(i); if (document != null) { Field field = document.getField(constant); if (field != null && field.stringValue() != null) { String tmp = field.stringValue(); if (tmp.endsWith(value)) { EmailItem item = (EmailItem) ItemFactory.newInstance(document, panel.getCaseFacade(), false); String to = ""; if (item.getTo() == null || item.getTo().trim().isEmpty()) to = value; else to = item.getTo(); if (checkingItemType(item)) { Entry entry = new Entry(item.getFrom(), to, item.getTime()); Integer indexNo = messageCounter.get(entry); if (indexNo == null) { messageCounter.put(entry, 1); } else { messageCounter.put(entry, indexNo + 1); } ids.add(Integer.valueOf(item.getDocumentId())); } } } } } if (this.type == EMAIL_PROCESSING_TYPE.INBOX || this.type == EMAIL_PROCESSING_TYPE.SEND_ITEM) addResultToTable(messageCounter); else addDateResultToTable(messageCounter); messageCounter.clear(); messageCounter = null; } catch (IOException ex) { ex.printStackTrace(); Logger.getLogger(EmailProcessingTask.class.getName()).log(Level.SEVERE, null, ex); } finally { try { indexReader.close(); } catch (IOException ex) { Logger.getLogger(EmailProcessingTask.class.getName()).log(Level.SEVERE, null, ex); } } }