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.m3958.apps.pcms.lucene.IndexFiles.java

License:Apache License

static void indexCusValues(IndexWriter writer) throws IOException {
    // make a new, empty document
    Document doc = new Document();

    Field custidField = new StringField("custid", "fhsites-6376743", Field.Store.YES);
    doc.add(custidField);// w  w  w.  j  av a  2s .  c o m

    Field emailField = new StringField("email", "jianglibo@gmail.com", Field.Store.YES);
    doc.add(emailField);

    Field idField = new StringField("_id", "iooweokodkkkkosdodosdoods", Field.Store.YES);
    doc.add(idField);

    Field pathField = new StringField("_sn", "2013-05-0001", Field.Store.YES);
    doc.add(pathField);

    Calendar c = Calendar.getInstance();
    doc.add(new LongField("_createdAt", c.getTimeInMillis(), Field.Store.NO));
    doc.add(new LongField("_updatedAt", c.getTimeInMillis(), Field.Store.NO));

    doc.add(new TextField("contents", new StringReader("hello cusvalue")));

    writer.updateDocument(new Term("_id", "iooweokodkkkkosdodosdoods"), doc);
    writer.deleteDocuments(new Term("_id", "iooweokodkkkkosdodosdoods"));
}

From source file:com.mathworks.xzheng.indexing.IndexingTest.java

License:Apache License

public void testDeleteAfterOptimize() throws IOException {
    IndexWriter writer = getWriter();
    assertEquals(2, writer.numDocs());/*  w w  w .j a va 2  s .  co m*/
    writer.deleteDocuments(new Term("id", "1"));
    //writer.optimize();                //3
    writer.forceMerge(1);
    writer.commit();
    assertFalse(writer.hasDeletions());
    assertEquals(1, writer.maxDoc()); //C
    assertEquals(1, writer.numDocs()); //C    
    writer.close();
}

From source file:com.mathworks.xzheng.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46,
            new StandardAnalyzer(Version.LUCENE_46));
    IndexWriter writer = new IndexWriter(dir, config);
    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);//w ww  . j  ava  2 s  .  c o  m
    }
    IndexReader reader = DirectoryReader.open(writer.getDirectory()); // #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
    IndexReader newReader = DirectoryReader.open(writer.getDirectory()); // #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();
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneBuilder.java

License:Open Source License

private void updateKeyMapDocument(StandardQueryParser parser, IndexWriter writer, ITemporalList tl,
        ITemporalKey tk, ITemporalData data, long endWrittenTime, LuceneField luceneField, KeyType keyType,
        KeyMap keyMap, Set<String> keyTypeNameSet, boolean isIdentityKeyPrimitive, SimpleDateFormat format)
        throws IOException {
    Query query = null;/*from   w w w .  jav  a2 s .co  m*/
    try {
        String queryString = String.format(TEMPORAL_KEY_QUERY_PREDICATE, tk.getIdentityKey(),
                tk.getStartValidTime(), tk.getEndValidTime(), tk.getWrittenTime());
        query = parser.parse(queryString, "__doc");
    } catch (Exception ex) {
        // Lucene 4.7 bug, internal message not serializable
        // Send message instead of nesting the cause.
        throw new RuntimeException(ex.getMessage());
    }

    writer.deleteDocuments(query);

    Document doc = createKeyMapDocument(parser, writer, tk, data, endWrittenTime, luceneField, keyType, keyMap,
            keyTypeNameSet, isIdentityKeyPrimitive, false, format);
    writer.addDocument(doc);
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneBuilder.java

License:Open Source License

private void updatePojoDocument(StandardQueryParser parser, IndexSearcher searcher, IndexWriter writer,
        ITemporalKey tk, ITemporalData data, long endWrittenTime, LuceneField luceneField,
        Method[] attributeGetters, boolean isIdentityKeyPrimitive, SimpleDateFormat format)
        throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Query query = null;/* www.j av a2s  .  c  o  m*/
    try {
        String queryString = String.format(TEMPORAL_KEY_QUERY_PREDICATE, tk.getIdentityKey(),
                tk.getStartValidTime(), tk.getEndValidTime(), tk.getWrittenTime());
        query = parser.parse(queryString, "__doc");
    } catch (Exception ex) {
        // Lucene 4.7 bug, internal message not serializable
        // Send message instead of nesting the cause.
        throw new RuntimeException(ex.getMessage());
    }

    writer.deleteDocuments(query);

    Document doc = createPojoDocument(parser, writer, tk, data, endWrittenTime, luceneField, attributeGetters,
            isIdentityKeyPrimitive, false, format);
    writer.addDocument(doc);
}

