List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode
public IndexWriterConfig setOpenMode(OpenMode openMode)
From source file:com.lucene.index.test.IKAnalyzerdemoMutilField.java
License:Apache License
/** * /*from w ww. j a v a 2 s .c o m*/ * ??? * @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 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 doc1 = new Document(); doc1.add(new StringField("ID", "10000", Field.Store.YES)); doc1.add(new TextField(fieldName, text1, Field.Store.YES)); iwriter.addDocument(doc1); Document doc2 = new Document(); doc2.add(new StringField("ID", "10000", Field.Store.YES)); doc2.add(new TextField(fieldName, text2, Field.Store.YES)); iwriter.addDocument(doc2); Document doc3 = new Document(); doc3.add(new StringField("ID", "10000", Field.Store.YES)); doc3.add(new TextField(fieldName, text3, Field.Store.YES)); iwriter.addDocument(doc3); 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.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 w w w. j a v a 2 s . c o m 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 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(); long Cbegintime = System.nanoTime();// 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(); long Cendtime = System.nanoTime();// Date end = new Date(); System.out.println(""); // ? BigDecimal diff = BigDecimal.valueOf(Cendtime - Cbegintime, 10);// double time = diff.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(end.getTime() - start.getTime() + "millsecond"); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } }
From source file:com.m3958.apps.pcms.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 = "c:/lucene/index"; String docsPath = "c:/lucene/docs"; 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; // }//from www. ja v a 2s . c om // } // 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); Analyzer analyzer = new ComplexAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45, analyzer); 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); indexCusValues(writer); // 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.main.Indexer.java
public void indexing() throws TikaException, SAXException { //Input folder String docsPath = "C:\\Users\\piyush\\Documents\\NetBeansProjects\\luceneFinal\\indexing\\doc"; //Output folder String indexPath = "C:\\Users\\piyush\\Documents\\NetBeansProjects\\luceneFinal\\indexing\\index"; //Input Path Variable final Path docDir = Paths.get(docsPath); try {//from ww w .j a v a 2s. c o m //org.apache.lucene.store.Directory instance Directory dir = FSDirectory.open(Paths.get(indexPath)); //analyzer with the default stop words Analyzer analyzer = new StandardAnalyzer(); //IndexWriter Configuration IndexWriterConfig iwc = new IndexWriterConfig(analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); //IndexWriter writes new index files to the directory IndexWriter writer = new IndexWriter(dir, iwc); //Its recursive method to iterate all files and directories indexDocs(writer, docDir); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.mathworks.xzheng.admin.CreateThreadedIndexTask.java
License:Apache License
public int doLogic() throws IOException { PerfRunData runData = getRunData();//from www .j a v a 2s .c om Config config = runData.getConfig(); IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_46, runData.getAnalyzer()); writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); writerConfig = CreateIndexTask.createWriterConfig(config, runData, IndexWriterConfig.OpenMode.CREATE, writerConfig.getIndexCommit()); IndexWriter writer = new ThreadedIndexWriter(runData.getDirectory(), writerConfig, config.get("writer.num.threads", 4), config.get("writer.max.thread.queue.size", 20)); runData.setIndexWriter(writer); return 1; }
From source file:com.mathworks.xzheng.analysis.codec.MetaphoneAnalyzerTest.java
License:Apache License
public void testKoolKat() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new MetaphoneReplacementAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new Field("contents", //#A "cool cat", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from ww w . j a va 2 s . co m writer.close(); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory)); Query query = new QueryParser(Version.LUCENE_46, //#B "contents", analyzer) //#B .parse("kool kat"); //#B TopDocs hits = searcher.search(query, 1); assertEquals(1, hits.totalHits); //#C int docID = hits.scoreDocs[0].doc; doc = searcher.doc(docID); assertEquals("cool cat", doc.get("contents")); //#D }
From source file:com.mathworks.xzheng.common.CreateTestIndex.java
License:Apache License
public static void main(String[] args) throws IOException { String dataDir = args[0];/*w ww .ja v a2s . c o m*/ String indexDir = args[1]; List<File> results = new ArrayList<File>(); findFiles(results, new File(dataDir)); System.out.println(results.size() + " books to index"); Directory dir = FSDirectory.open(new File(indexDir)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new MyStandardAnalyzer()); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter w = new IndexWriter(dir, config); for (File file : results) { Document doc = getDocument(dataDir, file); w.addDocument(doc); } w.close(); dir.close(); }
From source file:com.mathworks.xzheng.meetlucene.Indexer.java
License:Apache License
public Indexer(String indexDir) throws IOException { Directory dir = FSDirectory.open(new File(indexDir)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46)); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); writer = new IndexWriter(dir, config); //3 }
From source file:com.mathworks.xzheng.tools.FastVectorHighlighterSample.java
License:Apache License
static void makeIndex() throws IOException { IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(dir, config); for (String d : DOCS) { Document doc = new Document(); doc.add(new Field(F, d, Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS)); writer.addDocument(doc);//from w ww .j ava 2 s. c o m } writer.close(); }
From source file:com.miliworks.virgo.test.LuceneIndexAndSearchDemo.java
License:Apache License
/** * /* ww w. j av a 2 s.c o m*/ * ??? * @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(Version.LUCENE_40, 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(Version.LUCENE_40, 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(); } } } }