List of usage examples for org.apache.lucene.index IndexWriter IndexWriter
public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException
conf
. From source file:com.foundationdb.server.service.text.Indexer.java
License:Open Source License
public Indexer(FullTextIndexShared index, Analyzer analyzer) throws IOException { IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, analyzer); iwc.setMaxBufferedDeleteTerms(1); // The deletion needs to be reflected immediately (on disk) this.index = index; this.writer = new IndexWriter(index.open(), iwc); }
From source file:com.fuerve.villageelder.actions.results.SearchResultItemTest.java
License:Apache License
private void buildDummyIndex(final Directory indexDirectory, final Directory taxonomyDirectory) throws IOException { IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer()); iwc.setOpenMode(OpenMode.CREATE);/*ww w . j a va 2s . c om*/ IndexWriter iw = new IndexWriter(indexDirectory, iwc); TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectory, OpenMode.CREATE); List<CategoryPath> categories = new ArrayList<CategoryPath>(); FacetFields facetFields = new FacetFields(tw); Document doc = new Document(); categories.clear(); doc.add(new StringField("Author", "foo", Store.YES)); categories.add(new CategoryPath("Author", "foo")); doc.add(new LongField("RevisionNumber", 50L, Store.YES)); doc.add(new StringField("Revision", "50", Store.YES)); doc.add(new TextField("Message", "stuff", Store.YES)); iw.addDocument(doc); facetFields.addFields(doc, categories); doc = new Document(); facetFields = new FacetFields(tw); categories.clear(); doc.add(new StringField("Author", "bar", Store.YES)); categories.add(new CategoryPath("Author", "bar")); doc.add(new LongField("RevisionNumber", 5000L, Store.YES)); doc.add(new StringField("Revision", "5000", Store.YES)); doc.add(new TextField("Message", "stuff", Store.YES)); iw.addDocument(doc); facetFields.addFields(doc, categories); tw.commit(); tw.close(); iw.commit(); iw.close(); }
From source file:com.fuerve.villageelder.indexing.IndexManager.java
License:Apache License
/** * Gets the writers for the regular and taxonomy indices ready to go. * @throws IOException A fatal exception occurred while trying to * construct the index writers.//from ww w . j av a 2 s.c o m */ private void initializeWriters() throws IOException { if (luceneVersion == null || analyzer == null) { throw new IllegalArgumentException("The Lucene version and the index analyzer were unspecified " + "when attempting to create the index writers"); } IndexWriterConfig iwc = new IndexWriterConfig(luceneVersion, analyzer); iwc.setOpenMode(openMode); indexWriter = new IndexWriter(indexDirectory, iwc); taxonomyWriter = new DirectoryTaxonomyWriter(taxonomyDirectory, openMode); }
From source file:com.fuerve.villageelder.search.SearcherTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Searcher#initializeSearch()}. *//*from w ww.ja v a2s . co m*/ @SuppressWarnings("unused") @Test public final void testInitializeSearch() throws Exception { // Gather declared fields. Field indexDirectoryField = Searcher.class.getDeclaredField("indexDirectory"); Field taxonomyDirectoryField = Searcher.class.getDeclaredField("taxonomyDirectory"); Field indexDirectoryNameField = Searcher.class.getDeclaredField("indexDirectoryName"); Field taxonomyDirectoryNameField = Searcher.class.getDeclaredField("taxonomyDirectoryName"); Field stringDirectoriesField = Searcher.class.getDeclaredField("stringDirectories"); Field initializedField = Searcher.class.getDeclaredField("initialized"); Field searchField = Searcher.class.getDeclaredField("search"); Field indexReaderField = Searcher.class.getDeclaredField("indexReader"); Field indexSearcherField = Searcher.class.getDeclaredField("indexSearcher"); Field taxonomyReaderField = Searcher.class.getDeclaredField("taxonomyReader"); indexDirectoryField.setAccessible(true); taxonomyDirectoryField.setAccessible(true); indexDirectoryNameField.setAccessible(true); taxonomyDirectoryNameField.setAccessible(true); stringDirectoriesField.setAccessible(true); initializedField.setAccessible(true); searchField.setAccessible(true); indexReaderField.setAccessible(true); indexSearcherField.setAccessible(true); taxonomyReaderField.setAccessible(true); // Setup Directory indexDirectoryExpected = new RAMDirectory(); Directory taxonomyDirectoryExpected = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer()); IndexWriter iw = new IndexWriter(indexDirectoryExpected, iwc); TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectoryExpected, OpenMode.CREATE); iw.commit(); tw.commit(); Searcher target = new Searcher(indexDirectoryExpected, taxonomyDirectoryExpected); target.initializeSearch(); // Gather field values. Directory indexDirectoryActual = (Directory) indexDirectoryField.get(target); Directory taxonomyDirectoryActual = (Directory) taxonomyDirectoryField.get(target); String indexDirectoryNameActual = (String) indexDirectoryNameField.get(target); String taxonomyDirectoryNameActual = (String) taxonomyDirectoryNameField.get(target); boolean stringDirectoriesActual = stringDirectoriesField.getBoolean(target); boolean initializedActual = initializedField.getBoolean(target); Search searchFieldActual = (Search) searchField.get(target); IndexReader indexReaderActual = (IndexReader) indexReaderField.get(target); IndexSearcher indexSearcherActual = (IndexSearcher) indexSearcherField.get(target); TaxonomyReader taxonomyReaderActual = (TaxonomyReader) taxonomyReaderField.get(target); // Test assertEquals(true, initializedActual); assertNotNull(indexReaderActual); assertNotNull(indexSearcherActual); assertNotNull(taxonomyReaderActual); // Finish tw.close(); iw.close(); }
From source file:com.fuerve.villageelder.search.SearchQueryParserTest.java
License:Apache License
private IndexReader buildDummyIndex() throws IOException { RAMDirectory indexDirectory = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer()); iwc.setOpenMode(OpenMode.CREATE);/* w ww.j a v a 2 s. c o m*/ IndexWriter iw = new IndexWriter(indexDirectory, iwc); Document doc = new Document(); doc.add(new StringField("Author", "foo", Field.Store.YES)); doc.add(new LongField("RevisionNumber", 50L, Field.Store.YES)); doc.add(new StringField("Revision", "50", Field.Store.YES)); doc.add(new TextField("Message", "stuff", Field.Store.YES)); iw.addDocument(doc); doc = new Document(); doc.add(new StringField("Author", "bar", Field.Store.YES)); doc.add(new LongField("RevisionNumber", 5000L, Field.Store.YES)); doc.add(new StringField("Revision", "5000", Field.Store.YES)); doc.add(new TextField("Message", "stuff", Field.Store.YES)); iw.addDocument(doc); iw.commit(); iw.close(); DirectoryReader result = DirectoryReader.open(indexDirectory); return result; }
From source file:com.fuerve.villageelder.search.SearchTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Search#getFacetsCollector(org.apache.lucene.index.DirectoryReader, org.apache.lucene.facet.taxonomy.TaxonomyReader)}. *//*from w w w . j a v a 2 s .co m*/ @Test @SuppressWarnings({ "unchecked", "unused" }) public final void testGetFacetsCollector() throws Exception { // Constants Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT"); Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS"); Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS"); Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER"); Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS"); defaultSortField.setAccessible(true); defaultFacetsField.setAccessible(true); defaultFacetStringsField.setAccessible(true); defaultAnalyzerField.setAccessible(true); defaultHitsField.setAccessible(true); final Sort defaultSort = (Sort) defaultSortField.get(null); final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null); final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null); final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null); final int defaultHits = defaultHitsField.getInt(null); // Private members Field queryField = Search.class.getDeclaredField("query"); Field sortField = Search.class.getDeclaredField("sort"); Field facetsField = Search.class.getDeclaredField("facets"); queryField.setAccessible(true); sortField.setAccessible(true); facetsField.setAccessible(true); // Test setup QueryParser parser = getQueryParser(); Query queryExpected = parser.parse("test:foo"); List<FacetRequest> facetsExpected = new ArrayList<FacetRequest>(); Sort sortExpected = Sort.RELEVANCE; Search target = new Search(queryExpected, facetsExpected, sortExpected); target.addFacet("test", 100); // Gather fields Query queryActual = (Query) queryField.get(target); Sort sortActual = (Sort) sortField.get(target); List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target); // Set up some dummy indices. Directory indexDirectory = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer()); IndexWriter iw = new IndexWriter(indexDirectory, iwc); Directory taxonomyDirectory = new RAMDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectory, OpenMode.CREATE); iw.commit(); tw.commit(); // Test FacetsCollector actual = target.getFacetsCollector(DirectoryReader.open(indexDirectory), new DirectoryTaxonomyReader(taxonomyDirectory)); assertEquals("DocsOnlyCollector", actual.getClass().getSimpleName()); iw.close(); tw.close(); taxonomyDirectory.close(); }
From source file:com.fun.sb.demo.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 = "/Users/baidu/temp/index/"; String docsPath = "/Users/baidu/temp/"; boolean create = true; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { indexPath = args[i + 1];/*from w w w . ja v a 2s .com*/ 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(); 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.gauronit.tagmata.core.Indexer.java
License:Open Source License
private synchronized void loadIndexes() throws Exception { try {/* ww w . j ava 2s . c om*/ // Properties props = new Properties(); // props.load(new FileInputStream(new File("indexer.properties"))); indexDir = new File(".").getCanonicalPath() + File.separator + "indexes"; // props.getProperty("indexDir"); // Open or create main index -- try { IndexWriter iw = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); iw.close(); iw = null; } catch (Exception ex) { try { IndexWriter iw = new IndexWriter( FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); iw.close(); iw = null; } catch (Exception e) { logger.log(Level.SEVERE, "Failed creating main index", e); System.exit(0); } } // -- } catch (Exception ex) { logger.log(Level.SEVERE, "Failed to load indexes", ex); throw ex; } loaded = true; }
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public String createIndex(String indexDisplayName) { try {/*ww w .j a v a2s . com*/ UUID uuid = UUID.randomUUID(); String indexName = uuid.toString().substring(uuid.toString().length() - 8, uuid.toString().length()); IndexWriter iw = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); iw.prepareCommit(); iw.commit(); iw.close(); iw = null; Document doc = new Document(); doc.add(new Field("displayName", indexDisplayName, Store.YES, Index.NOT_ANALYZED)); doc.add(new Field("indexName", indexName, Store.YES, Index.NOT_ANALYZED)); IndexWriter mainIndexWriter = new IndexWriter( FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); mainIndexWriter.addDocument(doc); mainIndexWriter.commit(); mainIndexWriter.close(); mainIndexWriter = null; return indexName; } catch (IOException ex) { Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public void deleteIndex(String indexName) { try {//from w w w. java 2s . c om IndexWriter mainIndexWriter = new IndexWriter( FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); Query q = new QueryParser(Version.LUCENE_35, "indexName", new StandardAnalyzer(Version.LUCENE_35)) .parse(indexName); mainIndexWriter.deleteDocuments(q); mainIndexWriter.commit(); mainIndexWriter.close(); mainIndexWriter = null; IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)), new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); writer.deleteAll(); writer.prepareCommit(); writer.commit(); writer.close(); writer = null; IOUtil.deleteDir(new File(indexDir + File.separator + indexName)); } catch (Exception ex) { ex.printStackTrace(); } finally { } }