List of usage examples for org.apache.lucene.index IndexWriter deleteAll
@SuppressWarnings("try") public long deleteAll() throws IOException
From source file:WriteIndex.java
License:Apache License
/** * @param args// w ww . ja v a 2 s . c o m */ public static void main(String[] args) throws IOException { File docs = new File("documents"); File indexDir = new File(INDEX_DIRECTORY); Directory directory = FSDirectory.open(indexDir); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, analyzer); IndexWriter writer = new IndexWriter(directory, conf); writer.deleteAll(); for (File file : docs.listFiles()) { Metadata metadata = new Metadata(); ContentHandler handler = new BodyContentHandler(); ParseContext context = new ParseContext(); Parser parser = new AutoDetectParser(); InputStream stream = new FileInputStream(file); try { parser.parse(stream, handler, metadata, context); } catch (TikaException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } finally { stream.close(); } String text = handler.toString(); String fileName = file.getName(); Document doc = new Document(); doc.add(new Field("file", fileName, Store.YES, Index.NO)); for (String key : metadata.names()) { String name = key.toLowerCase(); String value = metadata.get(key); if (StringUtils.isBlank(value)) { continue; } if ("keywords".equalsIgnoreCase(key)) { for (String keyword : value.split(",?(\\s+)")) { doc.add(new Field(name, keyword, Store.YES, Index.NOT_ANALYZED)); } } else if ("title".equalsIgnoreCase(key)) { doc.add(new Field(name, value, Store.YES, Index.ANALYZED)); } else { doc.add(new Field(name, fileName, Store.YES, Index.NOT_ANALYZED)); } } doc.add(new Field("text", text, Store.NO, Index.ANALYZED)); writer.addDocument(doc); } writer.commit(); writer.deleteUnusedFiles(); System.out.println(writer.maxDoc() + " documents written"); }
From source file:br.bireme.ngrams.NGrams.java
public static void index(final NGIndex index, final NGSchema schema, final String inFile, final String inFileEncoding) throws IOException, ParseException { if (index == null) { throw new NullPointerException("index"); }//w ww .j a v a 2 s .c o m if (schema == null) { throw new NullPointerException("schema"); } if (inFile == null) { throw new NullPointerException("inFile"); } if (inFileEncoding == null) { throw new NullPointerException("inFileEncoding"); } final Charset charset = Charset.forName(inFileEncoding); final IndexWriter writer = index.getIndexWriter(false); int cur = 0; try (BufferedReader reader = Files.newBufferedReader(new File(inFile).toPath(), charset)) { writer.deleteAll(); while (true) { final String line; try { line = reader.readLine(); } catch (MalformedInputException mie) { System.err.println("Line with another encoding. Line number:" + (++cur)); continue; } if (line == null) { break; } final boolean ret = indexDocument(index, writer, schema, line, false); if (ret && (++cur % 100000 == 0)) { System.out.println(">>> " + cur); } } writer.forceMerge(1); // optimize index writer.close(); } }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
private void delete() throws Exception { final IndexWriterConfig config = new IndexWriterConfig(ConstResolver.VERSION, analyzer); final IndexWriter writer = new IndexWriter(getDirectory(), config); writer.deleteAll(); writer.close();//from w ww.j av a 2 s . com }
From source file:com.chimpler.example.FacetLuceneIndexer.java
License:Apache License
public static void main(String args[]) throws Exception { // if (args.length != 3) { // System.err.println("Parameters: [index directory] [taxonomy directory] [json file]"); // System.exit(1); // }/*from w w w. ja va 2s. c o m*/ String indexDirectory = "index"; String taxonomyDirectory = "taxonomy"; String jsonFileName = "/home/qiuqiang/workspace/facet-lucene-example/books.json"; IndexWriterConfig writerConfig = new IndexWriterConfig(LUCENE_VERSION, new WhitespaceAnalyzer(LUCENE_VERSION)); writerConfig.setOpenMode(OpenMode.APPEND); IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File(indexDirectory)), writerConfig); TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(MMapDirectory.open(new File(taxonomyDirectory)), OpenMode.APPEND); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); String content = IOUtils.toString(new FileInputStream(jsonFileName)); JSONArray bookArray = new JSONArray(content); Field idField = new IntField("id", 0, Store.YES); Field titleField = new TextField("title", "", Store.YES); Field authorsField = new TextField("authors", "", Store.YES); Field bookCategoryField = new TextField("book_category", "", Store.YES); indexWriter.deleteAll(); FacetFields facetFields = new FacetFields(taxonomyWriter); for (int i = 0; i < bookArray.length(); i++) { Document document = new Document(); JSONObject book = bookArray.getJSONObject(i); int id = book.getInt("id"); String title = book.getString("title"); String bookCategory = book.getString("book_category"); List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>(); String authorsString = ""; JSONArray authors = book.getJSONArray("authors"); for (int j = 0; j < authors.length(); j++) { String author = authors.getString(j); if (j > 0) { authorsString += ", "; } categoryPaths.add(new CategoryPath("author", author)); authorsString += author; } categoryPaths.add(new CategoryPath("book_category" + bookCategory, '/')); idField.setIntValue(id); titleField.setStringValue(title); authorsField.setStringValue(authorsString); bookCategoryField.setStringValue(bookCategory); facetFields.addFields(document, categoryPaths); document.add(idField); document.add(titleField); document.add(authorsField); document.add(bookCategoryField); indexWriter.addDocument(document); System.out.printf("Book: id=%d, title=%s, book_category=%s, authors=%s\n", id, title, bookCategory, authors); } taxonomyWriter.prepareCommit(); try { taxonomyWriter.commit(); } catch (Exception e) { taxonomyWriter.rollback(); } // taxonomyWriter.close(); // // indexWriter.commit(); // indexWriter.close(); String query = "story"; IndexReader indexReader = DirectoryReader.open(indexWriter, false); IndexReader indexReader2 = DirectoryReader.open(indexWriter, false); System.out.println(indexReader == indexReader2); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader newTaxonomyReader = DirectoryTaxonomyReader.openIfChanged(taxonomyReader); if (newTaxonomyReader != null) { TaxonomyReader tmp = taxonomyReader; taxonomyReader = newTaxonomyReader; tmp.close(); } else { System.out.println("null"); } ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>(); facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); FacetSearchParams searchParams = new FacetSearchParams(facetRequests); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); taxonomyWriter.commit(); taxonomyWriter.close(); indexWriter.commit(); indexWriter.close(); }
From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java
License:Open Source License
@Override public synchronized void deleteAll() { try {//from ww w .ja v a2s. c om Directory directory = FSDirectory.open(indexDir); Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH); IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_44, analyzer)); indexWriter.deleteAll(); indexWriter.addDocument(new Document()); // indexWriter.optimize(); indexWriter.close(); directory.close(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.dreamerpartner.codereview.lucene.IndexHelper.java
License:Apache License
/** * ?/* w w w . j a v a 2 s . com*/ * @param module ? * @throws IOException */ @SuppressWarnings("deprecation") public static void deleteAll(String module) throws IOException { long beginTime = System.currentTimeMillis(); IndexWriter writer = null; try { Directory dir = FSDirectory.open(new File(LuceneUtil.getIndexPath(module))); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0, analyzer); iwc.setMaxBufferedDocs(100); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(dir, iwc); writer.deleteAll(); writer.commit(); } finally { long endTime = System.currentTimeMillis(); logger.debug(module + " deleteAll " + (endTime - beginTime) + " milliseconds."); if (writer != null) writer.close(); } }
From source file:com.gauronit.tagmata.core.Indexer.java
License:Open Source License
public void deleteIndex(String indexName) { try {//from w w w . j a va 2 s. c o m 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 { } }
From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java
License:Open Source License
public void deleteAll() { IndexWriter indexWriter = null; try {/*from w w w. j av a2 s . c o m*/ IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer()); indexWriter = new IndexWriter(this.getDirectory(), config); Long result = indexWriter.deleteAll(); /**/ indexWriter.forceMergeDeletes(); log.info("deleted:{}", result); } catch (Exception e) { e.printStackTrace(); try { indexWriter.rollback(); } catch (IOException e1) { e1.printStackTrace(); } } finally { try { indexWriter.close(); } catch (IOException e1) { e1.printStackTrace(); } } }
From source file:com.intuit.tank.search.lucene.LuceneService.java
License:Open Source License
/** * Clears all the indices from the search index location; *//*from w w w. j a va2 s. c om*/ public void clearIndex() { IndexWriter writer = getWriter(); try { writer.deleteAll(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { closeWriter(writer); } }
From source file:com.jaeksoft.searchlib.index.WriterLocal.java
License:Open Source License
@Override public void deleteAll() throws SearchLibException { IndexWriter indexWriter = null; try {/* w w w.j av a 2 s. c o m*/ indexWriter = open(); indexWriter.deleteAll(); close(indexWriter); indexWriter = null; } catch (CorruptIndexException e) { throw new SearchLibException(e); } catch (LockObtainFailedException e) { throw new SearchLibException(e); } catch (IOException e) { throw new SearchLibException(e); } finally { close(indexWriter); } }