Example usage for org.apache.lucene.index IndexReader close

List of usage examples for org.apache.lucene.index IndexReader close

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader close.

Prototype

@Override
public final synchronized void close() throws IOException 

Source Link

Document

Closes files associated with this index.

Usage

From source file:com.it.lucene.test.IndexSearch.java

private void doSearch(Query query) {
    try {//from w w w .  ja v a2s  . co  m
        // 2?IndexSearcher
        // 2.1 IndexReader
        // 2.1Directory
        Directory directory = FSDirectory.open(new File("F:\\Lucene Index"));
        IndexReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        // 3?indexSearcher?
        // ?
        TopDocs topDocs = searcher.search(query, 10);

        // 4??
        // ???
        long count = topDocs.totalHits;

        System.out.println("???:" + count);

        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

        for (ScoreDoc scoreDoc : scoreDocs) {
            int docId = scoreDoc.doc;
            Document doc = searcher.doc(docId);
            System.out.println("?ID" + doc.get("id"));
            System.out.println("???" + doc.get("name"));
            System.out.println("?" + doc.get("price"));
            System.out.println("?pic" + doc.get("pic"));
        }
        // 5?IndexReader
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public int deleteDocuments(int[] ids) throws IOException, SearchLibException {
    if (ids == null || ids.length == 0)
        return 0;
    IndexReader indexReader = null;
    try {// www. j ava2  s.c o  m
        int l = 0;
        indexReader = IndexReader.open(indexDirectory.getDirectory(), false);
        for (int id : ids)
            if (!indexReader.isDeleted(id)) {
                indexReader.deleteDocument(id);
                l++;
            }
        indexReader.close();
        indexReader = null;
        return l;
    } finally {
        IOUtils.close(indexReader);
    }
}

From source file:com.jivesoftware.forum.database.DbSearchManager.java

License:Open Source License

/**
 * Deletes a message from the index.//  w w w . j  av  a2  s.  co  m
 */
private final void deleteMessagesFromIndex(long[] messages) throws IOException {
    if (messages == null) {
        return;
    }
    IndexReader reader = getReader();
    if (reader == null) {
        //Reader will be null if the search index doesn't exist.
        return;
    }
    try {
        Term messageIDTerm;
        for (int i = 0; i < messages.length; i++) {
            messageIDTerm = new Term("messageID", Long.toString(messages[i]));
            try {
                reader.delete(messageIDTerm);
            }
            //Bug in current version of Lucene. It will throw a null pointer
            //if the term to be deleted isn't found in the index.
            catch (Exception e) {
            }
        }
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.knowgate.lucene.ContactIndexer.java

License:Open Source License

public static void addOrReplaceContact(Properties oProps, String sGuid, String sWorkArea, JDCConnection oConn)
        throws ClassNotFoundException, IOException, IllegalArgumentException, NoSuchFieldException,
        IllegalAccessException, InstantiationException, NullPointerException, SQLException {
    String sDirectory = oProps.getProperty("luceneindex");

    if (DebugFile.trace) {
        DebugFile.writeln("Begin ContactIndexer.addOrReplaceContact([Properties]," + sGuid + "," + sWorkArea
                + ", [JDCConnection])");
        DebugFile.incIdent();/*ww  w.  j  a v  a2s. c  o  m*/
    }

    if (null == sDirectory) {
        if (DebugFile.trace)
            DebugFile.decIdent();
        throw new NoSuchFieldException("Cannot find luceneindex property");
    }

    sDirectory = Gadgets.chomp(sDirectory, File.separator) + "k_contacts" + File.separator + sWorkArea;

    if (DebugFile.trace)
        DebugFile.writeln("index directory is " + sDirectory);

    File oDir = new File(sDirectory);
    boolean bNewIndex = !oDir.exists();

    if (oDir.exists()) {
        File[] aFiles = oDir.listFiles();
        if (aFiles == null) {
            bNewIndex = true;
        } else if (aFiles.length == 0) {
            bNewIndex = true;
        }
    }

    if (bNewIndex) {
        Indexer.rebuild(oProps, "k_contacts", sWorkArea);
    }

    if (DebugFile.trace)
        DebugFile.writeln("IndexReader.open(" + sDirectory + ")");

    Directory oFsDir = Indexer.openDirectory(sDirectory);

    IndexReader oIRdr = IndexReader.open(oFsDir);
    oIRdr.deleteDocuments(new Term("guid", sGuid));
    oIRdr.close();

    IndexWriter oIWrt = new IndexWriter(oFsDir, instantiateAnalyzer(oProps),
            IndexWriter.MaxFieldLength.LIMITED);

    addDocument(oIWrt, sGuid, sWorkArea, oConn);

    oIWrt.close();
    oFsDir.close();

    if (DebugFile.trace) {
        DebugFile.decIdent();
        DebugFile.writeln("End ContactIndexer.addOrReplaceContact()");
    }

}

From source file:com.knowgate.lucene.Indexer.java

License:Open Source License

/**
 * <p>Rebuild Full Text Index for a table restricting to a given WorkArea</p>
 * Indexed documents have the following fields:<br>
 * <table border=1 cellpadding=4>// w  w w .  j  a  v a  2 s .  c  o  m
 * <tr><td><b>Field Name</b></td><td><b>Description</b></td><td><b>Indexed</b></td><td><b>Stored</b></td></tr>
 * <tr><td>workarea</td><td>GUID of WorkArea</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>container</td><td>Name of Container (NewsGroup, Project, etc)</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>guid</td><td>GUID for Retrieved Object</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>number</td><td>Object Ordinal Identifier</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>title</td><td>Title or Subject</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>author</td><td>Author</td><td align=middle>Yes</td><td align=middle>Yes</td></tr>
 * <tr><td>text</td><td>Document Text</td><td align=middle>Yes</td><td align=middle>No</td></tr>
 * <tr><td>abstract</td><td>First 80 characters of text</td><td align=middle>No</td><td align=middle>Yes</td></tr>
 * </table>
 * @param oProps Properties Collection (typically loaded from hipergate.cnf) containing:<br>
 * <b>driver</b> : Class name for JDBC driver<br>
 * <b>dburl</b> : Database Connection URL<br>
 * <b>dbuser</b> : Database User<br>
 * <b>dbpassword</b> : Database User Password<br>
 * <b>luceneindex</b> : Base path for Lucene index directories,
 * the rebuilded index will be stored at a subdirectory called as the table name.<br>
 * @param sTableName Name of table to be indexed (currently only k_bugs, k_newsmsgs or k_mime_msgs are permitted)
 * <b>analyzer</b> : org.apache.lucene.analysis.Analyzer subclass name
 * @param sWorkArea GUID of WorkArea to be rebuilt
 * @throws NoSuchFieldException If any of the requiered properties of oProps is not found
 * @throws ClassNotFoundException If JDBC driver or analyzer classes are not found
 * @throws SQLException
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public static void rebuild(Properties oProps, String sTableName, String sWorkArea)
        throws SQLException, IOException, ClassNotFoundException, IllegalArgumentException,
        NoSuchFieldException, IllegalAccessException, InstantiationException {

    String sGuid, sThread, sContainer, sTitle, sAuthor, sComments, sText;
    Date dtCreated;
    BigDecimal dNumber;
    int iNumber, iSize;

    final BigDecimal dZero = new BigDecimal(0);

    // Check whether table name is any of the allowed ones
    if (!allowedTable(sTableName))
        throw new IllegalArgumentException("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");

    if (DebugFile.trace) {
        DebugFile.writeln("Begin Indexer.rebuild([Properties]," + sTableName + "," + sWorkArea + ")");
        DebugFile.incIdent();
    }

    // Get physical base path to index files from luceneindex property
    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory) {
        if (DebugFile.trace)
            DebugFile.decIdent();
        throw new NoSuchFieldException("Cannot find luceneindex property");
    }

    // Append WorkArea and table name to luceneindex base path
    sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase();
    if (null != sWorkArea)
        sDirectory += File.separator + sWorkArea;

    if (DebugFile.trace)
        DebugFile.writeln("index directory is " + sDirectory);

    if (null == oProps.getProperty("driver")) {
        if (DebugFile.trace)
            DebugFile.decIdent();
        throw new NoSuchFieldException("Cannot find driver property");
    }

    if (null == oProps.getProperty("dburl")) {
        if (DebugFile.trace)
            DebugFile.decIdent();
        throw new NoSuchFieldException("Cannot find dburl property");
    }

    if (DebugFile.trace)
        DebugFile.writeln("Class.forName(" + oProps.getProperty("driver") + ")");

    Class oDriver = Class.forName(oProps.getProperty("driver"));

    if (DebugFile.trace)
        DebugFile.writeln("IndexReader.open(" + sDirectory + ")");

    // *********************************************************************
    // Delete every document from this table and WorkArea before re-indexing
    File oDir = new File(sDirectory);
    if (oDir.exists()) {
        File[] aFiles = oDir.listFiles();
        if (null != aFiles) {
            if (aFiles.length > 0) {
                Directory oFsDir = openDirectory(sDirectory);
                IndexReader oReader = IndexReader.open(oFsDir);
                int iDeleted = oReader.deleteDocuments(new Term("workarea", sWorkArea));
                oReader.close();
                oFsDir.close();
            }
        }
    } else {
        FileSystem oFS = new FileSystem();
        try {
            oFS.mkdirs(sDirectory);
        } catch (Exception e) {
            throw new IOException(e.getClass().getName() + " " + e.getMessage());
        }
    }
    // *********************************************************************

    if (DebugFile.trace)
        DebugFile.writeln("new IndexWriter(" + sDirectory + ",[Analyzer], true)");

    Directory oFsDir = openDirectory(sDirectory);
    IndexWriter oIWrt = new IndexWriter(oFsDir, instantiateAnalyzer(oProps),
            IndexWriter.MaxFieldLength.LIMITED);

    if (DebugFile.trace)
        DebugFile.writeln("DriverManager.getConnection(" + oProps.getProperty("dburl") + ", ...)");

    Connection oConn = DriverManager.getConnection(oProps.getProperty("dburl"), oProps.getProperty("dbuser"),
            oProps.getProperty("dbpassword"));
    oConn.setAutoCommit(true);

    Statement oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    ResultSet oRSet;

    if (sTableName.equalsIgnoreCase("k_bugs")) {

        if (DebugFile.trace)
            DebugFile.writeln(
                    "Statement.executeQuery(SELECT p.gu_workarea,p.nm_project,b.gu_bug,b.tl_bug,b.dt_modified,"
                            + IfNull(oConn) + "(b.nm_reporter,'')," + IfNull(oConn) + "(b.tx_bug_brief,''),"
                            + IfNull(oConn)
                            + "(b.tx_comments,'') FROM k_bugs b, k_projects p WHERE b.gu_project=p.gu_project AND p.gu_owner='"
                            + sWorkArea + "')");

        oRSet = oStmt.executeQuery("SELECT p.gu_owner,p.nm_project,b.gu_bug,b.pg_bug,b.tl_bug,b.dt_modified,"
                + IfNull(oConn) + "(b.nm_reporter,'')," + IfNull(oConn) + "(b.tx_comments,'')," + IfNull(oConn)
                + "(b.tx_bug_brief,'') FROM k_bugs b, k_projects p WHERE b.gu_project=p.gu_project AND p.gu_owner='"
                + sWorkArea + "'");

        while (oRSet.next()) {
            sWorkArea = oRSet.getString(1);
            sContainer = oRSet.getString(2);
            sGuid = oRSet.getString(3);
            iNumber = oRSet.getInt(4);
            sTitle = oRSet.getString(5);
            dtCreated = oRSet.getDate(6);
            sAuthor = oRSet.getString(7);
            sComments = oRSet.getString(8);
            if (null == sComments)
                sComments = "";
            sText = oRSet.getString(9);
            if (null == sText)
                sText = "";
            BugIndexer.addBug(oIWrt, sGuid, iNumber, sWorkArea, sContainer, sTitle, sAuthor, dtCreated,
                    sComments, sText);
        } // wend
        oRSet.close();
    }

    else if (sTableName.equalsIgnoreCase("k_newsmsgs")) {

        if (DebugFile.trace)
            DebugFile.writeln(
                    "Statement.executeQuery(SELECT g.gu_workarea,c.nm_category,m.gu_msg,m.tx_subject,m.dt_published,"
                            + IfNull(oConn) + "(b.nm_author,'')," + IfNull(oConn)
                            + "(b.tx_msg,'') FROM k_newsmsgs m, k_categories c, k_newsgroups g, k_x_cat_objs x WHERE m.id_status=0 AND m.gu_msg=x.gu_object AND x.gu_category=g.gu_newsgrp AND c.gu_category=g.gu_newsgrp AND g.gu_workarea='"
                            + sWorkArea + "')");

        oRSet = oStmt.executeQuery(
                "SELECT g.gu_workarea,c.nm_category,m.gu_msg,m.gu_thread_msg,m.tx_subject,m.dt_published,"
                        + IfNull(oConn) + "(m.nm_author,'')," + IfNull(oConn)
                        + "(m.tx_msg,'') FROM k_newsmsgs m, k_categories c, k_newsgroups g, k_x_cat_objs x WHERE m.id_status=0 AND m.gu_msg=x.gu_object AND x.gu_category=g.gu_newsgrp AND c.gu_category=g.gu_newsgrp AND g.gu_workarea='"
                        + sWorkArea + "'");

        while (oRSet.next()) {
            sWorkArea = oRSet.getString(1);
            sContainer = oRSet.getString(2);
            sGuid = oRSet.getString(3);
            sThread = oRSet.getString(4);
            sTitle = oRSet.getString(5);
            dtCreated = oRSet.getDate(6);
            sAuthor = oRSet.getString(7);
            sText = oRSet.getString(8);
            NewsMessageIndexer.addNewsMessage(oIWrt, sGuid, sThread, sWorkArea, sContainer, sTitle, sAuthor,
                    dtCreated, sText);
        } // wend
        oRSet.close();
    }
    // Inicio I2E 2009-12-23
    else if (sTableName.equalsIgnoreCase("k_contacts")) {

        Map<String, ContactRecord> contacts = new HashMap<String, ContactRecord>();
        String consultas[] = new String[6];
        consultas[0] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, csc.nm_scourse, csc.lv_scourse FROM k_contacts c, k_contact_short_courses csc WHERE c.gu_workarea='"
                + sWorkArea + "' AND csc.gu_contact = c.gu_contact";
        consultas[1] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, ccsl.tr_es,ccsl2.tr_es FROM k_contacts c, k_contact_computer_science ccc, k_contact_computer_science_lookup ccsl, k_contact_computer_science_lookup ccsl2 WHERE c.gu_workarea='"
                + sWorkArea
                + "' AND ccc.gu_contact = c.gu_contact AND ccc.nm_skill = ccsl.vl_lookup AND ccc.lv_skill = ccsl2.vl_lookup";
        consultas[2] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, ccsl.tr_en,ccsl2.tr_en FROM k_contacts c, k_contact_computer_science ccc, k_contact_computer_science_lookup ccsl, k_contact_computer_science_lookup ccsl2 WHERE c.gu_workarea='"
                + sWorkArea
                + "' AND ccc.gu_contact = c.gu_contact AND ccc.nm_skill = ccsl.vl_lookup AND ccc.lv_skill = ccsl2.vl_lookup";
        consultas[3] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, ed.nm_degree,'' as level FROM k_contacts c,k_contact_education ce,k_education_degree ed WHERE c.gu_workarea='"
                + sWorkArea + "' AND ce.gu_contact = c.gu_contact AND ce.gu_degree= ed.gu_degree";
        consultas[4] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, ll.tr_lang_es,cll.tr_es FROM k_contacts c, k_contact_languages cl, k_lu_languages ll,k_contact_languages_lookup cll WHERE c.gu_workarea='"
                + sWorkArea
                + "' AND c.gu_contact = cl.gu_contact AND cl.id_language = ll.id_language AND cl.lv_language_degree = cll.vl_lookup";
        consultas[5] = "SELECT c.gu_contact, c.gu_workarea, c.tx_name, c.tx_surname, ll.tr_lang_en,cll.tr_en FROM k_contacts c, k_contact_languages cl, k_lu_languages ll,k_contact_languages_lookup cll WHERE c.gu_workarea='"
                + sWorkArea
                + "' AND c.gu_contact = cl.gu_contact AND cl.id_language = ll.id_language AND cl.lv_language_degree = cll.vl_lookup";

        for (int i = 0; i < consultas.length; i++) {
            if (DebugFile.trace)
                DebugFile.writeln("Statement.executeQuery(" + consultas[i] + ")");

            oRSet = oStmt.executeQuery(consultas[i]);

            while (oRSet.next()) {
                sGuid = oRSet.getString(1);
                sWorkArea = oRSet.getString(2);
                String sName = oRSet.getString(3);
                String sSurname = oRSet.getString(4);
                String sValue = oRSet.getString(5);
                String sLevel = oRSet.getString(6);
                if (sLevel == null)
                    sLevel = "";
                ContactRecord contact = contacts.get(sGuid);
                if (contact == null) {
                    contact = new ContactRecord(null, sName + " " + sSurname, sWorkArea, sGuid);
                    contacts.put(sGuid, contact);
                }
                contact.addValue(sValue, sLevel);

                //ContactIndexer.addDocument(oIWrt, sGuid, sWorkArea, sName, sSurname, ContactRecord.COURSE, sValue, sLevel,null);

            }
            oRSet.close();
        }
        ContactRecord arrayContactos[] = contacts.values().toArray(new ContactRecord[contacts.size()]);
        for (int i = 0; i < arrayContactos.length; i++) {
            ContactIndexer.addDocument(oIWrt, arrayContactos[i]);
        }
    }

    //Fin i2E
    else if (sTableName.equalsIgnoreCase("k_mime_msgs")) {

        LinkedList oIndexedGuids = new LinkedList();

        PreparedStatement oRecp = oConn.prepareStatement(
                "SELECT tx_personal,tx_email FROM k_inet_addrs WHERE tp_recipient<>'to' AND gu_mimemsg=?",
                ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

        if (DebugFile.trace)
            DebugFile.writeln(
                    "Statement.executeQuery(SELECT g.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_mail_from,m.pg_mimemsg,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted<>0 AND m.bo_draft<>0 AND m.gu_category=c.gu_category AND m.gu_workarea='"
                            + sWorkArea + "')");

        oRSet = oStmt.executeQuery(
                "SELECT g.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_mail_from,m.pg_mimemsg,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted<>0 AND m.bo_draft<>0 AND m.gu_category=c.gu_category AND m.gu_workarea='"
                        + sWorkArea + "'");

        while (oRSet.next()) {

            sWorkArea = oRSet.getString(1);
            sContainer = oRSet.getString(2);
            sGuid = oRSet.getString(3);
            sTitle = oRSet.getString(4);
            sAuthor = oRSet.getString(5);
            if (oRSet.wasNull())
                sAuthor = "";
            sAuthor += " " + oRSet.getString(6);
            dNumber = oRSet.getBigDecimal(7);
            if (oRSet.wasNull())
                dNumber = dZero;
            sComments = oRSet.getString(8);
            dtCreated = oRSet.getDate(9);
            iSize = oRSet.getInt(10);

            if (DebugFile.trace)
                DebugFile.writeln("Indexing message " + sGuid + " - " + sTitle);

            InputStream oStrm = oRSet.getBinaryStream(11);

            String sRecipients = "";
            oRecp.setString(1, sGuid);
            ResultSet oRecs = oRecp.executeQuery();
            while (oRecs.next()) {
                sRecipients += oRecs.getString(1) + " " + oRecs.getString(2) + " ";
            } // wend
            oRecs.close();

            MailIndexer.addMail(oIWrt, sGuid, dNumber, sWorkArea, sContainer, sTitle, sAuthor, sRecipients,
                    dtCreated, sComments, oStrm, iSize);

            oIndexedGuids.add(sGuid);
        } // wend
        oRSet.close();
        oRecp.close();

        PreparedStatement oUpdt = oConn
                .prepareStatement("UPDATE k_mime_msgs SET bo_indexed=1 WHERE gu_mimemsg=?");
        ListIterator oIter = oIndexedGuids.listIterator();
        while (oIter.hasNext()) {
            oUpdt.setObject(1, oIter.next(), java.sql.Types.CHAR);
            oUpdt.executeUpdate();
        } // wend
        oUpdt.close();
    } // fi

    oStmt.close();
    oConn.close();

    if (DebugFile.trace)
        DebugFile.writeln("IndexWriter.optimize()");

    oIWrt.optimize();

    if (DebugFile.trace)
        DebugFile.writeln("IndexWriter.close()");

    oIWrt.close();
    oFsDir.close();

    if (DebugFile.trace) {
        DebugFile.decIdent();
        DebugFile.writeln("End Indexer.rebuild()");
    }
}

From source file:com.knowgate.lucene.Indexer.java

License:Open Source License

/**
 * Delete a document with a given GUID//  w ww .  j  av a  2s .com
 * @param sTableName k_bugs, k_newsmsgs or k_mime_msgs
 * @param oProps Properties Collection containing luceneindex directory
 * @param sGuid Document GUID
 * @return Number of documents deleted
 * @throws IllegalArgumentException If sTableName is not one of { k_bugs, k_newsmsgs, k_mime_msgs }
 * @throws NoSuchFieldException If luceneindex property is not found at oProps
 * @throws IllegalAccessException
 * @throws IOException
 * @throws NullPointerException If sGuid is <b>null</b>
 */
public static int delete(String sTableName, String sWorkArea, Properties oProps, String sGuid)
        throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException, IOException,
        NullPointerException {

    if (null == sGuid)
        throw new NullPointerException("Document GUID may not be null");

    if (!allowedTable(sTableName))
        throw new IllegalArgumentException("Table name must be k_bugs or k_newsmsgs or k_mime_msgs");

    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory)
        throw new NoSuchFieldException("Cannot find luceneindex property");

    sDirectory = Gadgets.chomp(sDirectory, File.separator) + sTableName.toLowerCase() + File.separator
            + sWorkArea;
    File oDir = new File(sDirectory);
    if (!oDir.exists()) {
        FileSystem oFS = new FileSystem();
        try {
            oFS.mkdirs(sDirectory);
        } catch (Exception e) {
            throw new IOException(e.getClass().getName() + " " + e.getMessage());
        }
    } // fi

    NIOFSDirectory oFsDir = new NIOFSDirectory(new File(sDirectory));
    IndexReader oReader = IndexReader.open(oFsDir);

    int iDeleted = oReader.deleteDocuments(new Term("guid", sGuid));

    oReader.close();
    oFsDir.close();

    return iDeleted;
}

From source file:com.knowgate.lucene.MailIndexer.java

License:Open Source License

/**
 * <p>Re-build full text index for a given mail folder</p>
 * All previously indexed messages for given folder are removed from index and written back
 * @param oProps Properties containing: luceneindex, driver, dburl, dbuser, dbpassword
 * @param sWorkArea String GUID of WorkArea to which folder belongs
 * @param sFolder String Folder name as in field nm_category of table k_categories
 * @throws SQLException/*from  w  ww. ja v a  2s .co  m*/
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws IllegalArgumentException
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public static void rebuildFolder(Properties oProps, String sWorkArea, String sFolder)
        throws SQLException, IOException, ClassNotFoundException, IllegalArgumentException,
        NoSuchFieldException, IllegalAccessException, InstantiationException {

    String sGuid, sContainer, sTitle, sAuthor, sComments;
    Date dtCreated;
    BigDecimal dNumber;
    int iSize;

    final BigDecimal dZero = new BigDecimal(0);

    if (DebugFile.trace) {
        DebugFile.writeln("Begin MailIndexer.rebuildFolder([Properties]" + sWorkArea + "," + sFolder + ")");
        DebugFile.incIdent();
    }

    // Get physical base path to index files from luceneindex property
    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory)
        throw new NoSuchFieldException("Cannot find luceneindex property");

    // Append WorkArea and table name to luceneindex base path
    sDirectory = Gadgets.chomp(sDirectory, File.separator) + "k_mime_msgs";
    if (null != sWorkArea)
        sDirectory += File.separator + sWorkArea;

    if (DebugFile.trace)
        DebugFile.writeln("index directory is " + sDirectory);

    if (null == oProps.getProperty("driver"))
        throw new NoSuchFieldException("Cannot find driver property");

    if (null == oProps.getProperty("dburl"))
        throw new NoSuchFieldException("Cannot find dburl property");

    if (DebugFile.trace)
        DebugFile.writeln("Class.forName(" + oProps.getProperty("driver") + ")");

    Class oDriver = Class.forName(oProps.getProperty("driver"));

    if (DebugFile.trace)
        DebugFile.writeln("IndexReader.open(" + sDirectory + ")");

    // *********************************************************************
    // Delete every document from this folder before re-indexing

    File oDir = new File(sDirectory);
    if (oDir.exists()) {
        File[] aSegments = oDir.listFiles();
        if (null != aSegments) {
            if (aSegments.length > 0) {
                Directory oRdDir = Indexer.openDirectory(sDirectory);
                IndexReader oReader = IndexReader.open(oRdDir);
                int iDeleted = oReader.deleteDocuments(new Term("container", sFolder));
                oReader.close();
                oRdDir.close();
            } // fi 
        } // fi
    } else {
        FileSystem oFS = new FileSystem();
        try {
            oFS.mkdirs(sDirectory);
        } catch (Exception e) {
            throw new IOException(e.getClass().getName() + " " + e.getMessage());
        }
    } // fi    
      // *********************************************************************

    if (DebugFile.trace)
        DebugFile.writeln("new IndexWriter(" + sDirectory + ",[Analyzer], true)");

    Directory oFsDir = Indexer.openDirectory(sDirectory);
    IndexWriter oIWrt = new IndexWriter(oFsDir, Indexer.instantiateAnalyzer(oProps),
            IndexWriter.MaxFieldLength.LIMITED);

    if (DebugFile.trace)
        DebugFile.writeln("DriverManager.getConnection(" + oProps.getProperty("dburl") + ", ...)");

    Connection oConn = DriverManager.getConnection(oProps.getProperty("dburl"), oProps.getProperty("dbuser"),
            oProps.getProperty("dbpassword"));

    Statement oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    ResultSet oRSet;

    PreparedStatement oRecp = oConn.prepareStatement(
            "SELECT tx_personal,tx_email FROM k_inet_addrs WHERE tp_recipient<>'to' AND gu_mimemsg=?",
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

    if (DebugFile.trace)
        DebugFile.writeln(
                "Statement.executeQuery(SELECT m.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_email_from,m.pg_message,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted=0 AND m.bo_draft=0 AND m.gu_category=c.gu_category AND m.gu_workarea='"
                        + sWorkArea + "' AND c.nm_category='" + sFolder + "')");

    oRSet = oStmt.executeQuery(
            "SELECT m.gu_workarea,c.nm_category,m.gu_mimemsg,m.tx_subject,m.nm_from,m.tx_email_from,m.pg_message,m.de_mimemsg,m.dt_sent,m.len_mimemsg,m.by_content FROM k_mime_msgs m, k_categories c WHERE m.bo_deleted=0 AND m.bo_draft=0 AND m.gu_category=c.gu_category AND m.gu_workarea='"
                    + sWorkArea + "' AND c.nm_category='" + sFolder + "'");

    int nIndexed = 0;

    while (oRSet.next()) {

        sWorkArea = oRSet.getString(1);
        sContainer = oRSet.getString(2);
        sGuid = oRSet.getString(3);
        sTitle = oRSet.getString(4);
        sAuthor = oRSet.getString(5);
        if (oRSet.wasNull())
            sAuthor = "";
        sAuthor += " " + oRSet.getString(6);
        dNumber = oRSet.getBigDecimal(7);
        if (oRSet.wasNull())
            dNumber = dZero;
        sComments = oRSet.getString(8);
        if (oRSet.wasNull())
            sComments = "";
        dtCreated = oRSet.getDate(9);
        iSize = oRSet.getInt(10);

        if (DebugFile.trace)
            DebugFile.writeln("Indexing message " + sGuid + " - " + sTitle);

        InputStream oStrm = oRSet.getBinaryStream(11);

        String sRecipients = "";
        oRecp.setString(1, sGuid);
        ResultSet oRecs = oRecp.executeQuery();
        while (oRecs.next()) {
            String sTxPersonal = oRecs.getString(1);
            if (oRecs.wasNull())
                sRecipients += oRecs.getString(2) + " ";
            else
                sRecipients += oRecs.getString(1) + " " + oRecs.getString(2) + " ";
        } // wend
        oRecs.close();

        MailIndexer.addMail(oIWrt, sGuid, dNumber, sWorkArea, sContainer, sTitle, sAuthor, sRecipients,
                dtCreated, sComments, oStrm, iSize);
        nIndexed++;

    } // wend

    if (DebugFile.trace) {
        DebugFile.writeln(String.valueOf(nIndexed) + " messages indexed");
    }

    oRSet.close();
    oRecp.close();

    if (DebugFile.trace) {
        DebugFile.writeln("Statement.executeUpdate(UPDATE k_mime_msgs SET bo_indexed=1 WHERE gu_workarea='"
                + sWorkArea + "' AND gu_category IN (SELECT gu_category FROM k_categories WHERE nm_category='"
                + sFolder + "'))");
    }

    oStmt.executeUpdate("UPDATE k_mime_msgs SET bo_indexed=1 WHERE gu_workarea='" + sWorkArea
            + "' AND gu_category IN (SELECT gu_category FROM k_categories WHERE nm_category='" + sFolder
            + "')");

    oStmt.close();
    oConn.close();

    if (DebugFile.trace)
        DebugFile.writeln("IndexWriter.optimize()");

    oIWrt.optimize();

    if (DebugFile.trace)
        DebugFile.writeln("IndexWriter.close()");

    oIWrt.close();
    oFsDir.close();

    if (DebugFile.trace) {
        DebugFile.decIdent();
        DebugFile.writeln("End MailIndexer.rebuildFolder()");
    }
}

From source file:com.knowgate.lucene.NewsMessageIndexer.java

License:Open Source License

public static void addOrReplaceNewsMessage(Properties oProps, String sGuid, String sThread, String sWorkArea,
        String sContainer, String sTitle, String sAuthor, Date dtCreated, String sText)
        throws ClassNotFoundException, IOException, IllegalArgumentException, NoSuchFieldException,
        IllegalAccessException, InstantiationException, NullPointerException {

    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory) {
        if (DebugFile.trace)
            DebugFile.decIdent();/*from ww w  .  ja va 2 s .com*/
        throw new NoSuchFieldException("Cannot find luceneindex property");
    }

    sDirectory = Gadgets.chomp(sDirectory, File.separator) + "k_newsmsgs" + File.separator + sWorkArea;

    if (DebugFile.trace)
        DebugFile.writeln("index directory is " + sDirectory);

    File oDir = new File(sDirectory);
    boolean bNewIndex = !oDir.exists();
    if (bNewIndex) {
        FileSystem oFs = new FileSystem();
        try {
            oFs.mkdirs("file://" + sDirectory);
        } catch (Exception xcpt) {
            if (DebugFile.trace)
                DebugFile.writeln(xcpt.getClass() + " " + xcpt.getMessage());
            throw new FileNotFoundException(
                    "Could not create directory " + sDirectory + " " + xcpt.getMessage());
        }
    } else {
        File[] aFiles = oDir.listFiles();
        if (aFiles == null) {
            bNewIndex = true;
        } else if (aFiles.length == 0) {
            bNewIndex = true;
        }
    }

    Directory oFsDir = Indexer.openDirectory(sDirectory);

    if (!bNewIndex) {
        if (DebugFile.trace)
            DebugFile.writeln("new IndexReader(...)");
        IndexReader oIRdr = IndexReader.open(oFsDir, false);
        if (DebugFile.trace)
            DebugFile.writeln("IndexReader.deleteDocuments(" + sGuid + ")");
        oIRdr.deleteDocuments(new Term("guid", sGuid));
        oIRdr.close();
    }

    if (DebugFile.trace)
        DebugFile.writeln("new IndexWriter(...)");

    IndexWriter oIWrt = new IndexWriter(oFsDir, Indexer.instantiateAnalyzer(oProps),
            IndexWriter.MaxFieldLength.LIMITED);

    addNewsMessage(oIWrt, sGuid, sThread, sWorkArea, sContainer, sTitle, sAuthor, dtCreated, sText);

    oIWrt.close();
    oFsDir.close();

}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * Delete a document contained within the lucene index
 * /*from www .  j  a  va  2s.  co  m*/
 * @param documentId
 * @throws Exception
 */
public void deleteDocument(int documentId) throws Exception {
    synchronized (this) {
        this.documentsDeleteCount++;
    }

    this.logger.debug("Deleting document: " + documentId);
    IndexReader reader = IndexReader.open(this.indexDirectory);
    int deleted = reader.deleteDocuments(new Term("DocumentID", IndexerManager.longToString(documentId)));
    reader.close();
    this.logger.debug("Deleted " + deleted + " documents.");

    this.reopenIndex();
}

From source file:com.krawler.esp.indexer.KrawlerIndexCreator.java

License:Open Source License

public void DeleteIndex(String documentId) {
    Term t = new Term("DocumentId", documentId);
    try {/*www .jav  a  2s .co m*/
        IndexReader docInRead = IndexReader.open(this.indexPath);
        docInRead.deleteDocuments(t);
        docInRead.close();
        IndexWriter inw = new IndexWriter(this.indexPath, KWLAnalyzer, false);
        inw.optimize();
        inw.close();
    } catch (Exception ex) {
        System.out.print(ex.toString());
    }

}