From source file:com.searchcode.app.service.CodeIndexer.java

License:Open Source License

/**
 * Deletes all files that belong to a repository.
 * TODO I don't think this clears anything from the facets, which it should
 *//*  w ww  .  j av a 2 s . c  om*/
public synchronized void deleteByReponame(String repoName) throws IOException {
    Directory dir = FSDirectory.open(Paths
            .get(Properties.getProperties().getProperty(Values.INDEXLOCATION, Values.DEFAULTINDEXLOCATION)));

    Analyzer analyzer = new CodeAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

    IndexWriter writer = new IndexWriter(dir, iwc);

    writer.deleteDocuments(new Term(Values.REPONAME, repoName));
    writer.close();
}

From source file:com.searchcode.app.service.CodeIndexer.java

License:Open Source License

/**
 * Deletes a file from the index using the code id which seems to be
 * the most reliable way of doing it/*from  w w w. j a v a2 s  .co  m*/
 * TODO Update the record and set the facets to a value we can ignore
 */
public synchronized void deleteByCodeId(String codeId) throws IOException {
    Directory dir = FSDirectory.open(Paths
            .get(Properties.getProperties().getProperty(Values.INDEXLOCATION, Values.DEFAULTINDEXLOCATION)));

    Analyzer analyzer = new CodeAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

    IndexWriter writer = new IndexWriter(dir, iwc);

    try {
        QueryParser parser = new QueryParser(Values.CONTENTS, analyzer);
        Query query = parser.parse(Values.CODEID + ":" + QueryParser.escape(codeId));
        writer.deleteDocuments(query);
    } catch (Exception ex) {
        Singleton.getLogger().warning(
                "ERROR - caught a " + ex.getClass() + " in CodeIndexer\n with message: " + ex.getMessage());
    } finally {
        writer.close();
    }
}

From source file:com.searchcode.app.service.IndexService.java

License:Open Source License

/**
 * Deletes all files that belong to a repository.
 * TODO I don't think this clears anything from the facets, which it should
 *//*from   w ww .  ja v  a2  s.c om*/
public synchronized void deleteByRepoName(String repoName) throws IOException {
    Directory dir = FSDirectory.open(this.INDEX_LOCATION);

    Analyzer analyzer = new CodeAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

    IndexWriter writer = new IndexWriter(dir, iwc);

    writer.deleteDocuments(new Term(Values.REPONAME, repoName));
    writer.close();
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.// ww  w.  ja  v  a 2s. c  o m
 */
public boolean addToIndex(final App app) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(app);
    String url = app.getURL().toExternalForm();
    String title = app.getTitle();
    String description = app.getDescription();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("app", existingDoc.get("class")) && areEqual(url, existingDoc.get("url"))
                && areEqual(title, existingDoc.get("title"))
                && areEqual(description, existingDoc.get("description"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "app", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("url", url, Field.Store.YES, Field.Index.TOKENIZED));
        if (title != null)
            newDoc.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
        if (description != null)
            newDoc.add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format("Indexed app[url=%s,title=%s,description=%s]", url, title, description));
    }

    return needNewEntry;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.//from  w w w.j a  va  2  s. c o  m
 */
public boolean addToIndex(final Group group) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(group);
    String handle = group.getHandle();
    String name = group.getName();
    String description = group.getDescription();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("group", existingDoc.get("class")) && areEqual(handle, existingDoc.get("handle"))
                && areEqual(name, existingDoc.get("name"))
                && areEqual(description, existingDoc.get("description"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "group", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("handle", handle, Field.Store.YES, Field.Index.TOKENIZED));
        newDoc.add(new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED));
        if (description != null)
            newDoc.add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format("Indexed group[handle=%s,name=%s,description=%s]", name, handle, description));
    }

    return needNewEntry;
}