Example usage for org.apache.lucene.index IndexWriter tryDeleteDocument

List of usage examples for org.apache.lucene.index IndexWriter tryDeleteDocument

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter tryDeleteDocument.

Prototype

public synchronized long tryDeleteDocument(IndexReader readerIn, int docID) throws IOException 

Source Link

Document

Expert: attempts to delete by document ID, as long as the provided reader is a near-real-time reader (from DirectoryReader#open(IndexWriter) ).

Usage

From source file:collene.TestIndexing.java

License:Apache License

@Test
public void test() throws IOException, ParseException {
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);

    // write it out.
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, config);

    for (int i = 0; i < 100; i++) {
        Collection<Document> documents = new ArrayList<Document>();
        Document doc = new Document();
        doc.add(new Field("key", "aaa_" + i, TextField.TYPE_STORED));
        doc.add(new Field("not", "notaaa", TextField.TYPE_NOT_STORED));
        doc.add(new Field("meta", "aaa_meta_aaa_" + i, TextField.TYPE_STORED));
        documents.add(doc);// ww  w. j a  v a2 s. co  m

        writer.addDocuments(documents);

        writer.commit();
        writer.forceMerge(1);
        writer.forceMergeDeletes(true);
    }

    // now read it back.
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    QueryParser parser = new QueryParser(Version.LUCENE_4_9, "key", analyzer);

    Query query = parser.parse("aaa_4");
    TopDocs docs = searcher.search(query, 1);
    int idToDelete = docs.scoreDocs[0].doc;
    Assert.assertTrue(docs.totalHits > 0);

    query = parser.parse("fersoius");
    docs = searcher.search(query, 1);
    Assert.assertFalse(docs.totalHits > 0);

    // delete that document.
    DirectoryReader reader = DirectoryReader.open(writer, true);
    writer.tryDeleteDocument(reader, idToDelete);

    reader.close();
    writer.close();

    // list files
    Set<String> files = new HashSet<String>();
    System.out.println("Listing files for " + directory.toString());
    for (String file : directory.listAll()) {
        files.add(file);
        System.out.println(" " + file);
    }

    if (strictFileChecking) {
        System.out.println("String file checking...");
        Sets.SetView<String> difference = Sets.difference(expectedFiles, files);
        Assert.assertEquals(Joiner.on(",").join(difference), 0, difference.size());
    }

    reader = DirectoryReader.open(directory);
    searcher = new IndexSearcher(reader);
    query = parser.parse("aaa_4");
    docs = searcher.search(query, 1);
    reader.close();
    Assert.assertFalse(docs.totalHits > 0);

    directory.close();
}

From source file:org.apache.maven.index.updater.DefaultIndexUpdater.java

License:Apache License

private static void filterDirectory(final Directory directory, final DocumentFilter filter) throws IOException {
    IndexReader r = null;//  www . j av  a 2  s  .  c om
    IndexWriter w = null;
    try {
        r = DirectoryReader.open(directory);
        w = new NexusIndexWriter(directory, new NexusAnalyzer(), false);

        Bits liveDocs = MultiFields.getLiveDocs(r);

        int numDocs = r.maxDoc();

        for (int i = 0; i < numDocs; i++) {
            if (liveDocs != null && !liveDocs.get(i)) {
                continue;
            }

            Document d = r.document(i);

            if (!filter.accept(d)) {
                boolean success = w.tryDeleteDocument(r, i);
                // FIXME handle deletion failure
            }
        }
        w.commit();
    } finally {
        IndexUtils.close(r);
        IndexUtils.close(w);
    }

    w = null;
    try {
        // analyzer is unimportant, since we are not adding/searching to/on index, only reading/deleting
        w = new NexusIndexWriter(directory, new NexusAnalyzer(), false);

        w.commit();
    } finally {
        IndexUtils.close(w);
    }
}