List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode
public IndexWriterConfig setOpenMode(OpenMode openMode)
From source file:com.isa.basic.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;// www .j a 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); } // docsPath = Thread.currentThread().getContextClassLoader().getResource(docsPath).; // indexPath = Thread.currentThread().getContextClassLoader().getResource(indexPath).toString(); 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 (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:com.ivannotes.searchbee.demo.TxtSearchBee.java
License:Apache License
@Override protected IndexWriter getIndexWriter() { Directory dir;//w ww .j a v a 2s.c om try { dir = FSDirectory.open(new File(idxPath)); IndexWriterConfig idxCfg = new IndexWriterConfig(matchVersion, analyzer); idxCfg.setOpenMode(OpenMode.CREATE); IndexWriter idxWriter = new IndexWriter(dir, idxCfg); return idxWriter; } catch (IOException e) { throw new RuntimeException("Create index writer error. ", e); } }
From source file:com.jaeksoft.searchlib.index.WriterLocal.java
License:Open Source License
private final IndexWriter open(boolean create) throws CorruptIndexException, LockObtainFailedException, IOException, SearchLibException { IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, null); config.setOpenMode(create ? OpenMode.CREATE_OR_APPEND : OpenMode.APPEND); config.setMergeScheduler(new SerialMergeScheduler()); config.setWriteLockTimeout(indexConfig.getWriteLockTimeout()); Similarity similarity = indexConfig.getNewSimilarityInstance(); if (similarity != null) config.setSimilarity(similarity); Logging.debug("WriteLocal open " + indexDirectory.getDirectory()); return new IndexWriter(indexDirectory.getDirectory(), config); }
From source file:com.jaeksoft.searchlib.index.WriterLucene.java
License:Open Source License
private final IndexWriter open(boolean create) throws CorruptIndexException, LockObtainFailedException, IOException, SearchLibException { IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, null); config.setOpenMode(create ? OpenMode.CREATE_OR_APPEND : OpenMode.APPEND); config.setMergeScheduler(new SerialMergeScheduler()); Similarity similarity = indexConfig.getNewSimilarityInstance(); if (similarity != null) config.setSimilarity(similarity); Logging.debug("WriteLocal open " + indexDirectory.getDirectory()); return new IndexWriter(indexDirectory.getDirectory(), config); }
From source file:com.joliciel.jochre.search.JochreIndexBuilderImpl.java
License:Open Source License
public JochreIndexBuilderImpl(File indexDir) { try {// w w w. ja v a2s. co m this.indexDir = indexDir; Directory directory = FSDirectory.open(this.indexDir); JochreAnalyzer analyzer = new JochreAnalyzer(Version.LUCENE_46); analyzer.setObserver(this); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); this.indexWriter = new IndexWriter(directory, iwc); } catch (IOException ioe) { LogUtils.logError(LOG, ioe); throw new RuntimeException(ioe); } }
From source file:com.liferay.portal.servlet.LuceneServlet.java
License:Open Source License
public void init(ServletConfig sc) throws ServletException { synchronized (LuceneServlet.class) { super.init(sc); ServletContext ctx = getServletContext(); _companyId = ctx.getInitParameter("company_id"); if (GetterUtil.getBoolean(PropsUtil.get(PropsUtil.INDEX_ON_STARTUP))) { _indexer = new LuceneIndexer(_companyId); if (GetterUtil.getBoolean(PropsUtil.get(PropsUtil.INDEX_WITH_THREAD))) { _indexerThread = new Thread(_indexer, THREAD_NAME + "." + _companyId); _indexerThread.setPriority(THREAD_PRIORITY); _indexerThread.start();/*from www .j av a 2 s . com*/ } else { _indexer.reIndex(); } } else { String luceneDir = LuceneUtil.getLuceneDir(_companyId); FileUtil.mkdirs(luceneDir); IndexWriter writer = null; try { Directory directory = FSDirectory.open(new File(luceneDir)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, new WhitespaceAnalyzer(Version.LUCENE_CURRENT)); /* IndexWriterConfig.OpenMode.CREATE_OR_APPEND if used IndexWriter will create a new index if there is not already an index at the provided path and otherwise open the existing index. */ config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(directory, config); } catch (IOException e) { Logger.error(this, e.getMessage(), e); } } } }
From source file:com.lin.studytest.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" // + " [-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 = "D:\\software\\lucene\\testdata\\indexpath"; String docsPath = "D:\\software\\lucene\\testdata\\docpath"; RAMDirectory ramDirectory = new RAMDirectory(); boolean create = false; // 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; // }/*from www. j a v a 2 s . c om*/ // } // 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 SmartChineseAnalyzer(); 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:com.lithium.flow.filer.lucene.LuceneFiler.java
License:Apache License
public LuceneFiler(@Nonnull Filer delegate, @Nonnull Config config) throws IOException { super(delegate); String path = config.getString("index.path"); maxAge = config.getTime("index.maxAge", "-1"); double maxMergeMb = config.getDouble("index.maxMergeMb", 4); double maxCachedMb = config.getDouble("index.maxCacheMb", 64); long targetMaxStale = config.getTime("index.targetMaxStale", "5s"); long targetMinStale = config.getTime("index.targetMinStale", "1s"); Version version = Version.LATEST;//from w ww. jav a 2s .co m Directory dir = FSDirectory.open(new File(path)); NRTCachingDirectory cachingDir = new NRTCachingDirectory(dir, maxMergeMb, maxCachedMb); IndexWriterConfig writerConfig = new IndexWriterConfig(version, null); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new TrackingIndexWriter(new IndexWriter(cachingDir, writerConfig)); manager = new SearcherManager(writer.getIndexWriter(), true, new SearcherFactory()); thread = new ControlledRealTimeReopenThread<>(writer, manager, targetMaxStale, targetMinStale); thread.start(); }
From source file:com.lorelib.analyzer.sample.LuceneIndexAndSearchDemo.java
License:Apache License
/** * //w ww . j ava 2 s .c om * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text = "IK Analyzer???????"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(true); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new StringField("ID", "10000", Field.Store.YES)); doc.add(new TextField(fieldName, text, Field.Store.YES)); iwriter.addDocument(doc); iwriter.close(); //?********************************** //? ireader = DirectoryReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); System.out.println("Query = " + query); //?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(); } } } }
From source file:com.lucene.index.test.IKAnalyzerdemo.java
License:Apache License
/** * /*from w w w .j a va 2s .com*/ * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text1 = "oracle,?"; String text2 = "?"; String text3 = "?"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(); Directory directory1 = null; Directory directory2 = null; IndexWriter iwriter1 = null; IndexWriter iwriter2 = null; IndexReader ireader1 = null; IndexReader ireader2 = null; IndexSearcher isearcher = null; try { // directory1 = new RAMDirectory(); directory2 = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig1 = new IndexWriterConfig(analyzer); iwConfig1.setOpenMode(OpenMode.CREATE); IndexWriterConfig iwConfig2 = new IndexWriterConfig(analyzer); iwConfig2.setOpenMode(OpenMode.CREATE); iwriter1 = new IndexWriter(directory1, iwConfig1); iwriter2 = new IndexWriter(directory2, iwConfig2); // Document doc1 = new Document(); doc1.add(new StringField("ID", "10000", Field.Store.YES)); doc1.add(new TextField("text1", text1, Field.Store.YES)); iwriter1.addDocument(doc1); Document doc2 = new Document(); doc2.add(new StringField("ID", "10001", Field.Store.YES)); doc2.add(new TextField("text2", text2, Field.Store.YES)); iwriter2.addDocument(doc2); iwriter1.close(); iwriter2.close(); //?********************************** //? ireader1 = DirectoryReader.open(directory1); ireader2 = DirectoryReader.open(directory2); IndexReader[] mreader = { ireader1, ireader2 }; MultiReader multiReader = new MultiReader(mreader); isearcher = new IndexSearcher(multiReader); String keyword = "?"; //QueryParser?Query String[] fields = { "text1", "text2" }; Map<String, Float> boosts = new HashMap<String, Float>(); boosts.put("text1", 5.0f); boosts.put("text2", 2.0f); /**MultiFieldQueryParser??? * */ MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer, boosts); Query query = parser.parse(keyword); System.out.println("Query = " + query); //?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 (ireader1 != null) { try { ireader1.close(); ireader2.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory1 != null) { try { directory1.close(); directory2.close(); } catch (IOException e) { e.printStackTrace(); } } } }