List of usage examples for org.apache.lucene.index IndexWriter close
@Override public void close() throws IOException
From source file:com.dreamerpartner.codereview.lucene.IndexHelper.java
License:Apache License
/** * ??// ww w .j a va2 s . co m * @param module ? * @throws IOException */ @SuppressWarnings("deprecation") public static void merge(String module) throws IOException { long beginTime = System.currentTimeMillis(); IndexWriter writer = null; try { Directory dir = FSDirectory.open(new File(LuceneUtil.getIndexPath(module))); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(dir, iwc); //?? writer.forceMerge(1); writer.commit(); } finally { long endTime = System.currentTimeMillis(); logger.debug("merge consume " + (endTime - beginTime) + " milliseconds."); if (writer != null) writer.close(); } }
From source file:com.duroty.lucene.bookmark.indexer.BookmarkIndexer.java
License:Open Source License
/** * DOCUMENT ME!//www . ja v a2 s. c o m * * @param path DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public static Searcher getSearcher(String path) throws Exception { String optimizedPath = null; if (!path.endsWith(File.separator)) { optimizedPath = path + File.separator + OPTIMIZED_PATH_NAME + File.separator; } else { optimizedPath = path + OPTIMIZED_PATH_NAME + File.separator; } File file = new File(optimizedPath); if (!IndexReader.indexExists(file)) { file.mkdirs(); IndexWriter writer = new IndexWriter(file, null, true); writer.close(); } return new IndexSearcher(optimizedPath); }
From source file:com.duroty.lucene.bookmark.indexer.BookmarkIndexer.java
License:Open Source License
/** * DOCUMENT ME!/*from w w w . j a va 2 s . c om*/ * * @param path DOCUMENT ME! * @param id DOCUMENT ME! * @param doc DOCUMENT ME! * @param analyzer DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void insertDocument(String path, String id, Document doc, Analyzer analyzer) throws Exception { if (!path.endsWith(File.separator)) { path = path + File.separator + SIMPLE_PATH_NAME + File.separator; } else { path = path + SIMPLE_PATH_NAME + File.separator; } IndexWriter writer = null; File file = null; boolean create = false; try { file = new File(path + id); if (!IndexReader.indexExists(file)) { file.mkdirs(); create = true; } if (IndexReader.isLocked(path)) { Thread.sleep(sleepTime); if (countInsert > 5) { throw new Exception("The index lucene MainIndexer is locked insert document"); } countInsert++; insertDocument(path, id, doc, analyzer); return; } Directory dir = FSDirectory.getDirectory(file, create); writer = new IndexWriter(dir, analyzer, create); writer.setMaxFieldLength(Integer.MAX_VALUE); writer.addDocument(doc); writer.optimize(); writer.close(); writer = null; } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } //Aquest fitxer dins el directori de l'index individual ens permet saber si est indexant File unlock = new File(file, FileUtilities.FILE_IS_UNLOCK); try { unlock.createNewFile(); } catch (Exception e) { } } }
From source file:com.duroty.lucene.bookmark.indexer.BookmarkIndexer.java
License:Open Source License
/** * DOCUMENT ME!/*from w w w . j ava 2 s. c om*/ * * @param path DOCUMENT ME! * @param field DOCUMENT ME! * @param doc DOCUMENT ME! */ public static void createSpell(String path, String field, Document doc) throws Exception { RAMDirectory ramDir = null; IndexWriter writer = null; try { ramDir = new RAMDirectory(); writer = new IndexWriter(ramDir, new DictionaryAnalyzer(), true); writer.addDocument(doc); writer.optimize(); writer.close(); DidYouMeanIndexer.createSpell(field, ramDir, path); } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } }
From source file:com.duroty.service.analyzer.LuceneFiltersAnalysis.java
License:Open Source License
public void service(String repositoryName, String messageName, MimeMessage mime) throws Exception, Throwable, OutOfMemoryError { Session hsession = null;//from w ww . j av a 2 s .c o m RAMDirectory auxDir = null; try { hsession = hfactory.openSession(); auxDir = new RAMDirectory(); IndexWriter auxWriter = new IndexWriter(auxDir, analyzer, true); auxWriter.addDocument(luceneMessage.getDocPrincipal()); auxWriter.optimize(); auxWriter.close(); Vector filters = getFilters(hsession, repositoryName); boolean setbox = true; String box = message.getMesBox(); if (box.equals("SPAM")) { setbox = false; } else if (box.equals("DRAFT")) { setbox = false; } if (filters != null) { while (filters.size() > 0) { Filter filter = (Filter) filters.remove(0); IndexSearcher auxSearcher = new IndexSearcher(auxDir); org.apache.lucene.search.Query query = FilterQueryParser.parse(filter, analyzer); Hits hits = auxSearcher.search(query); if (hits.length() > 0) { //he tingut una coincidencia de filtre per tant cal dur a terme les accions assocides //al filtre if (filter.isFilArchive() && setbox) { //Marco un header per a que s'inserti a la carpeta d'archived message.setMesBox("HIDDEN"); } else if (filter.isFilTrash() && setbox) { message.setMesBox("TRASH"); } else { } if (filter.isFilImportant()) { message.setMesFlagged(new Boolean(true)); } if (filter.getLabel() != null) { LabMes labMes = new LabMes(new LabMesId(message, filter.getLabel())); message.addLabMeses(labMes); } if ((filter.getFilForwardTo() != null) && !filter.getFilForwardTo().trim().equals("")) { InternetAddress forwardTo = null; try { forwardTo = new InternetAddress(filter.getFilForwardTo()); } catch (Exception e) { forwardTo = null; } if (forwardTo != null) { try { InternetAddress recipient = (InternetAddress) mime.getFrom()[0]; forwardMailFromLabel(recipient, forwardTo, "FW: ", mime); } catch (Exception ex) { ex.printStackTrace(); } } } } } } } catch (Exception ex) { DLog.log(DLog.DEBUG, this.getClass(), ex); } finally { GeneralOperations.closeHibernateSession(hsession); if (auxDir != null) { auxDir.close(); } } }
From source file:com.duroty.service.BookmarkOptimizerThread.java
License:Open Source License
/** * DOCUMENT ME!//from www .jav a 2 s .c om * * @param childs DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ private void flush(File[] childs) throws Exception { if ((childs == null) || (childs.length == 0)) { return; } try { Thread.sleep(100); } catch (Exception e) { } File optimized = new File(optimizedPath); boolean create = false; IndexWriter writer = null; try { if (!IndexReader.indexExists(optimized)) { optimized.mkdirs(); create = true; } synchronized (this) { if (IndexReader.isLocked(optimizedPath)) { return; } else { Directory dir = FSDirectory.getDirectory(new File(optimizedPath), create); writer = new IndexWriter(dir, analyzer, create); } for (int i = 0; i < childs.length; i++) { boolean lock = true; File child = childs[i]; File[] faux = child.listFiles(); for (int j = 0; j < faux.length; j++) { if (faux[j].getName().equals("is.unlock")) { faux[j].delete(); lock = false; break; } } if (!lock) { Directory[] aux = new Directory[1]; aux[0] = FSDirectory.getDirectory(child, false); IndexSearcher searcher = null; try { searcher = new IndexSearcher(aux[0]); Document doc = searcher.doc(0); if (doc != null) { BookmarkIndexer.createSpell(userPath + SPELL, Field_title, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_keywords, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_contents, doc); BookmarkIndexer.createSpell(userPath + SPELL, Field_comments, doc); } } catch (Exception ex) { if ((ex != null) && !(ex instanceof NullPointerException)) { DLog.log(DLog.INFO, this.getClass(), ex); } } finally { if (searcher != null) { try { searcher.close(); } catch (Exception e) { } } } writer.addIndexes(aux); writer.optimize(); for (int j = 0; j < faux.length; j++) { faux[j].delete(); } child.delete(); } } writer.close(); writer = null; } } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } }
From source file:com.duroty.service.MailOptimizerThread.java
License:Open Source License
/** * DOCUMENT ME!//from w ww .jav a 2 s . co m * * @param childs DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ private void flush(File[] childs) throws Exception { if ((childs == null) || (childs.length == 0)) { return; } try { Thread.sleep(500); } catch (Exception e) { } File optimized = new File(optimizedPath); boolean create = false; IndexWriter writer = null; try { if (!IndexReader.indexExists(optimized)) { optimized.mkdirs(); create = true; } synchronized (this) { if (IndexReader.isLocked(optimizedPath)) { return; } else { Directory dir = FSDirectory.getDirectory(new File(optimizedPath), create); writer = new IndexWriter(dir, analyzer, create); } for (int i = 0; i < childs.length; i++) { boolean lock = true; File child = childs[i]; File[] faux = child.listFiles(); for (int j = 0; j < faux.length; j++) { if (faux[j].getName().equals("is.unlock")) { faux[j].delete(); lock = false; break; } } if (!lock) { Directory[] aux = new Directory[1]; aux[0] = FSDirectory.getDirectory(child, false); IndexSearcher searcher = null; try { searcher = new IndexSearcher(aux[0]); Document doc = searcher.doc(0); if (doc != null) { MailIndexer.createSpell(userPath + SPELL, Field_from, doc); MailIndexer.createSpell(userPath + SPELL, Field_to, doc); MailIndexer.createSpell(userPath + SPELL, Field_subject, doc); MailIndexer.createSpell(userPath + SPELL, Field_body, doc); } } catch (Exception ex) { if ((ex != null) && !(ex instanceof NullPointerException)) { DLog.log(DLog.INFO, this.getClass(), ex); } } finally { if (searcher != null) { try { searcher.close(); } catch (Exception e) { } } } writer.addIndexes(aux); writer.optimize(); for (int j = 0; j < faux.length; j++) { faux[j].delete(); } child.delete(); } } writer.close(); writer = null; } } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } }
From source file:com.ecyrd.jspwiki.search.LuceneSearchProvider.java
License:Apache License
/** * Performs a full Lucene reindex, if necessary. * * @throws IOException If there's a problem during indexing *//*from w w w. j av a 2s. co m*/ protected void doFullLuceneReindex() throws IOException { File dir = new File(m_luceneDirectory); String[] filelist = dir.list(); if (filelist == null) { throw new IOException("Invalid Lucene directory: cannot produce listing: " + dir.getAbsolutePath()); } try { if (filelist.length == 0) { // // No files? Reindex! // Date start = new Date(); IndexWriter writer = null; log.info("Starting Lucene reindexing, this can take a couple minutes..."); // // Do lock recovery, in case JSPWiki was shut down forcibly // Directory luceneDir = FSDirectory.getDirectory(dir, false); if (IndexReader.isLocked(luceneDir)) { log.info("JSPWiki was shut down while Lucene was indexing - unlocking now."); IndexReader.unlock(luceneDir); } try { writer = new IndexWriter(m_luceneDirectory, getLuceneAnalyzer(), true); Collection allPages = m_engine.getPageManager().getAllPages(); for (Iterator iterator = allPages.iterator(); iterator.hasNext();) { WikiPage page = (WikiPage) iterator.next(); try { String text = m_engine.getPageManager().getPageText(page.getName(), WikiProvider.LATEST_VERSION); luceneIndexPage(page, text, writer); } catch (IOException e) { log.warn("Unable to index page " + page.getName() + ", continuing to next ", e); } } Collection allAttachments = m_engine.getAttachmentManager().getAllAttachments(); for (Iterator iterator = allAttachments.iterator(); iterator.hasNext();) { Attachment att = (Attachment) iterator.next(); try { String text = getAttachmentContent(att.getName(), WikiProvider.LATEST_VERSION); luceneIndexPage(att, text, writer); } catch (IOException e) { log.warn("Unable to index attachment " + att.getName() + ", continuing to next", e); } } writer.optimize(); } finally { try { if (writer != null) writer.close(); } catch (IOException e) { } } Date end = new Date(); log.info("Full Lucene index finished in " + (end.getTime() - start.getTime()) + " milliseconds."); } else { log.info("Files found in Lucene directory, not reindexing."); } } catch (NoClassDefFoundError e) { log.info("Lucene libraries do not exist - not using Lucene."); } catch (IOException e) { log.error("Problem while creating Lucene index - not using Lucene.", e); } catch (ProviderException e) { log.error("Problem reading pages while creating Lucene index (JSPWiki won't start.)", e); throw new IllegalArgumentException("unable to create Lucene index"); } catch (ClassNotFoundException e) { log.error("Illegal Analyzer specified:", e); } catch (Exception e) { log.error("Unable to start lucene", e); } }
From source file:com.ecyrd.jspwiki.search.LuceneSearchProvider.java
License:Apache License
/** * Updates the lucene index for a single page. * * @param page The WikiPage to check/*w w w. j a v a 2 s .c om*/ * @param text The page text to index. */ protected synchronized void updateLuceneIndex(WikiPage page, String text) { IndexWriter writer = null; log.debug("Updating Lucene index for page '" + page.getName() + "'..."); try { pageRemoved(page); // Now add back the new version. writer = new IndexWriter(m_luceneDirectory, getLuceneAnalyzer(), false); luceneIndexPage(page, text, writer); m_updateCount++; if (m_updateCount >= LUCENE_OPTIMIZE_COUNT) { writer.optimize(); m_updateCount = 0; } } catch (IOException e) { log.error("Unable to update page '" + page.getName() + "' from Lucene index", e); } catch (Exception e) { log.error("Unexpected Lucene exception - please check configuration!", e); } finally { try { if (writer != null) writer.close(); } catch (IOException e) { } } log.debug("Done updating Lucene index for page '" + page.getName() + "'."); }
From source file:com.eden.lucene.IndexFiles.java
License:Apache License
/** Index all text files under a directory. */ public static void main(String[] args) { String usage = "java org.apache.lucene.demo.IndexFiles" + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n" + "This indexes the documents in DOCS_PATH, creating a Lucene index" + "in INDEX_PATH that can be searched with SearchFiles"; String basePath = "D:/test/lucene"; String indexPath = basePath + "/index"; String docsPath = basePath + "/file"; boolean create = true; /*for(int i=0;i<args.length;i++) { if ("-index".equals(args[i])) {/* w w w . j a v a 2 s. c o m*/ indexPath = args[i+1]; i++; } else if ("-docs".equals(args[i])) { docsPath = args[i+1]; i++; } else if ("-update".equals(args[i])) { create = false; } }*/ if (docsPath == null) { System.err.println("Usage: " + usage); System.exit(1); } final File docDir = new File(docsPath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, please check the path"); System.exit(1); } Date start = new Date(); try { System.out.println("Indexing to directory '" + indexPath + "'..."); Directory dir = FSDirectory.open(new File(indexPath)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, analyzer); if (create) { // Create a new index in the directory, removing any // previously indexed documents: iwc.setOpenMode(OpenMode.CREATE); } else { // Add new documents to an existing index: iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); } // Optional: for better indexing performance, if you // are indexing many documents, increase the RAM // buffer. But if you do this, increase the max heap // size to the JVM (eg add -Xmxm or -Xmx1g): // // iwc.setRAMBufferSizeMB(.0); IndexWriter writer = new IndexWriter(dir, iwc); indexDocs(writer, docDir); // NOTE: if you want to maximize search performance, // you can optionally call forceMerge here. This can be // a terribly costly operation, so generally it's only // worth it when your index is relatively static (ie // you're done adding documents to it): // // writer.forceMerge(1); writer.close(); Date end = new Date(); System.out.println(end.getTime() - start.getTime() + " total milliseconds"); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } }