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:com.aurel.track.lucene.index.associatedFields.LinkIndexer.java

License:Open Source License

/**
 * Adds an attachment to the attachment index
 * Used by attaching a new file to the workItem 
 * @param attachFile/*w ww  . jav  a 2 s .  co m*/
 */
@Override
public void addToIndex(Object object, boolean add) {
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false");
        return;
    }
    IndexWriter indexWriter = LuceneIndexer.getIndexWriter(getIndexWriterID());
    if (indexWriter == null) {
        LOGGER.error("IndexWriter null by adding a link");
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Save a new " + add + " " + getLuceneFieldName());
    }
    TWorkItemLinkBean workItemLinkBean = (TWorkItemLinkBean) object;
    if (!add) {
        Integer objectID = workItemLinkBean.getObjectID();
        if (objectID != null) {
            Term keyTerm = new Term(getObjectIDFieldName(), objectID.toString());
            try {
                indexWriter.deleteDocuments(keyTerm);
                indexWriter.commit();
            } catch (IOException e) {
                LOGGER.error("Removing the entity " + objectID + " failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
    try {
        Document doc = createDocument(workItemLinkBean);
        if (doc != null) {
            indexWriter.addDocument(doc);
        }
    } catch (IOException e) {
        LOGGER.error("Adding an link to the index failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
    try {
        indexWriter.commit();
    } catch (IOException e) {
        LOGGER.error("Flushing the link failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.aurel.track.lucene.index.listFields.AbstractListFieldIndexer.java

License:Open Source License

/**
 * Deletes a document by key//w  w  w.  ja v a  2  s. co  m
 * @param objectID
 */
public synchronized void deleteByKeyAndType(Integer objectID, int type) {
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false by deleting the entry " + objectID + " from list " + type);
        return;
    }
    if (objectID != null) {
        IndexWriter indexWriter = LuceneIndexer.getIndexWriter(getIndexWriterID());
        if (indexWriter == null) {
            LOGGER.error("IndexWriter null by deleting a list option by key");
            return;
        }
        String combinedKeyFieldName = getCombinedKeyFieldName();
        String combinedKeyFieldValue = LuceneUtil.getCombinedKeyValue(objectID.toString(),
                String.valueOf(type));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Deleting the " + getListFieldType() + " document by Term " + combinedKeyFieldName
                    + "=" + combinedKeyFieldValue + " from "
                    + LuceneUtil.getDirectoryString(getIndexWriterID()));
        }
        Term keyTerm = new Term(combinedKeyFieldName, combinedKeyFieldValue);
        try {
            indexWriter.deleteDocuments(keyTerm);
        } catch (IOException e) {
            LOGGER.error("Removing the list option " + objectID + " type " + type + " failed with "
                    + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            indexWriter.commit();
        } catch (IOException e) {
            LOGGER.error("Flushing list option removal for list option " + objectID + " type " + type
                    + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:com.aurel.track.lucene.index.listFields.InternalListIndexer.java

License:Open Source License

/**
 * //from w ww.j  a  va2s  .c  om
 * Adds a ILabelBeans to the non-localized lookup index  
 * @param labelBean
 * @param type
 */
public synchronized void addLabelBean(ILabelBean labelBean, int type, boolean add) {
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false");
        return;
    }
    if (labelBean == null || labelBean.getObjectID() == null) {
        LOGGER.warn("Bean or value null by adding a internal list option of type " + type);
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Save a new " + add + " internal list option of type " + type);
    }
    Document document = createDocument(labelBean, type);
    if (document != null) {
        IndexWriter indexWriter = LuceneIndexer.getIndexWriter(getIndexWriterID());
        if (indexWriter == null) {
            LOGGER.error("IndexWriter null by adding a non-localized list option of type " + type);
            return;
        }
        try {
            indexWriter.addDocument(document);
        } catch (IOException e) {
            LOGGER.error("Adding the document for list option with key " + labelBean.getObjectID()
                    + " and type " + type + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            indexWriter.commit();
        } catch (IOException e) {
            LOGGER.error("Flushing list option with key " + labelBean.getObjectID() + " and type " + type
                    + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:com.aurel.track.lucene.index.LuceneIndexer.java

License:Open Source License

/**
 * Adds a list of TWorkItems to the workItem index
 * Used by recreating the index//from w ww.  j a v a 2  s. c o  m
 * @param indexWriter
 * @param workItemBeans the beans are considered to be fully prepared:
 *          i.e. history beans and custom attributes are set
 */
private static void addToWorkItemIndex(IndexWriter indexWriter, List<TWorkItemBean> workItemBeans) {
    //use lucene uncheched in site config
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (indexWriter == null) {
        LOGGER.error("IndexWriter null by adding a list if workItems");
        return;
    }
    prepareWorkItemBeans(workItemBeans);
    if (workItemBeans != null && !workItemBeans.isEmpty()) {
        for (TWorkItemBean workItemBean : workItemBeans) {
            //use lucene uncheched in site config
            if (!LuceneUtil.isUseLucene()) {
                return;
            }
            //addToWorkItemIndex(TWorkItemBean workItemBean) could be used,
            //but we do not use it because of the superfluous flushes
            //we make flush and optimize only after each workItem is added
            Document document = createWorkItemDocument(workItemBean);
            try {
                if (document != null) {
                    indexWriter.addDocument(document);
                }
            } catch (IOException e) {
                LOGGER.error("Adding a workItem document for workItemBean " + workItemBean.getObjectID()
                        + " failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        try {
            indexWriter.commit();
        } catch (IOException e) {
            LOGGER.error(
                    "Flushing the IndexWriter after adding all workItemBeanss failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:com.aurel.track.lucene.index.LuceneIndexer.java

License:Open Source License

/**
 * Adds a list of TWorkItems to the workItem index
 * Used by recreating the index//from w ww.j a v  a2s. c  o m
 * @param workItemBeans
 * @param fieldID the beans are considered to be fully prepared:
 *          i.e. history beans and custom attributes are set
 */
public static synchronized void addToWorkItemIndex(List<TWorkItemBean> workItemBeans, Integer fieldID) {
    //use lucene unchecked in site config
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (workItemBeans == null) {
        return;
    }
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false by adding workItemBeans " + workItemBeans.size() + " by field "
                + fieldID);
        return;
    }
    prepareWorkItemBeans(workItemBeans);
    if (workItemBeans != null && !workItemBeans.isEmpty()) {
        IndexWriter indexWriter = getWorkItemIndexWriter();
        if (indexWriter == null) {
            LOGGER.error("IndexWriter null by adding a list if workItems");
            return;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Add " + workItemBeans.size()
                    + " previously deleted workItem documents back to workItemIndex after changing a dependency for field "
                    + fieldID);
        }
        for (TWorkItemBean workItemBean : workItemBeans) {
            //use lucene uncheched in site config
            if (!LuceneUtil.isUseLucene()) {
                return;
            }
            //addToWorkItemIndex(TWorkItemBean workItemBean) could be used,
            //but we do not use it because of the superfluous flushes
            //we make flush and optimize only after each workItem is added
            Document document = createWorkItemDocument(workItemBean);
            try {
                if (document != null) {
                    indexWriter.addDocument(document);
                }
            } catch (IOException e) {
                LOGGER.error("Adding a workItem document for workItemBean " + workItemBean.getObjectID()
                        + " failed with IOException" + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        try {
            indexWriter.commit();
        } catch (IOException e) {
            LOGGER.error("Flushing the workItemBeans failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }

    }
}

From source file:com.aurel.track.lucene.index.LuceneIndexer.java

License:Open Source License

/**
 * Adds a TWorkItem to the workitem index
 * Used by creating a new and by updating (delete old and create new) of an existing workItem
 * Although workItemBean contains no history bean it is not needed
 * because by creating only an "emtpy" state change history is created.
 * However it contains the custom attribute values.
 * @param workItemBean//from   w  w w.j ava  2  s  .co m
 */
public static synchronized void addToWorkItemIndex(TWorkItemBean workItemBean, boolean deleteExisting) {
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false");
        return;
    }
    if (workItemBean == null || workItemBean.getObjectID() == null) {
        LOGGER.error("TWorkItemBean or ObjectID null by adding/updating a workitem");
        return;
    }
    IndexWriter indexWriter = getWorkItemIndexWriter();
    if (indexWriter == null) {
        LOGGER.error("IndexWriter null by adding a workitem");
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Save a new " + !deleteExisting + " workItem into workItemIndex");
    }
    if (deleteExisting) {
        String workItemID = workItemBean.getObjectID().toString();
        Term term = new Term(LuceneUtil.getFieldName(SystemFields.ISSUENO), workItemID);
        try {
            indexWriter.deleteDocuments(term);
            indexWriter.commit();
        } catch (IOException e) {
            LOGGER.error(
                    "Removing a workItembean " + workItemID + " from the index failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }

    List<Integer> workItemIDsList = new ArrayList<Integer>();
    workItemIDsList.add(workItemBean.getObjectID());
    Map<Integer, StringBuffer> workItemsCommentsMap = HistoryLoaderBL.getByWorkItemsLongTextField(
            workItemIDsList, SystemFields.INTEGER_COMMENT, HistoryLoaderBL.LONG_TEXT_TYPE.ISPLAIN);
    String actualComment = workItemBean.getComment();
    String actualCommentPlain = "";
    try {
        actualCommentPlain = Html2Text.getNewInstance().convert(actualComment);
    } catch (Exception e) {
        LOGGER.warn("Transforming the actual comment to plain text failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
        actualCommentPlain = workItemBean.getComment();
    }
    workItemBean
            .setComment(getComments(workItemsCommentsMap.get(workItemBean.getObjectID()), actualCommentPlain));
    Document doc = createWorkItemDocument(workItemBean);
    //set the original comment back because the workItem will be used by other parts (e-mail notification, etc.)
    workItemBean.setComment(actualComment);
    if (doc != null) {
        try {
            indexWriter.addDocument(doc);
        } catch (IOException e) {
            LOGGER.error("Adding a workItemBean " + workItemBean.getObjectID() + " to the index failed with "
                    + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    try {
        indexWriter.commit();
    } catch (IOException e) {
        LOGGER.error("Flushing the workItemBean failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.aurel.track.lucene.index.LuceneIndexer.java

License:Open Source License

/**
 * Removes a list of TWorkItems from the workitem index
 * Used by deleting a project (for deleting all of its workitems)
 * @param workItemBeans/*ww w. j a  v  a 2s  . com*/
 * @return
 */
public static synchronized void deleteFromWorkItemIndex(List<TWorkItemBean> workItemBeans, Integer fieldID,
        boolean deleteProject) {
    String workItemID;
    if (!LuceneUtil.isUseLucene()) {
        return;
    }
    if (workItemBeans == null) {
        return;
    }
    if (!ClusterBL.indexInstantly()) {
        LOGGER.debug("Index instantly is false by deleting workItemBeans " + workItemBeans.size() + " by field "
                + fieldID + " and deleteProject " + deleteProject);
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        if (deleteProject) {
            LOGGER.debug("Delete " + workItemBeans.size()
                    + " workItems from workItemIndex after deleting a project with all items " + fieldID);
        } else {
            LOGGER.debug("Delete " + workItemBeans.size()
                    + " workItems from workItemIndex after changing a dependency for field " + fieldID
                    + " to add them again");
        }
    }
    IndexWriter indexWriter = getWorkItemIndexWriter();
    if (indexWriter == null) {
        LOGGER.error("IndexWriter null by deleting a list of workItems");
        return;
    }
    try {
        for (TWorkItemBean workItemBean : workItemBeans) {
            workItemID = workItemBean.getObjectID().toString();
            Term term = new Term(LuceneUtil.getFieldName(SystemFields.ISSUENO), workItemID);
            try {
                indexWriter.deleteDocuments(term);
            } catch (Exception e) {
                LOGGER.error("Removing a workItemBean " + workItemID + " from the index failed with "
                        + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    } catch (Exception e) {
    }
    try {
        indexWriter.commit();
    } catch (IOException e) {
        LOGGER.error("Flushing the deleting of workItemBeans failed with " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.b2international.index.lucene.NullIndexSearcher.java

License:Apache License

private static DirectoryReader createRamReader() throws IOException {

    final RAMDirectory directory = new RAMDirectory();
    if (!DirectoryReader.indexExists(directory)) {

        final IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
        final IndexWriter writer = new IndexWriter(directory, conf);
        writer.commit();
        writer.close();//  w  w  w. j a  v a2 s.  c o  m
    }

    return DirectoryReader.open(directory);

}

From source file:com.baidu.rigel.biplatform.tesseract.isservice.index.service.IndexWriterFactory.java

License:Open Source License

/**
 * destoryWriters/*from   ww  w .  java2 s.  c o m*/
 * 
 * @param idxPath
 *            
 * @throws IOException
 *             IO
 */
public static void destoryWriters(String idxPath) throws IOException {
    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "destoryWriters",
            "[idxPath:" + idxPath + "]"));
    IndexWriter indexWriter = null;
    if (INSTANCE.idxWriterMaps.containsKey(idxPath)) {
        indexWriter = INSTANCE.idxWriterMaps.get(idxPath);

        try {
            indexWriter.commit();
            indexWriter.close();
        } catch (IOException e) {
            if (IndexWriter.isLocked(indexWriter.getDirectory())) {
                IndexWriter.unlock(indexWriter.getDirectory());
            }
        }
        INSTANCE.idxWriterMaps.remove(idxPath);
    }

    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "destoryWriters",
            "[idxPath:" + idxPath + "]"));
}

From source file:com.baidu.rigel.biplatform.tesseract.isservice.index.service.IndexWriterFactory.java

License:Open Source License

/**
 * //from   ww w.j av  a  2  s.  c om
 * indexWriter
 * 
 * @throws IOException
 *             IO
 */
public static synchronized void destoryAllWriters() throws IOException {
    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "destoryAllWriters", "[no param]"));
    for (String key : INSTANCE.idxWriterMaps.keySet()) {
        IndexWriter writer = INSTANCE.idxWriterMaps.get(key);
        try {
            writer.commit();
            writer.close();
        } catch (IOException e) {
            if (IndexWriter.isLocked(writer.getDirectory())) {
                IndexWriter.unlock(writer.getDirectory());
            }

        }
        INSTANCE.idxWriterMaps.remove(key);
    }

    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "destoryAllWriters", "[no param]"));
}