List of usage examples for org.apache.lucene.index IndexWriter close
@Override public void close() throws IOException
From source file:com.zimbra.cs.rmgmt.RemoteMailQueue.java
License:Open Source License
void clearIndexInternal() throws IOException { IndexWriter writer = null; try {//from w w w . j a va 2s.co m if (ZimbraLog.rmgmt.isDebugEnabled()) { ZimbraLog.rmgmt.debug("clearing index (" + mIndexPath + ") for " + this); } writer = new IndexWriter(LuceneDirectory.open(mIndexPath), new StandardAnalyzer(LuceneIndex.VERSION), true, IndexWriter.MaxFieldLength.LIMITED); mNumMessages.set(0); } finally { if (writer != null) { writer.close(); } } }
From source file:com.zsq.lucene.chapter1.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 indexPath = "index"; String docsPath = null;/*from w w w.j av a 2 s .co m*/ boolean create = true; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { 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 Path docDir = Paths.get(docsPath); if (!Files.isReadable(docDir)) { System.out.println("Document directory '" + docDir.toAbsolutePath() + "' 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 + "'..."); // Paths.get(indexPath) Directory dir = FSDirectory.open(null); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, 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()); } }
From source file:concurrency.IndexFiles.java
License:Apache License
static void process(String docsPath, Directory indexDir, IndexWriterConfig iwc) { //String indexPath, String docsPath) { boolean create = true; ///*from w w w.j av a 2 s .com*/ final Path docDir = Paths.get(docsPath); // if (!Files.isReadable(docDir)) { // System.out.println("Document directory '" + docDir.toAbsolutePath() // + "' does not exist or is not readable, please check the path"); // System.exit(1); // } IndexWriter writer = null; try { // System.out.println("Indexing to directory '" + indexPath + "'..."); // Directory dir = FSDirectory.open(Paths.get(indexPath)); // Analyzer analyzer = new StandardAnalyzer(); // IndexWriterConfig iwc = new IndexWriterConfig(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 -Xmx512m or -Xmx1g): // // iwc.setRAMBufferSizeMB(256.0); writer = new IndexWriter(indexDir, 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); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From source file:control.Search.java
/** * Index all feeds by 'id', 'title', 'link' and 'description' in a static inverted index * /* w ww . ja va 2 s . com*/ * @param feed Feeds to be indexed */ protected void indexFeed(Feed feed) { try { StandardAnalyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter w = new IndexWriter(index, config); for (int i = 0; i < feed.getItemCount(); i++) { FeedItem item = feed.getItem(i); if (!indexedFeedsIds.contains((String) item.getGUID())) { Document document = new Document(); document.add(new TextField("id", item.getGUID(), Field.Store.YES)); document.add(new TextField("title", item.getTitle(), Field.Store.YES)); document.add(new TextField("link", item.getLink().toString(), Field.Store.YES)); document.add(new TextField("description", item.getDescriptionAsText(), Field.Store.YES)); w.addDocument(document); indexedFeedsIds.add(item.getGUID()); } } w.close(); } catch (IOException ex) { Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:cs412.project.search.IndexFiles.java
License:Apache License
public IndexFiles(String docsPath, String indexPath) { boolean create = true; if (docsPath == null) { System.exit(1);/*from w ww. j av a 2 s. c o m*/ } 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).toPath()); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(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 -Xmx512m or -Xmx1g): // // iwc.setRAMBufferSizeMB(256.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()); } }
From source file:cs412.project.search.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 indexPath = "index"; // String docsPath = "H:\\data set 4"; //CHANGE BELOW TO YOUR PATH String docsPath = "Split Files/"; boolean create = true; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { indexPath = args[i + 1];//from ww w . jav a 2 s. co m 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).toPath()); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(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 -Xmx512m or -Xmx1g): // // iwc.setRAMBufferSizeMB(256.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()); } }
From source file:cs571.proj1.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 indexPath = "index"; String docsPath = null;/*w w w . ja v a2 s . c om*/ boolean create = true; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { indexPath = args[i + 1]; i++; } else if ("-docs".equals(args[i])) { docsPath = args[i + 1]; i++; } else if ("-update".equals(args[i])) { create = false; } else if ("-tfidf".equals(args[i])) { tfidf = true; } else if ("-bm25".equals(args[i])) { bm25 = true; } } if (docsPath == null) { System.err.println("Usage: " + usage); System.exit(1); } final Path docDir = Paths.get(docsPath); if (!Files.isReadable(docDir)) { System.out.println("Document directory '" + docDir.toAbsolutePath() + "' 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(Paths.get(indexPath)); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); if (tfidf) iwc.setSimilarity(new TFIDF()); if (bm25) iwc.setSimilarity(new BM25()); 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 -Xmx512m or -Xmx1g): // // iwc.setRAMBufferSizeMB(256.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"); System.out.println("Total # of Docs Indexed: " + numOfDocuments); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } }
From source file:dacapo.lucene.LuceneIndex.java
License:Apache License
/** * Index all text files under a directory. *///w w w. j ava2 s . c om public void iterate(String size) throws DacapoException, IOException { if (isVerbose()) System.out.println("luindex benchmark starting"); String[] args = config.getArgs(size); final File INDEX_DIR = new File(scratch, "index"); if (INDEX_DIR.exists()) { System.out.println("Cannot save index to '" + INDEX_DIR + "' directory, please delete it first"); throw new DacapoException("Cannot write to index directory"); } IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true); for (int arg = 0; arg < args.length; arg++) { final File docDir = new File(scratch, args[arg]); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, please check the path"); throw new DacapoException("Cannot read from document directory"); } indexDocs(writer, docDir); System.out.println("Optimizing..."); writer.optimize(); } writer.close(); }
From source file:ddf.catalog.pubsub.criteria.contextual.ContextualEvaluator.java
License:Open Source License
/** * Build one Lucene index for the specified XML Document that contains both case-insensitive and * case-sensitive indexed text. Use the provided XPath selectors to extract the indexable text * from the specified XML document.//w ww . j a v a 2 s .c o m * * @param fullDocument * the XML document to be indexed * @param xpathSelectors * the XPath selectors to use to extract the indexable text from the XML document * * @return the Lucene index for the indexed text from the XML document * * @throws IOException */ public static Directory buildIndex(String fullDocument, String[] xpathSelectors) throws IOException { String methodName = "buildIndex"; LOGGER.entry(methodName); // LOGGER.debug( XPathHelper.xmlToString( fullDocument ) ); // 0. Specify the analyzer for tokenizing text. // The same analyzer should be used for indexing and searching ContextualAnalyzer contextualAnalyzer = new ContextualAnalyzer(Version.LUCENE_30); // 1. create the index Directory index = new RAMDirectory(); // Retrieve the text from the document that can be indexed using the specified XPath // selectors String indexableText = getIndexableText(fullDocument, xpathSelectors); // Create an IndexWriter using the case-insensitive StandardAnalyzer // NOTE: the boolean arg in the IndexWriter constructor means to create a new index, // overwriting any existing index IndexWriter indexWriter = new IndexWriter(index, contextualAnalyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); logTokens(indexWriter.getAnalyzer(), FIELD_NAME, fullDocument, "ContextualAnalyzer"); // Add the indexable text to the case-insensitive index writer, assigning it the // "case-insensitive" field name addDoc(indexWriter, FIELD_NAME, indexableText); indexWriter.close(); CaseSensitiveContextualAnalyzer caseSensitiveStandardAnalyzer = new CaseSensitiveContextualAnalyzer( Version.LUCENE_30); // Create a second IndexWriter using the custom case-sensitive StandardAnalyzer // NOTE: set boolean to false to append the case-sensitive indexed text to the existing // index (populated by first IndexWriter) IndexWriter csIndexWriter = new IndexWriter(index, caseSensitiveStandardAnalyzer, false, IndexWriter.MaxFieldLength.UNLIMITED); // Add the indexable text to the case-sensitive index writer, assigning it the // "case-sensitive" field name addDoc(csIndexWriter, CASE_SENSITIVE_FIELD_NAME, indexableText); csIndexWriter.close(); LOGGER.exit(methodName); return index; }
From source file:de.berlios.jhelpdesk.utils.LuceneIndexer.java
License:Open Source License
@PostConstruct protected final void initializeIndex() { try {/* w w w .ja va 2 s . com*/ IndexWriter w = getIndexWriter(); w.commit(); w.close(); } catch (Exception ex) { log.error(ex.getMessage(), ex); throw new RuntimeException(ex); } }