List of usage examples for org.apache.lucene.index IndexWriter close
@Override public void close() throws IOException
From source file:com.test.LuceneDemo.java
License:Apache License
@Test public void test() throws IOException, org.apache.lucene.queryparser.classic.ParseException { Analyzer analyzer = new StandardAnalyzer(); // Store the index in memory: Directory directory = new RAMDirectory(); // To store an index on disk, use this instead: //Directory directory = FSDirectory.open("/tmp/testindex"); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); String text = "This is the text to be indexed."; doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); iwriter.addDocument(doc);/*from w ww .j a va 2 s . c o m*/ iwriter.close(); // Now search the index: DirectoryReader ireader = DirectoryReader.open(directory); IndexSearcher isearcher = new IndexSearcher(ireader); // Parse a simple query that searches for "text": QueryParser parser = new QueryParser("fieldname", analyzer); Query query = parser.parse("indexed"); ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; assertEquals(1, hits.length); // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); assertEquals("This is the text to be indexed.", hitDoc.get("fieldname")); } ireader.close(); directory.close(); }
From source file:com.tistory.devyongsik.demo.IndexFiles.java
License:Apache License
/** Index all text files under a directory. */ public static void main(String[] args) { String docsPath = "/Users/need4spd/Java/"; //1. String indexPath = "/Users/need4spd/Java/lucene_index/"; //2. 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);/* ww w. j a v a 2s . c om*/ } Date start = new Date(); try { System.out.println("Indexing to directory '" + indexPath + "'..."); //3. IndexWriter . Directory dir = FSDirectory.open(new File(indexPath)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31); // Analyzer IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); boolean create = true; //4. if (create) { // Create a new index in the directory, removing any // previously indexed documents: iwc.setOpenMode(OpenMode.CREATE); //5. . . } else { // Add new documents to an existing index: iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); //6. . } // 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); //7. IndexWriterConfig . // . IndexWriter writer = new IndexWriter(dir, iwc); //8. IndexWriter . indexDocs(writer, docDir); //9. . // NOTE: if you want to maximize search performance, // you can optionally call optimize here. This can be // a costly operation, so generally it's only worth // it when your index is relatively static (ie you're // done adding documents to it): // // writer.optimize(); 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:com.ua.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 <root_directory>"; if (args.length == 0) { System.err.println("Usage: " + usage); System.exit(1);/*from ww w . ja va 2 s. com*/ } if (INDEX_DIR.exists()) { System.out.println("Cannot save index to '" + INDEX_DIR + "' directory, please delete it first"); System.exit(1); } final File docDir = new File(args[0]); 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 { IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); System.out.println("Indexing to directory '" + INDEX_DIR + "'..."); indexDocs(writer, docDir); System.out.println("Optimizing..."); //writer.optimize(); 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:com.vmware.dcp.services.common.LuceneBlobIndexService.java
License:Open Source License
private void close(IndexWriter wr) { try {//from w w w.j ava 2 s .co m if (wr == null) { return; } wr.commit(); wr.close(); } catch (Throwable e) { } }
From source file:com.vmware.dcp.services.common.LuceneBlobIndexService.java
License:Open Source License
private void consolidateIndexFiles() throws IOException { IndexWriter w = this.writer; if (w == null) { return;//www.ja va 2s. co m } File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory); String[] list = directory.list(); int count = list == null ? 0 : list.length; try { logInfo("Before: File count: %d, document count: %d", count, w.maxDoc()); w.close(); } catch (Throwable e) { } this.writer = createWriter(directory); list = directory.list(); count = list == null ? 0 : list.length; logInfo("After: File count: %d, document count: %d", count, w.maxDoc()); }
From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java
License:Open Source License
private void close(IndexWriter wr) { try {//from w ww . ja v a 2 s . c o m if (wr == null) { return; } logInfo("Document count: %d ", wr.maxDoc()); wr.commit(); wr.close(); } catch (Throwable e) { } }
From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java
License:Open Source License
private void reOpenWriterSynchronously() { final int acquireReleaseCount = QUERY_THREAD_COUNT + UPDATE_THREAD_COUNT; try {// www.ja v a 2 s. c om if (getHost().isStopping()) { return; } // Do not proceed unless we have blocked all reader+writer threads. We assume // the semaphore is already acquired by the current thread this.writerAvailable.release(); this.writerAvailable.acquire(acquireReleaseCount); IndexWriter w = this.writer; if (w == null) { return; } long now = Utils.getNowMicrosUtc(); if (now - this.indexWriterCreationTimeMicros < getHost().getMaintenanceIntervalMicros()) { logInfo("Skipping writer re-open, it was created recently"); return; } File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory); String[] list = directory.list(); int count = list == null ? 0 : list.length; try { logInfo("Before: File count: %d, document count: %d", count, w.maxDoc()); w.close(); } catch (Throwable e) { } w = createWriter(directory, false); list = directory.list(); count = list == null ? 0 : list.length; logInfo("After: File count: %d, document count: %d", count, w.maxDoc()); } catch (Throwable e) { // If we fail to re-open we should stop the host, since we can not recover. logSevere(e); logWarning("Stopping local host since index is not accessible"); close(this.writer); this.writer = null; sendRequest(Operation.createDelete(this, ServiceUriPaths.CORE_MANAGEMENT)); } finally { // release all but one, so we stay owning one reference to the semaphore this.writerAvailable.release(acquireReleaseCount - 1); } }
From source file:com.vmware.xenon.services.common.LuceneBlobIndexService.java
License:Open Source License
private void close(IndexWriter wr) { try {// w ww . ja va 2s . co m if (wr == null) { return; } wr.commit(); wr.close(); this.buffer = null; } catch (Throwable e) { } }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java
License:Open Source License
private void reOpenWriterSynchronously() { final int acquireReleaseCount = QUERY_THREAD_COUNT + UPDATE_THREAD_COUNT; try {//from www . j a va 2s . co m if (getHost().isStopping()) { return; } // Do not proceed unless we have blocked all reader+writer threads. We assume // the semaphore is already acquired by the current thread this.writerAvailable.release(); this.writerAvailable.acquire(acquireReleaseCount); IndexWriter w = this.writer; long now = Utils.getNowMicrosUtc(); if (now - this.indexWriterCreationTimeMicros < getHost().getMaintenanceIntervalMicros()) { logInfo("Skipping writer re-open, it was created recently"); return; } File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory); try { if (w != null) { w.close(); } } catch (Throwable e) { } w = createWriter(directory, false); logInfo("Reopened writer, document count: %d", w.maxDoc()); } catch (Throwable e) { // If we fail to re-open we should stop the host, since we can not recover. logSevere(e); logWarning("Stopping local host since index is not accessible"); close(this.writer); this.writer = null; sendRequest(Operation.createDelete(this, ServiceUriPaths.CORE_MANAGEMENT)); } finally { // release all but one, so we stay owning one reference to the semaphore this.writerAvailable.release(acquireReleaseCount - 1); } }
From source file:com.weasel.lucene.ik.sample.IKAnalyzerDemo.java
License:Apache License
public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; ////from w ww .jav a 2s.co m String text = "IK Analyzer???????"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED)); iwriter.addDocument(doc); iwriter.close(); //?********************************** //? ireader = IndexReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); //?5? TopDocs topDocs = isearcher.search(query, 5); System.out.println("" + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println("" + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }