List of usage examples for org.apache.lucene.index IndexWriter commit
@Override public final long commit() throws IOException
Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.
From source file:org.geotoolkit.lucene.index.AbstractIndexer.java
License:Open Source License
/** * This method remove index of lucene a document identified by identifier. * * @param identifier//w w w . j a va2 s . co m */ public void removeDocument(final String identifier) { try { final Directory dir = LuceneUtils.getAppropriateDirectory(getFileDirectory()); final Term t = new Term("id", identifier); final TermQuery query = new TermQuery(t); LOGGER.log(logLevel, "Term query:{0}", query); // look for DOC ID for R-Tree removal final NamedEnvelope env = new NamedEnvelope(getTreeCrs(), identifier); final TreeElementMapper<NamedEnvelope> mapper = rTree.getTreeElementMapper(); final int treeID = mapper.getTreeIdentifier(env); if (treeID != -1) { final NamedEnvelope realEnv = mapper.getObjectFromTreeIdentifier(treeID); boolean removed = rTree.remove(realEnv); if (!removed) { LOGGER.log(Level.WARNING, "unable to remove envelope for:{0}", identifier); } else { //remove from mapper mapper.setTreeIdentifier(null, treeID); mapper.flush(); rTree.flush(); } } final IndexWriterConfig config = new IndexWriterConfig(analyzer); final IndexWriter writer = new IndexWriter(dir, config); writer.deleteDocuments(query); LOGGER.log(logLevel, "Metadata: {0} removed from the index", identifier); writer.commit(); writer.close(); } catch (CorruptIndexException ex) { LOGGER.log(Level.WARNING, "CorruptIndexException while indexing document: " + ex.getMessage(), ex); } catch (IOException ex) { LOGGER.log(Level.WARNING, "IOException while indexing document: " + ex.getMessage(), ex); } catch (StoreIndexException ex) { LOGGER.log(Level.WARNING, "StoreIndexException while indexing document: " + ex.getMessage(), ex); } }
From source file:org.getopt.luke.Luke.java
License:Apache License
public void commitUserData(Object dialog) { Map<String, String> userData = (Map<String, String>) getProperty(dialog, "userData"); remove(dialog);//from w w w .ja va 2 s . c o m if (!(ir instanceof DirectoryReader)) { errorMsg("Not possible with " + ir.getClass().getSimpleName()); return; } try { IndexWriter iw = createIndexWriter(); //iw.commit(userData); no such method exists anymore in Lucene trunk (5.0) iw.commit(); iw.close(); refreshAfterWrite(); } catch (Exception e) { errorMsg("Error: " + e.toString()); } }
From source file:org.getopt.luke.Luke.java
License:Apache License
/** * Optimize the index.//from w ww . ja v a 2 s . c o m */ public void optimize(final Object dialog) { Thread t = new Thread() { public void run() { IndexWriter iw = null; Object optimizeButton = find(dialog, "optimizeButton"); setBoolean(optimizeButton, "enabled", false); Object closeButton = find(dialog, "closeButton"); setBoolean(closeButton, "enabled", false); Object msg = find(dialog, "msg"); Object stat = find(dialog, "stat"); setString(stat, "text", "Running ..."); PanelPrintWriter ppw = new PanelPrintWriter(Luke.this, msg); boolean useCompound = getBoolean(find(dialog, "optCompound"), "selected"); boolean expunge = getBoolean(find(dialog, "optExpunge"), "selected"); boolean keep = getBoolean(find(dialog, "optKeepAll"), "selected"); boolean useLast = getBoolean(find(dialog, "optLastCommit"), "selected"); Object tiiSpin = find(dialog, "tii"); Object segnumSpin = find(dialog, "segnum"); int tii = Integer.parseInt(getString(tiiSpin, "text")); int segnum = Integer.parseInt(getString(segnumSpin, "text")); try { if (is != null) is = null; if (ir != null) ir.close(); if (ar != null) ar.close(); IndexDeletionPolicy policy; if (keep) { policy = new KeepAllIndexDeletionPolicy(); } else { policy = new KeepLastIndexDeletionPolicy(); } IndexWriterConfig cfg = new IndexWriterConfig(LV, new WhitespaceAnalyzer(LV)); if (!useLast) { IndexCommit ic = ((DirectoryReader) ir).getIndexCommit(); if (ic != null) { cfg.setIndexCommit(ic); } } cfg.setIndexDeletionPolicy(policy); cfg.setTermIndexInterval(tii); cfg.setUseCompoundFile(useCompound); cfg.setInfoStream(ppw); iw = new IndexWriter(dir, cfg); long startSize = Util.calcTotalFileSize(pName, dir); long startTime = System.currentTimeMillis(); if (expunge) { iw.forceMergeDeletes(); } else { if (segnum > 1) { iw.forceMerge(segnum, true); } else { iw.forceMerge(1, true); } } iw.commit(); long endTime = System.currentTimeMillis(); long endSize = Util.calcTotalFileSize(pName, dir); long deltaSize = startSize - endSize; String sign = deltaSize < 0 ? " Increased " : " Reduced "; String sizeMsg = sign + Util.normalizeSize(Math.abs(deltaSize)) + Util.normalizeUnit(Math.abs(deltaSize)); String timeMsg = String.valueOf(endTime - startTime) + " ms"; showStatus(sizeMsg + " in " + timeMsg); iw.close(); setString(stat, "text", "Finished OK."); } catch (Exception e) { e.printStackTrace(ppw); setString(stat, "text", "ERROR - aborted."); errorMsg("ERROR optimizing: " + e.toString()); if (iw != null) try { iw.close(); } catch (Exception e1) { } } finally { setBoolean(closeButton, "enabled", true); } try { actionReopen(); is = new IndexSearcher(ir); // add dialog again add(dialog); } catch (Exception e) { e.printStackTrace(ppw); errorMsg("ERROR reopening after optimize:\n" + e.getMessage()); } } }; t.start(); }
From source file:org.gridkit.coherence.search.lucene.xml.JAXBSchemaTest.java
License:Apache License
public IndexSearcher newSearcher(Object object) { try {// w w w . j a v a 2 s. c o m RAMDirectory dir = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_42, new WhitespaceAnalyzer(Version.LUCENE_42)); IndexWriter iw = new IndexWriter(dir, iwc); JAXBSchemaAdapter adapter = new JAXBSchemaAdapter(); AnalyzedDocument doc = adapter.extract(object); iw.addDocument(doc.getFields().values()); iw.commit(); DirectoryReader reader = DirectoryReader.open(iw, true); return new IndexSearcher(reader); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.hibernate.search.backend.lucene.work.impl.CommitIndexLuceneWork.java
License:LGPL
private CompletableFuture<Long> commitIndex(IndexWriter indexWriter) { try {/*from www . ja va 2 s .com*/ return CompletableFuture.completedFuture(indexWriter.commit()); } catch (IOException e) { throw log.unableToCommitIndex(getEventContext(), e); } }
From source file:org.hibernate.search.backend.lucene.work.impl.OptimizeIndexLuceneWork.java
License:LGPL
private CompletableFuture<Long> commitIndex(IndexWriter indexWriter) { try {/*from w w w. ja va 2 s . c om*/ indexWriter.forceMerge(1); return CompletableFuture.completedFuture(indexWriter.commit()); } catch (IOException e) { throw log.unableToCommitIndex(getEventContext(), e); } }
From source file:org.hibernate.search.test.engine.optimizations.deletebyterm.DeleteByTermTest.java
License:LGPL
@Test public void testRelatedHierarchiesWithRootNonIndexed() throws Exception { // Create two entities whose root entity is common but not indexed // delete by term should be used // create a unrelated Lucene Document with the same id // it should be deleted when the entity sharing the id is deleted FullTextSessionBuilder sessionBuilder = new FullTextSessionBuilder(); sessionBuilder.addAnnotatedClass(ASubOfRoot.class).addAnnotatedClass(BSubOfRoot.class).build(); FullTextSession fts = sessionBuilder.openFullTextSession(); fts.beginTransaction();/*from w w w . ja v a 2 s . c om*/ ASubOfRoot a = new ASubOfRoot(); a.id = "1"; a.name = "Foo"; fts.persist(a); BSubOfRoot b = new BSubOfRoot(); b.id = "2"; b.otherName = "Bar"; fts.persist(b); fts.getTransaction().commit(); fts.clear(); // add a document that matches the entity a identifier to see if it is removed when the entity is removed DirectoryBasedIndexManager indexManager = (DirectoryBasedIndexManager) fts.getSearchFactory() .unwrap(SearchIntegrator.class).getIndexManager("index1"); WorkspaceHolder backendProcessor = (WorkspaceHolder) indexManager.getWorkspaceHolder(); IndexWriter writer = backendProcessor.getIndexResources().getWorkspace().getIndexWriter(); Document document = new Document(); document.add(new StringField("id", "1", org.apache.lucene.document.Field.Store.NO)); document.add(new TextField("name", "Baz", org.apache.lucene.document.Field.Store.NO)); writer.addDocument(document); writer.commit(); fts.getTransaction().begin(); fts.delete(fts.get(ASubOfRoot.class, a.id)); fts.delete(fts.get(BSubOfRoot.class, b.id)); fts.getTransaction().commit(); fts.close(); // Verify that the index is empty IndexReader indexReader = fts.getSearchFactory().getIndexReaderAccessor().open("index1"); try { assertThat(indexReader.numDocs()).isEqualTo(0); } finally { indexReader.close(); } sessionBuilder.close(); }
From source file:org.hibernate.search.test.engine.optimizations.deletebyterm.DeleteByTermTest.java
License:LGPL
@Test public void testUnrelatedHierarchies() throws Exception { // Create two entities whose root entities are unrelated // delete by term should not be used // create a unrelated Lucene Document with the same id // it should not be deleted when the entity sharing the id is deleted FullTextSessionBuilder sessionBuilder = new FullTextSessionBuilder(); sessionBuilder.addAnnotatedClass(ASubOfRoot.class).addAnnotatedClass(Unrelated.class).build(); FullTextSession fts = sessionBuilder.openFullTextSession(); fts.beginTransaction();// ww w . ja v a 2 s . co m ASubOfRoot a = new ASubOfRoot(); a.id = "1"; a.name = "Foo"; fts.persist(a); Unrelated b = new Unrelated(); b.id = "2"; b.name = "Bar"; fts.persist(b); fts.getTransaction().commit(); fts.clear(); // add a document that matches the entity a identifier to see if it is removed when the entity is removed DirectoryBasedIndexManager indexManager = (DirectoryBasedIndexManager) fts.getSearchFactory() .unwrap(SearchIntegrator.class).getIndexManager("index1"); WorkspaceHolder backendProcessor = (WorkspaceHolder) indexManager.getWorkspaceHolder(); IndexWriter writer = backendProcessor.getIndexResources().getWorkspace().getIndexWriter(); Document document = new Document(); document.add(new StringField("id", "1", org.apache.lucene.document.Field.Store.NO)); document.add(new TextField("name", "Baz", org.apache.lucene.document.Field.Store.NO)); writer.addDocument(document); writer.commit(); fts.getTransaction().begin(); fts.delete(fts.get(ASubOfRoot.class, a.id)); fts.delete(fts.get(Unrelated.class, b.id)); fts.getTransaction().commit(); fts.close(); // Verify that the index is empty IndexReader indexReader = fts.getSearchFactory().getIndexReaderAccessor().open("index1"); try { assertThat(indexReader.numDocs()).isEqualTo(1); } finally { indexReader.close(); } sessionBuilder.close(); }
From source file:org.hibernate.search.test.performance.reader.ReaderPerformance.java
License:Open Source License
private void buildBigIndex() throws InterruptedException, CorruptIndexException, LockObtainFailedException, IOException { System.out.println("Going to create fake index..."); FSDirectory directory = FSDirectory.open(new File(getBaseIndexDir(), Detective.class.getCanonicalName())); IndexWriter.MaxFieldLength fieldLength = new IndexWriter.MaxFieldLength( IndexWriter.DEFAULT_MAX_FIELD_LENGTH); IndexWriter iw = new IndexWriter(directory, new SimpleAnalyzer(), true, fieldLength); IndexFillRunnable filler = new IndexFillRunnable(iw); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(WORKER_THREADS); for (int batch = 0; batch <= 5000000; batch++) { executor.execute(filler);/* ww w. jav a 2s . com*/ } executor.shutdown(); executor.awaitTermination(600, TimeUnit.SECONDS); iw.commit(); iw.optimize(); iw.close(); System.out.println("Index created."); }
From source file:org.hibernate.search.test.query.engine.FieldNameCollectorTest.java
License:LGPL
private void indexTestDocuments(Directory directory) throws IOException { IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); Document document = new Document(); document.add(new StringField("stringField", "test", Field.Store.NO)); document.add(new IntField("intField", 0, Field.Store.NO)); indexWriter.addDocument(document);/*from w w w. j a va 2 s. c om*/ indexWriter.commit(); indexWriter.close(); }