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

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

Introduction

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

Prototype

public long deleteDocuments(Query... queries) throws IOException 

Source Link

Document

Deletes the document(s) matching any of the provided queries.

Usage

From source file:com.github.alvanson.xltsearch.IndexTask.java

License:Apache License

@Override
protected Boolean call() {
    IndexWriter iwriter = null;
    boolean result = false;

    updateMessage("started");
    try {/*from w  ww.ja v a2  s .c o  m*/
        int count = 0;
        Docket docket;

        IndexWriterConfig iwconfig = new IndexWriterConfig(config.getVersion(), config.getAnalyzer());
        iwconfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        iwconfig.setSimilarity(config.getSimilarity());
        iwriter = new IndexWriter(config.getDirectory(), iwconfig);

        while ((docket = inQueue.take()) != Docket.DONE) {
            count++;
            updateMessage(docket.relPath);
            switch (docket.status) {
            case PARSED:
                // index parsed file
                Document doc = new Document();
                // store relative path  ** must be indexed for updateDocument
                doc.add(new StringField(config.pathField, docket.relPath, Field.Store.YES));
                // index content
                doc.add(new TextField(config.contentField, docket.content.toString(), Field.Store.NO));
                // index standard metadata
                for (Map.Entry<String, Property> e : config.metadataFields.entrySet()) {
                    for (String value : docket.metadata.getValues(e.getValue())) {
                        doc.add(new TextField(e.getKey(), value, Field.Store.YES));
                    }
                }
                // store hashsum
                doc.add(new StringField(config.hashSumField, docket.hashSum, Field.Store.YES));
                // add/update document
                iwriter.updateDocument(new Term(config.pathField, docket.relPath), doc);
                // fall through
            case PASS:
                break;
            case DELETE:
                iwriter.deleteDocuments(new Term(config.pathField, docket.relPath));
                break;
            default:
                logger.error("Unexpected docket state while processing {}: {}", docket.relPath,
                        docket.status.toString());
                cancel(true); // cancel task
            }
            updateProgress(count, count + docket.workLeft);
        }
        // end of queue
        updateMessage("complete");
        updateProgress(count, count + docket.workLeft);
        result = true;
    } catch (IOException ex) {
        updateMessage("I/O exception");
        logger.error("I/O exception while writing to index", ex);
    } catch (InterruptedException ex) {
        if (isCancelled()) {
            updateMessage("cancelled");
        } else {
            updateMessage("interrupted");
            logger.error("Interrupted", ex);
        }
    }
    // close iwriter
    if (iwriter != null) {
        try {
            iwriter.close();
        } catch (IOException ex) {
            logger.warn("I/O exception while closing index writer", ex);
        }
    }
    return result;
}

From source file:com.github.mosuka.apache.lucene.example.cmd.DeleteCommand.java

License:Apache License

@Override
public void execute(Map<String, Object> attrs) {
    Map<String, Object> responseMap = new LinkedHashMap<String, Object>();

    String responseJSON = null;//from  w w w  .  j  av a 2  s . co m
    Directory indexDir = null;
    IndexWriter writer = null;

    try {
        String index = (String) attrs.get("index");
        String uniqueId = (String) attrs.get("unique_id");

        indexDir = FSDirectory.open(new File(index).toPath());

        IndexWriterConfig config = new IndexWriterConfig(LuceneExampleUtil.createAnalyzerWrapper());
        config.setOpenMode(OpenMode.CREATE_OR_APPEND);

        writer = new IndexWriter(indexDir, config);
        writer.deleteDocuments(new Term("id", uniqueId));
        writer.commit();

        responseMap.put("status", 0);
        responseMap.put("message", "OK");
    } catch (IOException e) {
        responseMap.put("status", 1);
        responseMap.put("message", e.getMessage());
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            responseMap.put("status", 1);
            responseMap.put("message", e.getMessage());
        }
        try {
            if (indexDir != null) {
                indexDir.close();
            }
        } catch (IOException e) {
            responseMap.put("status", 1);
            responseMap.put("message", e.getMessage());
        }
    }

    try {
        ObjectMapper mapper = new ObjectMapper();
        responseJSON = mapper.writeValueAsString(responseMap);
    } catch (IOException e) {
        responseJSON = String.format("{\"status\":1, \"message\":\"%s\"}", e.getMessage());
    }
    System.out.println(responseJSON);
}

