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

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

Introduction

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

Prototype

@Override
public final long commit() throws IOException 

Source Link

Document

Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.

Usage

From source file:de.blizzy.documentr.search.InaccessibleDocIdsCollectorTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.commit();
    writer.close(true);//from  w ww .  j  av  a  2 s.c  o m

    reader = DirectoryReader.open(directory);

    collector = new InaccessibleDocIdsCollector(Permission.VIEW, authentication, permissionEvaluator);
}

From source file:de.blizzy.documentr.search.PagePermissionFilterTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.commit();
    writer.close(true);//from   w w  w  .j a v  a2  s  .  c om

    reader = DirectoryReader.open(directory);

    BitSet docs = new BitSet();
    docs.set(1);
    Bits docIds = new DocIdBitSet(docs);
    filter = new PagePermissionFilter(docIds);
}

From source file:de.dfki.km.perspecting.obie.corpus.TextCorpus.java

License:Open Source License

/**
 * Returns a Lucene index on this {@link TextCorpus}.
 * //w w  w.  j  a  v a2s .  c o m
 * @param dir
 *            The directory the index is stored.
 * @param reindex
 *            If <code>true</code>, an existing index will be re-created.
 * @return Access to the Lucene index.
 * 
 * @throws Exception
 */
public IndexSearcher getLuceneIndex(File dir, boolean reindex) throws Exception {

    if (dir.exists()) {
        if (reindex) {
            FileUtils.deleteDirectory(dir);
            log.info("deleted directory: " + dir);
        } else {
            return new IndexSearcher(dir.getAbsolutePath());
        }
    }

    dir.mkdirs();
    log.info("created directory: " + dir);

    final WhitespaceAnalyzer analyser = new WhitespaceAnalyzer();

    final IndexWriter indexWriter = new IndexWriter(dir, analyser, true, MaxFieldLength.LIMITED);
    forEach(new DocumentProcedure<String>() {
        @Override
        public String process(Reader doc, URI uri) throws Exception {
            org.apache.lucene.document.Document document = new org.apache.lucene.document.Document();
            document.add(new Field("text", doc, TermVector.YES));
            indexWriter.addDocument(document, analyser);
            log.fine("indexes: " + document);
            return uri.toString();
        }
    });
    log.info("indexed: " + indexWriter.numDocs() + " documents");

    indexWriter.commit();
    indexWriter.close();

    return new IndexSearcher(dir.getAbsolutePath());
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexSites(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {//  w  w w  .j a  v  a2  s.c om
        pst = con.prepareStatement(INDEX_SITES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            SiteSearchData data = new SiteSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " sites");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexSite(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from  ww w .jav  a 2 s  .  c o m*/
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            SiteSearchData data = new SiteSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing site");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexPages(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {//ww w  . j  a  v  a 2 s.  c  om
        pst = con.prepareStatement(INDEX_PAGES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            PageSearchData data = new PageSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " pages");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexPage(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from w  w  w .j  a  v a2 s.  c o m*/
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            PageSearchData data = new PageSearchData();
            getSearchData(data, rs);
            //todo read parts
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing page");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexFiles(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*  www.  j  ava 2 s .  c o m*/
        pst = con.prepareStatement(INDEX_FILES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            FileSearchData data = new FileSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " files");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexFile(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from  w  ww .  ja  v a 2 s  . c  o  m*/
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            FileSearchData data = new FileSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing file");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexUsers(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*www . ja  va2  s .  co m*/
        int count = 0;
        pst = con.prepareStatement(INDEX_USERS_SQL);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            SearchData data = getUserSearchData(rs);
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " users");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}