List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode
public IndexWriterConfig setOpenMode(OpenMode openMode)
From source file:com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager.java
License:Apache License
@Override public IndexWriter openIndexWriter(Directory directory, ODocument metadata) throws IOException { Analyzer analyzer = getAnalyzer(metadata); Version version = getLuceneVersion(metadata); IndexWriterConfig iwc = new IndexWriterConfig(version, analyzer); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); return new IndexWriter(directory, iwc); }
From source file:com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager.java
License:Apache License
@Override public IndexWriter createIndexWriter(Directory directory, ODocument metadata) throws IOException { Analyzer analyzer = getAnalyzer(metadata); Version version = getLuceneVersion(metadata); IndexWriterConfig iwc = new IndexWriterConfig(version, analyzer); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); return new IndexWriter(directory, iwc); }
From source file:com.orientechnologies.lucene.test.LuceneVsLuceneTest.java
License:Apache License
@BeforeClass public void init() { initDB();//from www . j a va2 s . c o m OSchema schema = databaseDocumentTx.getMetadata().getSchema(); OClass v = schema.getClass("V"); OClass song = schema.createClass("Song"); song.setSuperClass(v); song.createProperty("title", OType.STRING); song.createProperty("author", OType.STRING); try { Directory dir = getDirectory(); Analyzer analyzer = new StandardAnalyzer(OLuceneIndexManagerAbstract.LUCENE_VERSION); IndexWriterConfig iwc = new IndexWriterConfig(OLuceneIndexManagerAbstract.LUCENE_VERSION, analyzer); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); indexWriter = new IndexWriter(dir, iwc); } catch (IOException e) { e.printStackTrace(); } databaseDocumentTx .command(new OCommandSQL("create index Song.title on Song (title) FULLTEXT ENGINE LUCENE")) .execute(); }
From source file:com.orientechnologies.spatial.engine.OLuceneSpatialIndexEngineAbstract.java
License:Apache License
@Override public IndexWriter openIndexWriter(Directory directory) throws IOException { IndexWriterConfig iwc = new IndexWriterConfig(indexAnalyzer()); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); return new IndexWriter(directory, iwc); }
From source file:com.orientechnologies.spatial.engine.OLuceneSpatialIndexEngineAbstract.java
License:Apache License
@Override public IndexWriter createIndexWriter(Directory directory) throws IOException { IndexWriterConfig iwc = new IndexWriterConfig(indexAnalyzer()); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); return new IndexWriter(directory, iwc); }
From source file:com.orientechnologies.test.CreateCityDbIndex.java
License:Apache License
@Override @Test(enabled = false)/*from w ww .j a v a 2s .c o m*/ public void init() throws Exception { String buildDirectory = System.getProperty("buildDirectory", "."); if (buildDirectory == null) buildDirectory = "."; ZipFile zipFile = new ZipFile("files/allCountries.zip"); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().equals("allCountries.txt")) { InputStream stream = zipFile.getInputStream(entry); lineReader = new LineNumberReader(new InputStreamReader(stream)); } } Directory dir = NIOFSDirectory.open(new File("Spatial")); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, new StandardAnalyzer(Version.LUCENE_47)); iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(dir, iwc); this.ctx = SpatialContext.GEO; SpatialPrefixTree grid = new GeohashPrefixTree(ctx, 11); this.strategy = new RecursivePrefixTreeStrategy(grid, "location"); }
From source file:com.paladin.common.LuceneHelper.java
License:Apache License
/** * //from ww w . ja v a 2 s . c o m * * @param tables * @throws IOException */ public static void index(final String[] tables, boolean _create) { // true ?? false ? Analyzer analyzer = new IKAnalyzer(false); for (String table : tables) { long begin = System.currentTimeMillis(); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_33, analyzer); if (_create) iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE); else iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); final String index_dir = Constants.LUCENE_INDEX_ROOT + table; File dir = new File(index_dir); if (!dir.exists()) dir.mkdirs(); try { Directory directory = FSDirectory.open(dir); IndexWriter writer = new IndexWriter(directory, iwc); indexTable(writer, table); writer.close(); } catch (Exception e) { e.printStackTrace(); } log.info(" " + table + " " + (System.currentTimeMillis() - begin) + " milliseconds"); } }
From source file:com.paladin.sys.lucene.IndexFiles.java
License:Apache License
/** * Index all text files under a directory. */// w w w .j a v a 2 s .c o m public static void main(String[] args) { args = new String[] { "-docs", "D:\\BJ\\ETLWorkspace\\ETL\\src", "-index", "D:\\myData\\luceneIdx" }; final 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; 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()) { 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 { out.println("Indexing to directory '" + indexPath + "'..."); Directory dir = FSDirectory.open(new File(indexPath)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_33, 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 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(); out.println(end.getTime() - start.getTime() + " total milliseconds"); } catch (IOException e) { out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } }
From source file:com.plug.Plug_8_5_2.java
License:Apache License
private void reindexTermbase(DbServer dbServer, HashMap<String, String> companys) throws Exception { log.info("Start upgrading Lucene index for termbase"); TermbaseHandler h = new TermbaseHandler(); List<Termbase> tbs = dbServer.getDbUtil().query(TermbaseHandler.SQL, h); m_analyzer = new NgramAnalyzer(3); for (Termbase tb : tbs) { if (tb.getCOMPANYID().equals(LuceneConstants.SUPER_COMPANY_ID)) { continue; }/*from www .j a va2 s . com*/ String cname = companys.get(tb.getCOMPANYID()); File termDir = new File(fileStorageDir, cname + "/TB-" + tb.getTB_NAME()); // check re-indexed if (isIndexedBefore(termDir, tb.getTB_NAME())) { logAlreadyIndex(tb.getTB_NAME()); continue; } showMsg(cname, tb.getTB_NAME(), false); // 1 delete old term base indexes logDeleteFile(termDir.getAbsolutePath()); deleteFile(termDir.getAbsolutePath()); // 2 create new empty dir termDir.mkdirs(); Definition dif = new Definition(tb.getTB_DEFINITION()); List<Index> indexs = dif.getIndexes(); for (Index index : indexs) { // 3 write index into ram RAMDirectory ramdir = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, m_analyzer); config.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter ramIndexWriter = new IndexWriter(ramdir, config); if (index != null && "fuzzy".equalsIgnoreCase(index.getType())) { String folder = index.getLanguageName() + "-" + index.getLocale() + "-TERM"; File indexFolder = new File(termDir, folder); m_directory = indexFolder.getAbsolutePath(); m_fsDir = new SimpleFSDirectory(indexFolder); String sql = TermHandler.generateSQL(tb.getTBID(), index.getLanguageName()); TermHandler termH = new TermHandler(); List<Document> docs = dbServer.getDbUtil().query(sql, termH); for (Document doc : docs) { ramIndexWriter.addDocument(doc); ramIndexWriter.commit(); } // 4 write index from ram into disk IndexWriter diskwriter = getIndexWriter(true); diskwriter.commit(); if (docs != null && docs.size() > 0) { Directory[] ds = new Directory[] { ramdir }; diskwriter.addIndexes(ds); diskwriter.commit(); } // 5 close index writer IOUtils.closeWhileHandlingException(ramIndexWriter); IOUtils.closeWhileHandlingException(diskwriter); ramIndexWriter = null; ramdir = null; } } writeTagFile(termDir, tb.getTB_NAME()); } log.info("End upgrading Lucene index for termbase"); }
From source file:com.plug.Plug_8_5_2.java
License:Apache License
private boolean isIndexedBefore(File parent, String name) { if (isEmptyIndex(parent)) { return false; }//from w w w . j a v a2s.com File tf = new File(parent, tagFileName); if (tf.exists()) { return true; } File[] fs = parent.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); GlobalSightLocale locale = new GlobalSightLocale("zh", "CN", false); if (fs != null && fs.length > 0) { for (File f : fs) { IndexWriter indexWriter = null; try { FSDirectory directory = FSDirectory.open(f); IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, new GsAnalyzer(locale)); conf.setOpenMode(OpenMode.CREATE); indexWriter = new IndexWriter(directory, conf); } catch (IndexFormatTooOldException ie) { // need to re-index return false; } catch (Exception e) { // ignore } finally { if (indexWriter != null) { IOUtils.closeWhileHandlingException(indexWriter); } } } writeTagFile(parent, name); return true; } else { return false; } }