From source file:com.github.wxiaoqi.search.lucene.LuceneDao.java

License:Open Source License

public void delete(IndexObject indexObject) {
    IndexWriter indexWriter = null;
    try {/*from   w  w w. j  a va 2  s.  c  o  m*/
        Term term = new Term("id", indexObject.getId().toString());
        IndexWriterConfig config = new IndexWriterConfig(this.getAnalyzer());
        indexWriter = new IndexWriter(this.getDirectory(), config);
        indexWriter.deleteDocuments(term);
    } 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.globalsight.ling.lucene.Index.java

License:Apache License

public void deleteDocument(long p_mainId, long p_subId) throws IOException {
    synchronized (m_state) {
        if (m_state != STATE_OPENED) {
            throw new IOException("index is not available");
        }/*from www  . ja  v a2  s .co m*/
    }

    // clean cache if have
    LuceneCache.cleanLuceneCache(m_directory);

    try {
        m_lock.writeLock().acquire();

        try {
            IndexWriter w = getIndexWriter(true);
            //                IndexReader reader = DirectoryReader.open(m_fsDir);
            //                reader.delete(new Term(
            //                    IndexDocument.SUBID, String.valueOf(p_subId)));
            w.deleteDocuments(new Term(IndexDocument.SUBID, String.valueOf(p_subId)));

            w.close();
        } finally {
            m_lock.writeLock().release();
        }
    } catch (InterruptedException ex) {
        throw new IOException(ex.getMessage());
    }
}

From source file:com.globalsight.ling.tm2.lucene.LuceneIndexWriter.java

License:Apache License

public void remove(Collection p_tuvs) throws Exception {
    IndexWriterConfig conf = new IndexWriterConfig(LuceneUtil.VERSION, m_analyzer);
    conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(m_directory, conf);

    try {//from   w w  w  .j  a va2  s.c  o m
        for (Iterator it = p_tuvs.iterator(); it.hasNext();) {
            Object tuv = it.next();
            Long id = tuv instanceof BaseTmTuv ? ((BaseTmTuv) tuv).getId()
                    : tuv instanceof TM3Tuv ? ((TM3Tuv) tuv).getId() : null;

            Term term = new Term(TuvDocument.TUV_ID_FIELD, id.toString());
            writer.deleteDocuments(term);
        }
    } catch (Throwable e) {
        c_logger.error(e.getMessage(), e);
        //indexReader.undeleteAll();
        throw (e instanceof Exception ? (Exception) e : new Exception(e));
    } finally {
        writer.commit();
        writer.close();
    }

    // clean cache if have
    LuceneCache.cleanLuceneCache(m_indexDir);
}

From source file:com.intuit.tank.search.lucene.LuceneService.java

License:Open Source License

/**
 * Removes the indexes of the documents.
 * /* w  w w  . j av  a2s . c o  m*/
 * @param docs
 */
public void removeDocuments(List<Query> queries) {
    IndexWriter writer = getWriter();
    for (Query query : queries) {
        try {
            writer.deleteDocuments(query);
        } catch (Exception e) {
            e.printStackTrace();
            closeWriter(writer);
            throw new RuntimeException(e);
        }
    }
    closeWriter(writer);
}

From source file:com.jaeksoft.searchlib.index.WriterLucene.java

License:Open Source License

private int deleteDocumentNoLock(String field, String value) throws SearchLibException {
    IndexWriter indexWriter = null;
    try {/*from   w  w w .  j  ava 2 s  . com*/
        long l = indexLucene.getStatistics().getNumDocs();
        indexWriter = open();
        indexWriter.deleteDocuments(new Term(field, value));
        close(indexWriter);
        indexWriter = null;
        indexLucene.reloadNoLock();
        l = l - indexLucene.getStatistics().getNumDocs();
        if (l > 0)
            if (afterDeleteList != null)
                for (UpdateInterfaces.Delete afterDelete : afterDeleteList)
                    afterDelete.delete(field, value);
        return (int) l;
    } catch (IOException e) {
        throw new SearchLibException(e);
    } finally {
        close(indexWriter);
    }
}

From source file:com.jaeksoft.searchlib.index.WriterLucene.java

License:Open Source License

private int deleteDocumentsNoLock(String field, Collection<String> values) throws SearchLibException {
    IndexWriter indexWriter = null;
    try {/*  w w  w.j a  va2  s.  c  o  m*/
        Term[] terms = new Term[values.size()];
        int i = 0;
        for (String value : values)
            terms[i++] = new Term(field, value);
        long l = indexLucene.getStatistics().getNumDocs();
        indexWriter = open();
        indexWriter.deleteDocuments(terms);
        close(indexWriter);
        indexWriter = null;
        if (terms.length > 0)
            indexLucene.reloadNoLock();
        l = l - indexLucene.getStatistics().getNumDocs();
        if (l > 0)
            if (afterDeleteList != null)
                for (UpdateInterfaces.Delete afterDelete : afterDeleteList)
                    afterDelete.delete(field, values);
        return (int) l;
    } catch (IOException e) {
        throw new SearchLibException(e);
    } finally {
        close(indexWriter);
    }
}

From source file:com.jaeksoft.searchlib.index.WriterLucene.java

License:Open Source License

private int deleteDocumentsNoLock(AbstractSearchRequest query) throws SearchLibException {
    IndexWriter indexWriter = null;
    try {//from  w  w  w  . j a v  a2s  .c o  m
        long l = indexLucene.getStatistics().getNumDocs();
        indexWriter = open();
        indexWriter.deleteDocuments(query.getQuery());
        close(indexWriter);
        indexWriter = null;
        indexLucene.reloadNoLock();
        l = l - indexLucene.getStatistics().getNumDocs();
        return (int) l;
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (ParseException e) {
        throw new SearchLibException(e);
    } catch (SyntaxError e) {
        throw new SearchLibException(e);
    } finally {
        close(indexWriter);
    }
}

From source file:com.leavesfly.lia.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
            IndexWriter.MaxFieldLength.UNLIMITED);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new Field("id", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc);// www .  j  a  va  2s . c o  m
    }
    IndexReader reader = writer.getReader(); // #1
    IndexSearcher searcher = new IndexSearcher(reader); // #A

    Query query = new TermQuery(new Term("text", "aaa"));
    TopDocs docs = searcher.search(query, 1);
    assertEquals(10, docs.totalHits); // #B

    writer.deleteDocuments(new Term("id", "7")); // #2

    Document doc = new Document(); // #3
    doc.add(new Field("id", // #3
            "11", // #3
            Field.Store.NO, // #3
            Field.Index.NOT_ANALYZED_NO_NORMS)); // #3
    doc.add(new Field("text", // #3
            "bbb", // #3
            Field.Store.NO, // #3
            Field.Index.ANALYZED)); // #3
    writer.addDocument(doc); // #3

    IndexReader newReader = reader.reopen(); // #4
    assertFalse(reader == newReader); // #5
    reader.close(); // #6
    searcher = new IndexSearcher(newReader);

    TopDocs hits = searcher.search(query, 10); // #7
    assertEquals(9, hits.totalHits); // #7

    query = new TermQuery(new Term("text", "bbb")); // #8
    hits = searcher.search(query, 1); // #8
    assertEquals(1, hits.totalHits); // #8

    newReader.close();
    writer.close();
}