List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode
public IndexWriterConfig setOpenMode(OpenMode openMode)
From source file:org.apache.solr.codecs.test.testONSQLWrapperDirectory.java
License:Apache License
public static void main(String[] args) { try {// w ww .ja v a 2s . c o m testUtil.initPropsONSQL(); //----------- index documents ------- StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_1); //Codec cd = new ONSQLCodec("omega"); //Codec.setDefault(cd); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_1, analyzer); // recreate the index on each execution config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); //config.setCodec(new SimpleTextCodec()); ONSQLCodec codec = new ONSQLCodec(); config.setCodec(codec); config.setUseCompoundFile(false); Directory luceneDir = new ONSQLWrapperDirectory(new File(INDEX_ROOT_FOLDER)); String[] list = luceneDir.listAll(); System.out.println("listall length=" + list.length); for (int i = 0; i < list.length; i++) { System.out.println(list[i]); } IndexWriter writer = new IndexWriter(luceneDir, config); writer.addDocument(Arrays.asList(new TextField("title", "The title of the first document", Store.YES), new TextField("content", "The content of the first document", Store.YES), new TextField("global_bu_id", "1b", Store.YES), new TextField("omega_order_num", "1n", Store.YES))); writer.addDocument(Arrays.asList(new TextField("title", "The tAtle of the second document", Store.YES), new TextField("content", "The content of the second document", Store.YES), new TextField("global_bu_id", "1k", Store.YES), new TextField("omega_order_num", "2b", Store.YES))); writer.addDocument(Arrays.asList(new TextField("title", "The title of the third document", Store.YES), new TextField("content", "The content of the third document", Store.YES), new TextField("global_bu_id", "2k", Store.YES), new TextField("omega_order_num", "3b", Store.YES))); writer.addDocument(Arrays.asList(new TextField("title", "The title of the fourth document", Store.YES), new TextField("content", "The content of the fourth document", Store.YES), new TextField("global_bu_id", "2k", Store.YES), new TextField("omega_order_num", "4b", Store.YES))); //writer.commit(); writer.close(); /* IndexReader reader = DirectoryReader.open(luceneDir); // now test for docs if (reader.numDocs() != 3) throw new IOException("amount of returned docs are less than indexed"); else System.out.println("test passed"); */ searchIndex("content", "second"); System.out.println("---- now we delete docs for second document----"); deleteDocs("content", "second"); System.out.println("--- and repeat search-----"); searchIndex("content", "second"); } catch (Throwable te) { te.printStackTrace(); } }
From source file:org.apache.solr.codecs.test.testONSQLWrapperDirectory.java
License:Apache License
public static void deleteDocs(String searchField, String searchString) throws IOException, ParseException { System.out.println("deleting docs for '" + searchString + "'"); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_1, new StandardAnalyzer(Version.LUCENE_4_10_1)); config.setOpenMode(IndexWriterConfig.OpenMode.APPEND); ONSQLCodec codec = new ONSQLCodec(); config.setCodec(codec);/*from www. j a v a 2s . c om*/ config.setUseCompoundFile(false); Directory luceneDir = new ONSQLWrapperDirectory(new File(INDEX_ROOT_FOLDER)); IndexWriter writer = new IndexWriter(luceneDir, config); QueryParser queryParser = new QueryParser(Version.LUCENE_4_10_1, searchField, new StandardAnalyzer(Version.LUCENE_4_10_1)); Query query = queryParser.parse(searchString); writer.deleteDocuments(query); writer.commit(); writer.close(); luceneDir.close(); System.out.println("docs were deleted"); }
From source file:org.apache.solr.codecs.test.testSimpleTextCodec.java
License:Apache License
public static void main(String[] args) { try {/*ww w. j a v a 2s . co m*/ plaintextDir = assureDirectoryExists(new File(INDEX_ROOT_FOLDER, "plaintext")); //----------- index documents ------- StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_48); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_48, analyzer); // recreate the index on each execution config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); config.setCodec(new SimpleTextCodec()); config.setUseCompoundFile(false); Directory luceneDir = FSDirectory.open(plaintextDir); IndexWriter writer = new IndexWriter(luceneDir, config); writer.addDocument(Arrays.asList(new TextField("title", "The title of my first document", Store.YES), new TextField("content", "The content of the first document", Store.YES))); writer.addDocument(Arrays.asList(new TextField("title", "The tAtle of the second document", Store.YES), new TextField("content", "The content of the second document", Store.YES))); writer.addDocument(Arrays.asList(new TextField("title", "The title of the third document", Store.YES), new TextField("content", "The content of the third document", Store.YES))); writer.commit(); writer.close(); IndexReader reader = DirectoryReader.open(luceneDir); // now test for docs if (reader.numDocs() != 3) throw new IOException("amount of returned docs are less than indexed"); else System.out.println("test passed"); searchIndex("content", "third"); } catch (Throwable te) { te.printStackTrace(); } }
From source file:org.apache.solr.core.snapshots.SolrSnapshotManager.java
License:Apache License
/** * This method deletes index files of the {@linkplain IndexCommit} for the specified generation number. * * @param core The Solr core/* ww w.ja va2 s . c om*/ * @param dir The index directory storing the snapshot. * @throws IOException in case of I/O errors. */ private static void deleteSnapshotIndexFiles(SolrCore core, Directory dir, IndexDeletionPolicy delPolicy) throws IOException { IndexWriterConfig conf = core.getSolrConfig().indexConfig.toIndexWriterConfig(core); conf.setOpenMode(OpenMode.APPEND); conf.setMergePolicy(NoMergePolicy.INSTANCE);//Don't want to merge any commits here! conf.setIndexDeletionPolicy(delPolicy); conf.setCodec(core.getCodec()); try (SolrIndexWriter iw = new SolrIndexWriter("SolrSnapshotCleaner", dir, conf)) { // Do nothing. The only purpose of opening index writer is to invoke the Lucene IndexDeletionPolicy#onInit // method so that we can cleanup the files associated with specified index commit. // Note the index writer creates a new commit during the close() operation (which is harmless). } }
From source file:org.apache.solr.core.SolrCoreCheckLockOnStartupTest.java
License:Apache License
@Override @Before//from ww w .ja va2 s.com public void setUp() throws Exception { super.setUp(); System.setProperty("solr.directoryFactory", "org.apache.solr.core.SimpleFSDirectoryFactory"); //explicitly creates the temp dataDir so we know where the index will be located createTempDir(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, null); Directory directory = newFSDirectory(new File(dataDir, "index")); //creates a new index on the known location new IndexWriter(directory, indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE)).close(); directory.close(); }
From source file:org.apache.vxquery.runtime.functions.index.IndexConstructorUtil.java
License:Apache License
public void evaluate(String collectioFolder, String indexFolder, IPointable result, ArrayBackedValueStorage abvs, ITreeNodeIdProvider nodeIdProvider, ArrayBackedValueStorage abvsFileNode, boolean isElementPath, String nodeId) throws IOException { metaFileUtil = new MetaFileUtil(indexFolder); isMetaFilePresent = metaFileUtil.isMetaFilePresent(); metaFileUtil.setCollection(collectioFolder); File collectionDirectory = new File(collectioFolder); if (!collectionDirectory.exists()) { throw new IOException("The collection directory (" + collectioFolder + ") does not exist."); }/*from w w w . j a va 2 s . c om*/ try { abvs.reset(); sb.reset(abvs); Directory dir = FSDirectory.open(Paths.get(indexFolder)); Analyzer analyzer = new CaseSensitiveAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); // Create will overwrite the index everytime iwc.setOpenMode(OpenMode.CREATE); //Create an index writer IndexWriter writer = new IndexWriter(dir, iwc); //Add files to index indexXmlFiles(collectionDirectory, writer, isElementPath, abvsFileNode, nodeIdProvider, sb, nodeId); if (!isMetaFilePresent) { // Write metadata map to a file. metaFileUtil.updateMetadataMap(metadataMap, indexFolder); metaFileUtil.writeMetadataToFile(); } //This makes write slower but search faster. writer.forceMerge(1); writer.close(); sb.finish(); result.set(abvs); } catch (IOException e) { throw new SystemException(ErrorCode.SYSE0001, e); } }
From source file:org.apache.wiki.search.LuceneSearchProvider.java
License:Apache License
IndexWriter getIndexWriter(Directory luceneDir) throws CorruptIndexException, LockObtainFailedException, IOException, ProviderException { IndexWriter writer = null;/*from w w w .j a va2 s .c o m*/ IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, getLuceneAnalyzer()); writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(luceneDir, writerConfig); // writer.setInfoStream( System.out ); return writer; }
From source file:org.archive.index.AsAReference.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 ww . ja 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 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)); // :Post-Release-Update-Version.LUCENE_XY: Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, 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 -Xmxm or -Xmx1g): // // iwc.setRAMBufferSizeMB(.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:org.archive.index.IndexFiles.java
License:Apache License
/** * index files: ..._check.xml// w w w . ja v a2 s. c o m * **/ public static void indexFiles_check(String dirStr) { String indexPath = TDirectory.LPFileIndexPath; try { logOddFile = new PrintStream( new FileOutputStream(new File(TDirectory.ROOT_OUTPUT + "logOddCheckIndexFiles.txt"))); System.out.println("Indexing to directory '" + indexPath + "'..."); Directory dir = FSDirectory.open(new File(indexPath)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, analyzer); boolean create = true; 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); } IndexWriter indexWriter = new IndexWriter(dir, iwc); Date start = new Date(); File dirFile = new File(dirStr); File[] files = dirFile.listFiles(); System.out.println(files.length); int count = 1; int badCount = 0; for (File f : files) { System.out.print("file-" + count + "\t"); count++; List<org.apache.lucene.document.Document> docs = new ArrayList<org.apache.lucene.document.Document>(); List<TreeMap<String, String>> checkdocList = TemLoader.parseCheckFile(logOddFile, f.getAbsolutePath()); if (null == checkdocList) { System.out.print("null"); System.out.println(); badCount++; continue; } System.out.print(f); System.out.println(); for (TreeMap<String, String> checkdoc : checkdocList) { // make a new, empty document org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document(); Field idField = new StringField("id", checkdoc.get("id"), Field.Store.YES); doc.add(idField); for (Entry<String, String> entry : checkdoc.entrySet()) { if (!entry.getKey().equals("id")) { StoredField storeField = new StoredField(entry.getKey(), entry.getValue()); doc.add(storeField); } } docs.add(doc); } for (org.apache.lucene.document.Document doc : docs) { indexWriter.addDocument(doc); } } indexWriter.commit(); indexWriter.close(); logOddFile.flush(); logOddFile.close(); Date end = new Date(); System.out.println(end.getTime() - start.getTime() + " total milliseconds"); System.out.println("BadCount:\t" + badCount); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }
From source file:org.archive.tnh.tools.IndexMerger.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("IndexMerger [-v|-o|-f] <dest> <source>..."); System.exit(1);/*from www .j a v a 2s.c o m*/ } boolean verbose = false; boolean optimize = false; boolean force = false; int i = 0; for (; i < args.length; i++) { if ("-o".equals(args[i])) { optimize = true; } else if ("-f".equals(args[i])) { force = true; } else if ("-v".equals(args[i])) { verbose = true; } else { break; } } if ((args.length - i) < (2 - (optimize ? 1 : 0))) { System.err.println("Erorr: no source files!"); System.err.println("IndexMerger [-v|-o|-f] <dest> <source>..."); System.exit(1); } File dest = new File(args[i++]); if (!force && dest.exists()) { System.err.println("Destination exits, use -f to force merging into existing index: " + dest); System.exit(2); } IndexReader ir[] = new IndexReader[args.length - i]; for (int j = i; j < args.length; j++) { ir[j - i] = IndexReader.open(new MMapDirectory(new File(args[j])), true /* read-only */ ); } IndexWriter w = null; try { // Configure the IndexWriter. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, null); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); w = new IndexWriter(new MMapDirectory(dest), config); if (verbose) { w.setInfoStream(System.out); } if (ir.length > 0) { w.addIndexes(ir); } if (optimize) { w.optimize(); } w.commit(); w.close(); } catch (IOException ioe) { System.err.println("Error: " + args[0] + " " + ioe); if (w != null) w.close(); } }