List of usage examples for org.apache.lucene.index IndexWriter close
@Override public void close() throws IOException
From source file:collene.TestLuceneAssumptions.java
License:Apache License
@Test public void listAfterEachStep() throws Exception { File fdir = TestUtil.getRandomTempDir(); pleaseDelete.add(fdir);/* www .jav a2s .c om*/ Directory dir = FSDirectory.open(fdir); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); //System.out.println("Before creating writer"); dump(fdir, dir); IndexWriter writer = new IndexWriter(dir, config); //System.out.println("After creating writer"); dump(fdir, dir); List<Document> docs = new ArrayList<Document>(); for (int i = 0; i < 50000; i++) { Document doc = new Document(); for (int f = 0; f < 5; f++) { doc.add(new Field("field_" + f, TestUtil.randomString(128), TextField.TYPE_STORED)); } docs.add(doc); } writer.addDocuments(docs, analyzer); docs.clear(); //System.out.println("After doc add 0"); dump(fdir, dir); for (int i = 0; i < 50000; i++) { Document doc = new Document(); for (int f = 0; f < 5; f++) { doc.add(new Field("field_" + f, TestUtil.randomString(128), TextField.TYPE_STORED)); } docs.add(doc); } writer.addDocuments(docs, analyzer); docs.clear(); //System.out.println("After doc add 1"); dump(fdir, dir); writer.commit(); //System.out.println("After commit"); dump(fdir, dir); writer.forceMerge(1, true); //System.out.println("Right after merge"); dump(fdir, dir); try { Thread.currentThread().sleep(5000); } catch (Exception ex) { } //System.out.println("After sleeping after merge"); dump(fdir, dir); writer.close(); //System.out.println("After writer close"); dump(fdir, dir); dir.close(); //System.out.println("After dir close"); dump(fdir, dir); }
From source file:com.adanac.module.blog.search.LuceneHelper.java
License:Apache License
private static void generateIndex(String path, String id, String title, String content, List<Map<String, String>> dataList) { try {/* ww w .j a va2 s .co m*/ Directory dir = FSDirectory.open(Paths.get(INDEX_PATH + path)); Analyzer analyzer = new SmartChineseAnalyzer(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); indexWriterConfig.setOpenMode(OpenMode.CREATE); IndexWriter writer = new IndexWriter(dir, indexWriterConfig); for (Map<String, String> data : dataList) { Document document = new Document(); Field idField = new IntField("id", Integer.valueOf(data.get(id)), Field.Store.YES); Field indexedContentField = new TextField("indexedContent", data.get(title) + SEPARATOR + data.get(content), Field.Store.YES); document.add(idField); document.add(indexedContentField); writer.addDocument(document); if (logger.isInfoEnabled()) { logger.info("add index for : [" + data.get(title) + "]"); } } writer.close(); } catch (Exception e) { logger.error("add index failed ...", e); } }
From source file:com.admarketplace.isg.lucene.demo.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 ww w . ja v a 2 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; } } 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 { 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:com.agiletec.plugins.jacms.aps.system.services.searchengine.IndexerDAO.java
License:Open Source License
/** * Inizializzazione dell'indicizzatore./*from ww w . j av a2 s. com*/ * @param dir La cartella locale contenitore dei dati persistenti. * @param newIndex true se una nuova indicizzazione (ed in tal caso * cancella tutte le precedenti indicizzazioni), false in caso contrario. * @throws ApsSystemException */ @Override public void init(File dir, boolean newIndex) throws ApsSystemException { try { this._dir = FSDirectory.open(dir); boolean indexExists = IndexReader.indexExists(this._dir); IndexWriter writer = new IndexWriter(_dir, getAnalyzer(), !indexExists, new MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH)); writer.close(); } catch (Throwable t) { throw new ApsSystemException("Errore in creazione directory", t); } ApsSystemUtils.getLogger().config("Indexer: search engine index ok."); }
From source file:com.agiletec.plugins.jacms.aps.system.services.searchengine.IndexerDAO.java
License:Open Source License
/** * Aggiunge un documento nel db del motore di ricerca. * @param document Il documento da aggiungere. * @throws ApsSystemException In caso di errori in accesso al db. *//*from www . j a v a 2 s . co m*/ private synchronized void add(Document document) throws ApsSystemException { try { IndexWriter writer = new IndexWriter(_dir, this.getAnalyzer(), false, new MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH)); writer.addDocument(document); writer.optimize(); writer.close(); } catch (IOException e) { throw new ApsSystemException("Errore nell'aggiunta di un documento", e); } }
From source file:com.agiletec.plugins.jacms.aps.system.services.searchengine.IndexerDAO.java
License:Open Source License
/** * Cancella un documento in base alla chiave (di nome "id") * mediante il quale stato indicizzato. * Nel caso della cancellazione di un contenuto il nome del campo * da utilizzare sar "id" mentre il valore sar l'identificativo del contenuto. * @param name Il nome del campo Field da utilizzare per recupero del documento. * @param value La chiave mediante il quale * stato indicizzato il documento.// ww w. j ava 2 s. com * @throws ApsSystemException */ @Override public synchronized void delete(String name, String value) throws ApsSystemException { IndexReader reader = null; try { reader = IndexReader.open(this._dir, false); reader.deleteDocuments(new Term(name, value)); reader.close(); IndexWriter writer = new IndexWriter(this._dir, this.getAnalyzer(), false, new MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH)); writer.optimize(); writer.close(); } catch (IOException e) { throw new ApsSystemException("Errore nella cancellazione di un indice", e); } }
From source file:com.aliasi.lingmed.medline.IndexMedline.java
License:Lingpipe license
/** * Run the command. See class documentation above for details on * arguments and behavior./*from www . j ava 2 s . co m*/ */ public void run() { System.out.println("start run"); try { File[] files = getLaterFiles(mDistDir); System.out.println("Total files to process: " + files.length); System.out.println("File names: " + java.util.Arrays.asList(files)); // if (mLogger.isDebugEnabled()) // mLogger.debug("File names: " + java.util.Arrays.asList(files)); if (files.length > 0) { MedlineParser parser = new MedlineParser(true); // true = save raw XML Directory fsDir = FSDirectory.open(mIndex); IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_36, mCodec.getAnalyzer()); iwConf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); iwConf.setRAMBufferSizeMB(RAM_BUF_SIZE); if (sIsBaseline) { LogDocMergePolicy ldmp = new LogDocMergePolicy(); ldmp.setMergeFactor(MERGE_FACTOR_HI); iwConf.setMergePolicy(ldmp); } IndexWriter indexWriter = new IndexWriter(fsDir, iwConf); for (File file : files) { System.out.println("processing file: " + file); MedlineIndexer indexer = new MedlineIndexer(indexWriter, mCodec); parser.setHandler(indexer); parseFile(parser, file); indexer.close(); recordFile(indexWriter, file.getName()); System.out.println("completed processing file: " + file); } System.out.println("All files parsed, now optimize index"); indexWriter.forceMerge(1); indexWriter.commit(); indexWriter.close(); } System.out.println("Processing complete."); } catch (Exception e) { // mLogger.warn("Unexpected Exception: "+e.getMessage()); // mLogger.warn("stack trace: "+Logging.logStackTrace(e)); // mLogger.warn("Aborting this run"); IllegalStateException e2 = new IllegalStateException(e.getMessage()); e2.setStackTrace(e.getStackTrace()); throw e2; } }
From source file:com.aliasi.lingmed.medline.SearchableMedlineCodec.java
License:Lingpipe license
public static void main(String[] args) throws Exception { org.apache.lucene.store.RAMDirectory directory = new org.apache.lucene.store.RAMDirectory(); // org.apache.lucene.analysis.SimpleAnalyzer analyzer // = new org.apache.lucene.analysis.SimpleAnalyzer(); // org.apache.lucene.analysis.KeywordAnalyzer analyzer // = new org.apache.lucene.analysis.KeywordAnalyzer(); MedlineCodec codec = new MedlineCodec(); Analyzer analyzer = codec.getAnalyzer(); org.apache.lucene.index.IndexWriterConfig iwConf = new org.apache.lucene.index.IndexWriterConfig( org.apache.lucene.util.Version.LUCENE_36, analyzer); iwConf.setOpenMode(org.apache.lucene.index.IndexWriterConfig.OpenMode.CREATE_OR_APPEND); org.apache.lucene.index.IndexWriter indexWriter = new org.apache.lucene.index.IndexWriter(directory, iwConf);//from w w w . j a va 2s.c om Document doc = new Document(); doc.add(new Field(Fields.MESH_MINOR_FIELD, "abc", Field.Store.NO, Field.Index.ANALYZED)); doc.add(new Field(Fields.MESH_MINOR_FIELD, " xyz efg", Field.Store.NO, Field.Index.ANALYZED)); indexWriter.addDocument(doc); indexWriter.close(); org.apache.lucene.index.IndexReader reader = org.apache.lucene.index.IndexReader.open(directory); org.apache.lucene.search.IndexSearcher searcher = new org.apache.lucene.search.IndexSearcher(reader); org.apache.lucene.queryParser.QueryParser qp = new org.apache.lucene.queryParser.QueryParser( org.apache.lucene.util.Version.LUCENE_36, "foo", analyzer); org.apache.lucene.search.Query query = qp.parse(Fields.MESH_MINOR_FIELD + ":efg"); org.apache.lucene.search.TopDocs hits = searcher.search(query, 1000); System.out.println("hits.length()=" + hits.scoreDocs.length); org.apache.lucene.analysis.TokenStream ts = analyzer.tokenStream(Fields.MESH_MINOR_FIELD, new java.io.StringReader("abc xyz efg")); org.apache.lucene.analysis.tokenattributes.CharTermAttribute terms = ts .addAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute.class); org.apache.lucene.analysis.tokenattributes.OffsetAttribute offsets = ts .addAttribute(org.apache.lucene.analysis.tokenattributes.OffsetAttribute.class); org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute positions = ts .addAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute.class); while (ts.incrementToken()) { int increment = positions.getPositionIncrement(); int start = offsets.startOffset(); int end = offsets.endOffset(); String term = terms.toString(); System.out.println("token=|" + term + "|" + " startOffset=" + start + " endOffset=" + end + " positionIncr=" + increment); } }
From source file:com.aperigeek.dropvault.web.service.IndexService.java
License:Open Source License
public void index(String username, String password, String id, Map<String, String> metadata) throws IndexException { try {/*ww w. j a va 2 s. c o m*/ Document document = new Document(); document.add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED)); for (Map.Entry<String, String> e : metadata.entrySet()) { if (e.getValue() != null) { document.add(new Field(e.getKey(), e.getValue(), Field.Store.NO, Field.Index.ANALYZED)); } } IndexWriter index = getIndexWriter(username, password); index.addDocument(document); index.close(); } catch (IOException ex) { throw new IndexException(ex); } }
From source file:com.aperigeek.dropvault.web.service.IndexService.java
License:Open Source License
public void remove(String username, String password, String id) throws IndexException { try {// w w w. j av a2 s . c om IndexWriter writer = getIndexWriter(username, password); writer.deleteDocuments(new Term("id", id)); writer.close(); } catch (IOException ex) { throw new IndexException(ex); } }